2013年1月20日 星期日

Object Literals - Objective-C 新的寫法

現在 Objective-C 也提供了一些新的簡化寫法,看起來似乎可以少打幾個字,程式碼也比較簡潔,但總是要花時間習慣一下。這裡整理了2012年 Apple 在 WWDC 提供的一些例子,如果有興趣,可以去官方網站下載觀看!

NSNumber Creation

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];

簡化後

NSNumber *value;
value = @'X';
value = @12345;
value = @12345ul;
value = @12345ll;
value = @123.45f;
value = @123.45;
value = @YES; 




Expression Literals



NSNumber *piOverSixteen = [NSNumber numberWithDouble: ( M_PI / 16 )];
NSNumber *hexDigit = [NSNumber numberWithChar: "012345679ABCDEF"[i % 16]);
NSNumber *usesScreenFonts = [NSNumber numberWithBool:
                              [NSLayoutManager usesScreenFonts]];
NSNumber *writingDirection = [NSNumber numberWithInt:
                                NSWritingDirectionLeftToRight];
NSString *path = [NSString stringWithUTF8String: getenv("PATH")];


簡化後


NSNumber *piOverSixteen = @( M_PI / 16 );
NSNumber *hexDigit = @( "012345679ABCDEF"[i % 16] );
NSNumber *usesScreenFonts = @( [NSLayoutManager usesScreenFonts] );
NSNumber *writingDirection = @( NSWritingDirectionLeftToRight );
NSString *path = @( getenv("PATH") );


Array Creation

NSArray *array;
array = [NSArray array];
array = [NSArray arrayWithObject:a];
array = [NSArray arrayWithObjects:a, b, c, nil];
id objects[] = { a, b, c };
NSUInteger count = sizeof(objects) / sizeof(id);
array = [NSArray arrayWithObjects:objects count:count];

簡化後

NSArray *array;
array = @[];
array = @[ a ];
array = @[ a, b, c ];
array = @[ a, b, c ];



Dictionary Creation

NSDictionary *dict;
dict = [NSDictionary dictionary];
dict = [NSDictionary dictionaryWithObject:o1 forKey:k1];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
                       o1, k1, o2, k2, o3, k3, nil];
id objects[] = { o1, o2, o3 };
id keys[] = { k1, k2, k3 };
NSUInteger count = sizeof(objects) / sizeof(id);
dict = [NSDictionary dictionaryWithObjects:objects
                                   forKeys:keys
                                     count:count];

簡化後            

NSDictionary *dict;
dict = @{};
dict = @{ k1 : o1 };
dict = @{ k1 : o1, k2 : o2, k3 : o3 };
dict = @{ k1 : o1, k2 : o2, k3 : o3 };



Array Subscripting

@implementation SongList {
  NSMutableArray *_songs;
}
- (Song *)replaceSong:(Song *)newSong atIndex:(NSUInteger)idx {
    Song *oldSong = [_songs objectAtIndex:idx];
    [_songs replaceObjectAtIndex:idx withObject:newSong];
    return oldSong;
}

簡化後

@implementation SongList {
  NSMutableArray *_songs;
}
- (Song *)replaceSong:(Song *)newSong atIndex:(NSUInteger)idx {
    Song *oldSong = _songs[idx];
    _songs[idx] = newSong;
    return oldSong;
}


Dictionary Subscripting

@implementation Database {
  NSMutableDictionary *_storage;
}
- (id)replaceObject:(id)object forKey:(id <NSCopying>)key {
    id oldObject = [_storage objectForKey:key];
    [_storage setObject:object forKey:key];
    return oldObject;
}

@implementation Database {
  NSMutableDictionary *_storage;
}
- (id)replaceObject:(id)newObject forKey:(id <NSCopying>)key {
    id oldObject = _storage[key];
    _storage[key] = newObject;
    return oldObject;
}


Keyed Subscripting

- (id)replaceObject:(id)newObject forKey:(id <NSCopying>)key {
    id oldObject = _storage[key];
    _storage[key] = newObject;
    return oldObject;
}

- (id)replaceObject:(id)newObject forKey:(id <NSCopying>)key {
    id oldObject = [_storage objectForKeyedSubscript:key];
    [_storage setObject:newObject forKeyedSubscript:key];
    return oldObject;
}

還提供了新的 Subscripting Method


Subscripting Methods

• Indexed subscripting methods

- (elementType)objectAtIndexedSubscript:(indexType)idx; - (void)setObject:(elementType)object
atIndexedSubscript:(indexType)idx; 

• Keyed subscripting methods

- (elementType)objectForKeyedSubscript:(keyType)key; - (void)setObject:(elementType)object
forKeyedSubscript:(keyType)key; 

它的限制要注意!

•indexType must be integral
•elementType and keyType must be an object pointer

現在你的 Class 也可以有 Subscript 的能力

 class MyGroovySongList : NSObject
 - (Song *)objectAtIndexedSubscript:(NSUInteger)idx;
 - (void)setObject:(Song *)song atIndexedSubscript:(NSUInteger)idx;
 @end

 @implementation MyGroovySongList {
   NSMutableArray *mySongs;
 }

 - (Song *)objectAtIndexedSubscript:(NSUInteger)idx {
   return (Song *)mySongs[idx];
 }

 - (void)setObject:(Song *)song atIndexedSubscript:(NSUInteger)idx {
   mySongs[idx] = song;
 }





沒有留言:

張貼留言