1. 自動補上@synthesize
只要打@property,@synthesize會自動幫我們補上。
例子:
TestViewController.h
@interface TestViewController : UIViewController
@property (strong, nonatomic) NSString *name;
@end
TestViewController.m
@implementation TestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_name = @"彼得潘";
self.name = @"彼得潘";
}
@end
說明:
自動補上的@synthesize,其實長得像這樣
@synthesize name = _name;
因此若要直接存取物件的member,於前面加 _ 即為member名稱。
自動補上的@synthesize,其實長得像這樣
@synthesize name = _name;
因此若要直接存取物件的member,於前面加 _ 即為member名稱。
2. 神奇的@
Objective-C的物件讓人又愛又恨。因為物件,讓我們可以輕易設計許多天馬行空的App。但也因為物件,讓我們在使用某些常用型別,比方int, Array, Dictionary時,需要寫許多令人心煩的程式碼。現在有了神奇的@,一切都簡單多了。
例子:
(1) NSNumber
a. 一拍即合型
沒有@的世界末日
NSNumber * endOfTheWorld = [NSNumber numberWithInt: 2012];
神奇@的世界末日
NSNumber *endOfTheWorld = @2012;
說明:
如果對象是數字,它和@完全是一拍即合,可以直接結合。
b. 月下老人 ( ) 牽線型
如果不是數字,還是可以結合,只不過需要 ( ) 的幫忙。
-(int)getEndOfTheWorld
{
return 2012;
}
沒有@的世界末日
NSNumber * endOfTheWorld = [NSNumber numberWithInt: [self getEndOfTheWorld]];
神奇@的世界末日
NSNumber *endOfTheWorld = @([self getEndOfTheWorld]);
(2) NSArray
結合@ 和 [ ] ,我們可以輕易創造NSArray。
結合@ 和 [ ] ,我們可以輕易創造NSArray。
沒有@的蝙蝠俠三部曲
NSArray *batmanMovies = [NSArray arrayWithObjects:@"開戰時刻", @"黑暗騎士", @"黎明升起", nil];
神奇@的蝙蝠俠三部曲
NSArray *batmanMovies = @[@"開戰時刻", @"黑暗騎士", @"黎明升起"];
不過如果蝙蝠俠想再拍第四集,要先變型為NSMutableArray才能加入第四集,例如以下例子:
NSMutableArray *batmanMovies = [@[@"開戰時刻", @"黑暗騎士", @"黎明升起"] mutableCopy];
[batmanMovies addObject:@"當蝙蝠俠遇上彼得潘"];
(3) NSDictionary
結合@ 和 { },我們可以輕易創造NSDictionary。
沒有@的蔡淳佳新專輯主打歌
NSDictionary *joiNewAlbum = [NSDictionary dictionaryWithObjectsAndKeys:@"Love You", @"song1", @"不透光", @"song2", nil];
神奇@的蔡淳佳新專輯主打歌
NSDictionary *joiNewAlbum = @{ @"song1" : @"Love You", @"song2": @"不透光" };沒有@的蔡淳佳新專輯主打歌
NSDictionary *joiNewAlbum = [NSDictionary dictionaryWithObjectsAndKeys:@"Love You", @"song1", @"不透光", @"song2", nil];
神奇@的蔡淳佳新專輯主打歌
3. 更方便的存取集合裡的元素 (Object Subscripting)
NSMutableArray *batmanMovies = [@[@"開戰時刻", @"黑暗騎士", @"黎明升起"]
mutableCopy];
[batmanMovies addObject:@"當蝙蝠俠遇上彼得潘"];
沒有subscripting的蝙蝠俠第4集
movieLabel.text = [batmanMovies objectAtIndex:3];
支援subscripting的蝙蝠俠第4集
movieLabel.text = batmanMovies[3];
說明:
在 [ ] 內指定index
說明:
在 [ ] 內指定index
NSDictionary *joiNewAlbum = @{ @"song1" : @"Love You", @"song2": @"不透光" };
沒有subscripting的蔡淳佳抒情主打歌
songLabel.text = [joiNewAlbum objectForKey:@"song2"];
支援subscripting的蔡淳佳抒情主打歌
songLabel.text = joiNewAlbum[@"song2"];
說明:
在[ ]內指定key
執行結果:
想要具有Object Subscripting的神奇魔力,其實很簡單,想要像NSArray一樣透過index存取,只要定義以下2兩個method:
- (id)objectAtIndexedSubscript:(NSUInteger);
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
想要像NSDictionary一樣透過key存取,只要定義以下2兩個method:
- (id)objectForKeyedSubscript:(id)key;
- (void)setObject:(id)object forKeyedSubscript:(id)key;
有一點值得注意的,NSArray和NSDictionary要等到iOS 6 SDK才定義這2個method,因此目前尚未支援iOS 6的Xcode 4.4是無法以[ ] 存取NSArray和NSDictionary裡的元件,只有Xcode 4.5可以。
接下來彼得潘就以Peter Pan的自我介紹為例,說明如何讓自訂類別具有Object Subscripting的神奇魔力。
接下來彼得潘就以Peter Pan的自我介紹為例,說明如何讓自訂類別具有Object Subscripting的神奇魔力。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface PeterPan : NSObject | |
{ | |
NSString *lover; | |
NSString *enemy; | |
} | |
- (id)objectForKeyedSubscript:(id)key; | |
- (void)setObject:(id)object forKeyedSubscript:(id)key; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation PeterPan | |
- (id)objectForKeyedSubscript:(id)key | |
{ | |
if([key isEqualToString:@"最愛"]) | |
{ | |
return lover; | |
} | |
else if([key isEqualToString:@"死對頭"]) | |
{ | |
return enemy; | |
} | |
else | |
{ | |
return nil; | |
} | |
} | |
- (void)setObject:(id)object forKeyedSubscript:(id)key | |
{ | |
if([key isEqualToString:@"最愛"]) | |
{ | |
[self setValue:object forKey:@"lover"]; | |
} | |
else if([key isEqualToString:@"死對頭"]) | |
{ | |
[self setValue:object forKey:@"enemy"]; | |
} | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PeterPan *peterPan = [[PeterPan alloc] init]; | |
peterPan[@"最愛"] = @"温蒂"; | |
peterPan[@"死對頭"] = @"虎克船長"; | |
self.loverLabel.text = peterPan[@"最愛"]; | |
self.enemyLabel.text = peterPan[@"死對頭"]; |
沒有留言:
張貼留言