Latest Objective-C features in iOS6

Source: Internet
Author: User

WWDC2012 released iOS6 and introduced some new features for Objective C to simplify programming. The following are new features that require XCode4.4 and later versions: 1. the declarative sequence of methods is no longer required to call the methods declared later in the method. The compiler will help to find the declarative statement of the method, and the sequence is no longer required. @ Interface SongPlayer: NSObject-(void) playSong :( Song *) song; @ end @ implementation SongPlayer-(void) playSong :( Song *) song {NSError * error; [self startAudio: & error]; // The method is not defined before XCode4.4. You can safely use XCode4.4 ...} -(void) startAudio :( NSError **) error {...} @ end2. enumeration supports the following methods for defining enumeration before the strong type XCode4.4, which is equivalent to defining an enumeration type of the int type. Typedef enum {NSNumberFormatterNoStyle, numeric, NSNumberFormatterCurrencyStyle, NSNumberFormatterPercentStyle, numeric, numeric} NSNumberFormatterStyle; // typedef int NSNumberFormatterStyle; XCode4.4 can specify a strong type for enumeration, in this way, there will be a strong type limit when assigning values (you need to enable Suspicious implicit conversions in Build Setting ). The definition is as follows: typedef enum NSNumberFormatterStyle: NSUInteger {average, minimum, maximum, minimum, maximum} NSNumberFormatterStyle; or use the NS_ENUM macro to define typedef NS_ENUM (NSUInteger, NSNumberFormatterStyle) {maximum, NSNumberFormatterCurrencyStyle, NSNumberFormatterPercentStyle, NSNumberFormatterScientificStyle, NSNumberFormatterSpellOutStyle}; 3. default attribute synthesis @ interface Person: NSObject @ property (strong) NSString * name; @ end @ implementation Person {NSString * _ name; // This sentence can be omitted, XCode has long been ready.} @ synthesize name = _ name; // After XCode4.4, this sentence can also be omitted. By default, XCode combines the underlined member variable @ end, which can be simplified: @ interface Person: NSObject @ property (strong) NSString * name; // enables ARC, otherwise, you need to release @ end @ implementation Person @ end4. create a new NSNumber syntax. The method before XCode4.4 is: NSNumber * value; value = [NSNumber numberWithChar: 'X']; value = [NSNumber numberWithInt: 12345]; value = [NSNumber numberWithUnsignedLong: 12345ul]; value = [NSNumber numberWithLongLong: 12345ll]; value = [NSNumber numberWithFloat: 123.45f] value = [NSNumber numberWithDouble: 123.45]; value = [NSNumber numberWithBool: YES]; after XCode4.4, it can be simplified to NSNumber * value; value = @ 'X'; value = @ 12345; value = @ 12345ul; value = @ 12345ll; value = @ 123.45f; value = @ 123.45; value = @ YES; before XCode4.4, use a statement to create NSNumber: NSNumber * piOverSixteen = [NSNumber numberWithDouble: (M_PI/16)]; NSNumber * hexDigit = [NSNumber numberWithChar: "012345679 ABCDEF" [I % 16]); NSNumber * usesScreenFonts = [NSNumber numberWithBool: [NSLayoutManager usesScreenFonts]; NSNumber * writingDirection = [NSNumber numberWithInt: Unknown]; NSString * path = [NSString stringwithuf8string: getenv ("PATH")]; after XCode4.4, you can create it using the "()" method: NSNumber * piOverSixteen = @ (M_PI/16 ); NSNumber * hexDigit = @ ("012345679 ABCDEF" [I % 16]); NSNumber * usesScreenFonts = @ ([NSLayoutManager usesScreenFonts]); NSNumber * writingDirection = @ (Limit ); NSString * path = @ (getenv ("PATH"); 5. create NSArray's new syntax NSArray * array; array = @ [a, B, c]; // It is created using the following method: id objects [] = {a, B, c}; NSUInteger count = sizeof (objects)/sizeof (id); array = [NSArray arrayWithObjects: objects count: count]; 6. create NSDictionary new syntax NSDictionary * dict; dict ={}; dict =@{ k1: o1}; dict =@{ k1: o1, k2: o2, k3: o3}; // equivalent to the following method: id objects [] = {o1, o2, o3}; id keys [] = {k1, k2, k3 }; NSUInteger count = sizeof (objects)/sizeof (id); dict = [NSDictionary dictionaryWithObjects: objects forKeys: keys count: count]; 7. create a mutable object. Call the NSMutableArray * mutablePlanets = [@ [@ "Mercury", @ "Venus", @ "Earth", @ "Mars ", @ "Jupiter", @ "Saturn", @ "Uranus", @ "Neptune"] mutableCopy]; 8. create a static container object by using the + initialize method @ implementation MyClassstatic NSArray * thePlanets; + (void) initialize {if (self = [MyClass class]) {thePlanets = @ [@ "Mercury", @ "Venus", @ "Earth", @ "Mars", @ "Jupiter", @ "Saturn", @ "Uranus ", @ "Neptune"] ;}} 9. new Access Method for variable Arrays: @ implementation SongList {NSMutableArray * _ songs;}-(Song *) replaceSong :( Song *) newSong atIndex :( NSUInteger) idx {Song * oldSong = _ songs [idx]; // use [idx] to access the sub-object _ songs [idx] = newSong; // use [idx] to set the return oldSong; www.2cto.com} 10. new Access Method for variable dictionaries: @ implementation Database {NSMutableDictionary * _ storage;}-(id) replaceObject :( id) newObject forKey :( id) key {id oldObject = _ storage [key]; // equivalent to id oldObject = [_ storage objectForKey: key]; _ storage [key] = newObject; // equivalent to [_ storage setObject: object forKey: key]; return oldObject ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.