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
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;
}
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;
}
沒有留言:
張貼留言