锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 涓哄尶鍚嶄嬌鐢ㄥ崗璁?/span> 鏈夋椂鍊欙紝涓涓鏋剁殑寮鍙戣呬負(fù)浜嗗悜浣跨敤鑰呴殣钘忎竴涓被錛屽彧鎶婂畠鐨勬帴鍙i氳繃鍗忚鏆撮湶
]]>
浣跨敤NSError錛?/span>
濮旀墭鏂規(guī)硶浼?xì)漶旂敓Error
閫氳繃寮曠敤鍙傛暟浼犻扙rror
//////////////////////////
NSError *anyError;
BOOL sucess = [receivedData writeToURL:someLocalFileURL option:0 error:&anyError];
if(!success){
NSLog(@"Write failed with error: %@", anyError);
//present error to user
}
// 濡傛灉writeToURL鍙戠敓閿欒錛岃繑鍥濶O錛屽茍涓旀洿鏂癮nyError
// 濡傛灉浣犲error涓嶆劅鍏磋叮錛屽彲浠ヤ紶閫扤ULL
浜х敓浣犺嚜宸辯殑error
NSString *desc = NSLocalizedString(@"Unable to,,,", @"");
NSDictionary *userInfo = @{NSLocalizedDescriptionKey : desc};
NSError *error = [NSError errorWithDomain:domain code:-101 userInfo:userInfo];
//////////////////////
- (BOOL) doSomethingThatMayGenerateAnError:(NSError **)errorPtr;
//////////////////
-(BOOL)doSomethingThatMayGenerateAnError:(NSError **)errorPtr{
//,,,
// error occurred
if(errorPtr){
*errorPtr = [NSError errorWithDomain:,,, code:,,, userInfo:,,,];
}
return NO;
}
浣跨敤寮傚父
OBJC鍍忓叾浠栬璦涓鏍鋒敮鎸佸紓甯革紝NSException鍜孨SError涓鏍鋒槸涓涓璞?/span>
// 鍙兘瀵艱嚧寮傚父
}
@catch (NSException *exception){
// 澶勭悊寮傚父
}
@finally{
// 娓呯悊錛屽鐞嗘棤寮傚父鎯呭喌
}
]]>
Block 鏄璦綰у埆鐨勭壒鎬с傚畠鏄竴涓狾BJC鐨勫璞★紝鍙互琚姞鍏ュ鍣ㄥNSArray鎴朜SDictionary銆傚畠鍙互鎹曡幏鎵澶勪綔鐢ㄥ煙鐨勬暟鍊鹼紝闈炲父綾諱技鍏跺畠璇█鐨刢losure鎴栬卨ambda銆?/span>
璇硶
NSLog(@"This is a block");
}
鍙互鍍忓嚱鏁版寚閽堥偅鏍峰0鏄庝竴涓彉閲忔潵鎸佹湁榪欎釜block
simpleBlock = ^{
NSLog(@"This is a block");
}; // 娉ㄦ剰榪欓噷鏈変竴涓垎鍙?/span>
涔熷彲浠ヨ繖鏍峰啓
NSLog(@"This is a block");
};
璋冪敤榪欎釜block
甯︿笂鍙傛暟鍜岃繑鍥炲?/span>
return firstValue*secondValue;
}
// ,,,,,,,,,,,
double (^multiplyTwoValues)(double, double) =
^(double firstValue, double secondValue) { // 榪斿洖鍊肩被鍨嬪彲浠ョ渷鐣?/span>
return firstValue*secondValue;
};
double result = multiplyTwoValues(2,4);
NSLog(@"The result is %f", result);
鎹曡幏Enclosing Scope鍐呯殑鍊?/span>錛屼竴鏃︽崟鑾鳳紝榪欎釜鍊煎氨涓嶄細(xì)鍙樺寲錛屽嵆渚垮悗緇敼鍙樿繖涓?/span>
int anInteger = 42;
void (^testBlock)(void) = ^{
NSLog(@"Integer is: %i", anInteger);
}
anInteger = 84;
testBlock(); // 浠嶇劧杈撳嚭42
}
浣跨敤__block
void (^testBlock)(void) = ^{
NSLog(@"Integer is: %i", anInteger);
};
anInteger = 84:
testBlock(); // output 84;
// ,,,,,,,,,,,,,
_block int anInteger = 42;
void (^testBlock)(void) = ^{
NSLog(@"Integer is: %i", anInteger); // output 42
anInteger = 100;
};
testBlock();
NSLog(@"Value of original variable is now: %i", anInteger); // output 100;
閫氳繃鍙傛暟浼犻払lock錛屼緥濡傚疄鐜頒竴涓洖璋?/span>
[self showProgressIndicator];
XYZWebTask *task = //,,,
[task beginTaskWithCallbackBlock:^{
[self hideProgressIndicator];
}];
}
// beginTaskWithCallbackBlock 鐨勫畾涔夋槸榪欐牱鐨?/span>
-(void)beginTaskWithCallbackBlock:(void)(^)(void))callbackBlock{
//,,,
callbackBlock();
}
鏈浣?jīng)_疄璺墊槸灝哹lock浣滀負(fù)鏈鍚庝竴涓弬鏁幫紝榪欐牱渚夸簬闃呰銆?/span>
涔熷彲浠ヤ嬌鐢╰ypedef綆鍖栬娉?/span>
//,,,,,,,,,,,,,,,
XYZSimpleBlock anotherBlock = ^{ /*,,,*/ };
//,,,,,,,,,,,,
-(void)beginFetchWithCallbackBlock:(XYZSimpleBlock)callbackBlock{
//,,,,
callbackBlock();
}
鍙互灝哹lock浣滀負(fù)灞炴?/span>
@property (copy) void (^blockProperty)(void); // 蹇呴』浣跨敤copy
@end
//,,,,,,,,,,,,,,
self.blockProperty = ^{ /* ,,, */ };
self.blockProperty();
閬垮厤寮哄紩鐢ㄥ驚鐜?/span>
鍦╞lock閲屾崟鑾穝elf錛岃濡傚湪涓涓猚allback block閲岋紝浼?xì)寮曞叆鍐呭瓨绠$悊闂銆俠lock浼?xì)浼?xì)緇存姢涓涓崟鑾峰璞$殑寮哄紩鐢紝鍖呮嫭self
@property (copy) void (^block)(void);
@end
////////////////////////
@implementation XYZBlockKeeper
-(void)configureBlock{
self.block = ^{
[self doSomething]; // 鎹曡幏浜嗕竴涓猻elf鐨勫己寮曠敤
// 寤虹珛浜嗕竴涓己寮曠敤寰幆
}
}
@end
涓婅堪浠g爜浼?xì)漶旂敓涓涓紪璇戣鍛婏紝涓轟簡閬垮厤榪欑鎯呭喌錛屾渶浣?jīng)_疄璺墊槸鎹曡幏涓涓猻elf鐨勫急寮曠敤
XYZBlockKeeper * __weak weakSelf = self;
self.block = ^ {
[weakSelf doSomething];
};
}
block鍙互綆鍖栨灇涓撅紙鐣ワ級
block鍙互綆鍖栧茍鍙戜換鍔★紙鐣ワ級
]]>
]]>
鍗忚瀹氫箟浜嗕氦浜掔殑娑堟伅
- (NSUInteger) numberOfSegments;
- (CGFloat) sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
- (NSString *) titleForSegmentAtIndex:(NSUInteger)segmentIndex;
@end
鏁版嵁婧愪綔涓篤iew鐨勪竴涓睘鎬э紝鍙鏄鍚堝崗璁殑瀵硅薄灝卞彲浠ワ紝鎵浠ョ被鍨嬫槸id銆?/span>
@property (weak) id <XYZPieChartViewDataSource> dataSource;
@end
璁劇疆灞炴т負(fù)涓涓笉絎﹀悎鍗忚鐨勫璞★紝灝嗕細(xì)寮曡搗涓涓紪璇戞椂璀﹀憡銆?/span>
鍙夋柟娉曪紝浣跨敤@optional 鍜?@required
- (NSUInteger) numberOfSegments;
- (CGFloat) sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;
@optional
- (NSString *) titleForSegmentAtIndex:(NSUInteger)segmentIndex;
- (BOOL) shouldExplodeSegmentAtIndex:(NSUInteger)segementIndex;
@required
- (UIColor *) colorForSegmentAtIndex:(NSUInteger)segementIndex;
@end
榪愯鏃舵鏌ュ彲閫夋柟娉?/span>
濡傛灉涓涓柟娉曟槸鍙夌殑錛岄偅涔堝湪璋冪敤鍓嶅簲璇ユ鏌ュ畠鏄惁瀹炵幇銆?/span>
if([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]){
thisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index];
}
respondsToSelector: 鏂規(guī)硶鐢ㄤ簡涓涓猻elector錛孈selector銆?/span>
濡備笂瀹氫箟涓涓鍚堝崗璁殑id綾誨瀷錛岃皟鐢╮espondsToSelector錛屼細(xì)浜х敓涓涓紪璇戞椂閿欒錛岃В鍐蟲柟妗堟槸澹版槑瀵硅薄絎﹀悎NSObject鍗忚
浠庡崗璁戶鎵?/span>
鏈浣?jīng)_疄璺墊槸錛屼綘鐨勫崗璁緷浠嶯SObject鍗忚銆侼SObject瀵硅薄渚濅粠NSObject鍗忚銆?/span>
涓鏃︿綘鐨勫崗璁緷浠嶯SObject鍗忚錛岄偅涔堜緷浠庝綘鍗忚鐨勪換浣曞璞¢兘蹇呴』瀹炵幇NSObject鍗忚鐨勬柟娉曪紝浣嗗洜涓哄畠浠簲璇ユ槸NSObject鐨勫瓙綾伙紝浣犲氨涓嶅繀鑷繁瀹炵幇榪欎簺NSObject鐨勬柟娉曘備緷浠嶯SObject鍗忚闈炲父鏈夌敤銆?/span>
@end
comform 涓涓崗璁?/span>
@end
涓鏃﹀0鏄庝緷浠庢煇涓崗璁紝灝卞繀欏誨疄鐜版墍鏈夌殑required鏂規(guī)硶錛屽拰闇瑕佺殑optional鏂規(guī)硶錛屽惁鍒欑紪璇戝櫒浼?xì)缁欏國櫗﹀憡銆傛柟娉曠殑絳懼悕蹇呴』鐩稿悓銆?/span>
Cocoa 鍜?Cocoa Touch 瀹氫箟浜嗗ぇ閲忕殑protocol
渚嬪NSFetcheResultsController
id <NSFetchedResultSectionInfo> sectionInfo =
[self.fetchedResultsController.sections objectAtIndex:sectionNumber];
NSInteger numberOfRowsInSection = [sectionInfo numberOfObjects];
]]>
閫氳繃Category涓虹被澧炲姞鏂規(guī)硶
@interface XYZPerson (XYZPersonNameDisplayAddtions)
- (NSString *)lastNameFirstNameString;
@end
// ----------------------
#import "XYZPerson+XYZPersonNameDisplayAddtions.h"
@implementation XYZPerson (XYZPersonNameDisplayAddtions)
- (NSString *)lastNameFirstNameString{
return [NSString stringWithFormat:@"%@ %@", self.lastName, self.firstName];
}
@end
//-------------------------------
#import "XYZPerson+XYZPersonNameDisplayAddtions.h"
@implementation SomeObject
-(void) someMethod{
XYZPerson *person = [[XYZPerson alloc] initWithFirstName:@"John", lastName:@"Doe"];
NSLog(@"The people is %@", [person lastNameFirstNameString]);
}
@end
Category 鍙互澧炲姞浠諱綍鐨勫疄渚嬫柟娉曞拰綾繪柟娉曪紝浣嗘槸閫氬父涓嶉傚悎澧炲姞Property錛岃櫧鐒惰娉曚笂鍙互澹版槑涓涓狿roperty錛屼絾涓嶈兘閫氳繃Category澧炲姞涓涓疄渚嬪彉閲忋傝繖鎰忓懗鐫涓嶈兘synthesize浠諱綍瀹炰緥鍙橀噺錛?涔熸病鏈夊瓨鍙栨柟娉曘?浣犲彲浠ュ啓鑷繁鐨刟ccessor錛屼絾鏄笉鑳絢eep track property錛屽畠浠瓨鍌ㄥ湪鍘熷鐨勭被閲屻?/span>
閬垮厤鍚嶅瓧鍐茬獊
Category 鐨勬柊澧炴柟娉曞彲鑳戒細(xì)瀵艱嚧鍚嶅瓧鍐茬獊錛屽湪榪愯鏃跺彲鑳戒細(xì)鍑虹幇鏈煡鐨勮涓猴紝涓轟簡閬垮厤榪欑鎯呭喌錛岄渶瑕佸鍔犲墠緙
+ (id)xyz_sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending;
@end
// ------ use it -------
NSSortDescriptor *descriptor = [NSSortDescriptor xyz_sortDescriptorWithKey:@"name" ascending:YES];
綾繪墿灞曠被浼煎垎綾伙紝浣嗗彧鑳界敤浜庣紪璇戞椂鏈夋簮鐮佹儏鍐碉紝騫朵笖蹇呴』鍐欏湪瀹炵幇鏂囦歡閲岋紝鍥犳涓嶈兘閫氳繃榪欎釜涓烘鏋剁被鎵╁睍銆傛墿灞曡娉曠被浼煎垎綾?/span>
{
id _someCustomInstanceVariable;
}
@property NSObject *extraProperty;
@end
鍙互鐢ㄦ墿灞曟潵澹版槑縐佹湁灞炴у拰鏂規(guī)硶
@proerty (readonly) NSString *uniqueIdentifier;
-(void)assignUniqueIdentifier;
@end
/// ---------------------
@interface XYZPerson ()
@property (readwrite) NSString *uniqueIdentifier;
@end
@implementation XYZPerson
// ,,,
@end
浠諱綍璁塊棶縐佹湁鏂規(guī)硶錛屾垨鑰卻et涓涓猺eadonly灞炴х殑琛屼負(fù)錛岀紪璇戝櫒閮戒細(xì)浜х敓閿欒錛屼絾鏄彲浠ラ氳繃鍔ㄦ佽繍琛屾椂鐗規(guī)ч伩鍏嶇紪璇戝櫒閿欒錛岃濡傝皟鐢∟SObject鐨刾erformSelector鏂規(guī)硶銆?/span>
濡傛灉浣犳墦綆楁瀯寤虹鏈夋柟娉曟垨灞炴э紝浣犲彲浠ュ0鏄庝竴涓垎紱葷殑澶存枃浠舵潵澹版槑鎵╁睍錛岃濡俋YZPerson.h鍜孹YZPersonPrivate.h
鍒嗙被鍜屾墿灞曞茍涓嶆槸瀹氬埗涓涓被鐨勫敮涓閫斿緞錛屼篃涓嶆槸鏈濂芥柟娉曪紝瑕佽冭檻鍙惁浣跨敤瀛愮被鍖栨垨鑰呭鎵樼殑鏂規(guī)硶鏉ュ疄鐜般?/span>
]]>
@property NSString *firstName;
@property NSString *lastName;
@end
/// ============
NSString *firstName = [somePerson firstName];
[somePerson setFirstName:@"Johnny"];
闄愬畾灞炴т負(fù)鍙錛屼篃鍙檺瀹氫負(fù)readwrite錛屼絾榪欎笉蹇咃紝鍥犱負(fù)緙虹渷濡傛槸銆?/span>
鍙互鎸囧畾灞炴х殑璁塊棶鍣ㄥ悕縐幫紝澶氫釜闄愬畾璇嶅涓嬫牸寮?/span>
浣跨敤鐐硅娉?/span>
// NSString *firstName = [somePerson firstName];
somePerson.firstName = @"Johnny";
// [somePerson setFirstName:@"Johnny"];
澶у鏁板睘鎬ф湁涓涓疄渚嬪彉閲忋?/span>
緙虹渷鐨勮鍐欏睘鎬т細(xì)鐢辯紪璇戝櫒鑷姩鐢熸垚涓涓疄渚嬪彉閲忥紝浠ヤ笅鍒掔嚎寮濮嬶紝濡俖firstName;
NSString *myString = @"An interesting string";
_someString = myString;
// self.someString = myString;
// or
// [self setSomeString:myString];
}
鍙互鎸囧畾瀹炰緥鍙橀噺鐨勫悕瀛?/span>
@synthesize propertyName = instanceVariableName;
@end
// ---- for example
@synthesize firstName = ivar_firstName;
濡傛灉浣犱笉鎸囧畾鍚嶅瓧錛屽疄渚嬪彉閲忓垯鍜屽睘鎬у悓鍚嶏紝鍓嶉潰娌℃湁涓嬪垝綰?/span>
濡傛灉浣犲茍涓嶆兂鎻愪緵鏁板肩粰鍏跺畠瀵硅薄錛屼綘涓嶅繀澹版槑涓涓睘鎬ц屼嬌鐢ㄤ竴涓疄渚嬪彉閲?/span>
NSString *_myNonPropertyInstanceVariable;
}
@end
@implementation SomeClass{
NSString *_anotherCustomInstanceVariable;
}
鍦ㄥ垵濮嬪寲鏂規(guī)硶閲岃闂疄渚嬪彉閲?/span>
Setter鏂規(guī)硶浼?xì)鏈夐檮鍔犳晥鏋溿傚畠浠彲鑳借Е鍙慘VC閫氱煡錛屾垨鑰呭畬鎴愪綘瀹氬埗鐨勬柟娉曘?/span>
浣犲簲璇ュ湪鍒濆鍖栨柟娉曢噷鐩存帴璁塊棶瀹炰緥鍙橀噺錛屽洜涓哄璞¤繕娌℃湁鍒濆鍖栧畬鎴愩傜敋鑷充綘涓嶅簲璇ユ彁渚涘畾鍒剁殑璁塊棶鍣ㄦ柟娉曠粰浣犵殑綾繪彁渚涢檮鍔犳晥鏋溿傝繖鏍峰皢鏉ョ殑瀛愮被鍙互寰堝ソ鐨刼verride榪欎釜琛屼負(fù)銆?/span>
涓涓吀鍨嬬殑init鏂規(guī)硶濡備笅
self = [super init];
if(self){
// initialize instance variables here
}
return self;
}
鍙互鎸囧畾鍒濆鍖栨柟娉?/span>
self = [super init];
if(self){
_firstName = aFirstName;
_lastName = aLastName;
}
return self;
}
鍙互鎸囧畾璁塊棶鏂規(guī)硶
// -------------
-(NSString *)fullName{
return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}
濡傛灉浣犻渶瑕佸湪璁塊棶鍣ㄩ噷璁塊棶瀹炰緥鍙橀噺錛岄偅搴旇鐩存帴璁塊棶銆備緥瀛愰噷寤惰繜鍒濆鍖栦竴涓璞★紝lazy accessor銆?/span>
if(!_someImportantObject){
_someImportantObject = [[XYZObject alloc] init];
}
return _someImportantObject;
}
緙栬瘧鍣ㄤ細(xì)鑷姩synthesize涓涓疄渚嬪彉閲忋傝嚦灝戜竴涓闂柟娉曘傚鏋滀綘涓簉eadwrite灞炴у疄鐜頒簡getter鍜宻etter錛屾垨鑰呬負(fù)readonly瀹炵幇浜唃etter銆傜紪璇戝櫒璁や負(fù)浣犳兂鎺у埗灞炴у疄鐜幫紝涔熶笉浼?xì)鍐嶄皋Z綘鑷姩鐢熸垚涓涓疄渚嬪彉閲忋傚洜姝わ紝濡傛灉浣犱粛鐒墮渶瑕佷竴涓疄渚嬪彉閲忥紝浣犻渶瑕佹墜鍔╯ynthesize
灞炴х己鐪佹槸鍘熷瓙鎬х殑銆俛tomic
@property NSObject *implicitAtomObject; // 緙虹渷鏄痑tomic
@property (atomic) NSObject *explicitAtomicObject; // 鎸囨槑atomic
@end
濡傛灉浣犲畾鍒朵簡涓涓猘tomic, readwrite鐨勫睘鎬х殑setter錛岃岃緙栬瘧鍣ㄨ嚜鍔ㄧ敓鎴恎etter錛屽皢浼?xì)寰楀堫C竴涓紪璇戞椂璀﹀憡銆?/span>
浣犲彲浠ュ0鏄巒onatomic灞炴э紝鍥犱負(fù)涓嶉渶瑕乬uarantee錛屽鐞嗗茍鍙戯紝鍥犳瀹冪殑璁塊棶鍣ㄦ瘮atomic灞炴ф洿蹇?/span>
灞炴х殑鍘熷瓙鎬у茍涓嶆剰鍛崇潃瀵硅薄鏄嚎紼嬪畨鍏ㄧ殑銆備緥濡俧irstName鍜孡astName銆?/span>
綆$悊瀵硅薄鐨勭敓鍛藉懆鏈燂紝瀵硅薄鏄氳繃鎸囬拡鏉ヨ闂紝鍐呭瓨鏄姩鎬佺敵璇風(fēng)殑錛屾寚閽堝彉閲忕殑鐢熷懡鍛ㄦ湡涓嶄唬琛ㄥ璞$殑璇佹槑鍛ㄦ湡銆俿trong reference鎰忓懗鐫瀵硅薄鍜屽彟涓涓璞$殑鐢熷懡鍛ㄦ湡涓鏍烽暱銆?/span>
灞炴х己鐪佹槸寮哄紩鐢紝鍙互鎸囧畾weak銆傛湰鍦板彉閲忛兘鏄己寮曠敤錛屽鏋滀綘涓嶅笇鏈涚淮鎶や竴涓己寮曠敤錛屽彲浠ヤ嬌鐢╛_weak
// ---------
NSObject * __weak weakVariable;
寮卞紩鐢ㄤ細(xì)甯︽潵涓嶅畨鍏ㄧ殑琛屼負(fù)錛屽洜涓哄彉閲忓彲鑳戒細(xì)琚疆涓簄il銆?/span>
涓浜汣ocoa綾諱笉鑳藉0鏄庝負(fù)寮卞紩鐢紝鍖呮嫭NSTextView, NSFont, NSColorSpace絳夛紝濡傛灉浣犻渶瑕佷嬌鐢ㄨ繖浜涚被鐨勪竴涓急寮曠敤錛屼綘闇瑕佷竴涓猽nsafe_unretained澹版槑銆?/span>
// ------------
NSObject * __unsafe_unretained unsafeReference;
copy灞炴?/span>
@property NSString *firstName;
@peoperty NSString *lastName;
@end
濡傛灉浣犺繖鏍峰仛
self.badgeView.firstName = nameString;
// ----
[nameString appendString:@"ny"];
@property (copy) NSString *firstName;
@property (copy) NSString *lastName;
@end
// --------------------
NSMutableString *nameString = [NSMutableString stringWithString:@"John"];
self.badgeView.firstName = nameString;
// ----
[nameString appendString:@"ny"];
涓涓澹版槑涓篶opy鐨勫璞?/span>蹇呴』鏀寔NSCopying鍗忚銆傚鏋滀綘瑕佺洿鎺et涓涓猚opy灞炴х殑瀹炰緥鍙橀噺錛屼緥濡傚湪鍒濆鍖栨柟娉曢噷錛屼竴瀹氳璁劇疆鍘熷瀵硅薄鐨刢opy
self = [super init];
if(self){
_instanceVariableForCopyProperty = [aString copy];
}
return self;
}
]]>
鍙戦佸拰鎺ユ敹娑堟伅
-(void) sayHello;
@end
// implemetation
@implementation XYZPerson
- (void) sayHello{
NSLog(@"Hello, world!");
}
@end // XYZPerson
// -----
[somePerson sayHello];
閫氳繃鎸囬拡keep瀵硅薄
NSString *myString = // get a string from somewhere.
}
閫氳繃鍙傛暟浼犻掑璞?/span>
// implementation
-(void)saySomething:(NSString *)greeting{
NSLog(@"%@", greeting); // "%@",鐢ㄦ潵鎵撳嵃瀵硅薄
}
閫氳繃榪斿洖鍊間紶閫?/span>
//implementation
-(NSString *)magicString{
NSString *stringToReturn = // create string
return stringToReturn;
}
// use it
NSString *magic = [testString magicString];
鍚戣嚜宸卞彂閫佹秷鎭?/span>
-(void)sayHello{
[self saySomething:@"Hello, world!"];
}
-(void)saySomething:(NSString *)greeting{
NSLog(@"%@", greeting);
}
@end
鍚戠埗綾誨彂娑堟伅
@end
/////////////////////
@implementation XYZShoutingPerson
-(void)saySomething:(NSString *)greeting{
NSString *uppercaseGreeting = [greeting uppercaseString];
[super saySomething:uppercaseGreeting];
}
@end
鍔ㄦ佸垱寤哄璞?/span>
+(id)alloc;
//
-(id)init;
//=============== use it
NSObject *newObject = [[NSObject alloc] init];
NSObjet *someObject = [NSObject alloc];
[someObject init];
鍒濆鍖栨柟娉曞彲浠ュ甫鍙傛暟
-(id)initWithLong:(long)value;
//------------------
NSNumber *magicNumber = [[NSNumber alloc] initWithInt:42];
綾誨伐鍘傛柟娉曟彁渚涗簡鍙︿竴涓夋嫨
+(NSNumber *)numberWithLong:(long)value;
//------------------
NSNumber *magicNumber = [NSNumber numberWithInt:42];
浣跨敤new鏉ヤ唬鏇?/span>
// is effectively the same as:
XYZObject *object = [[XYZObject alloc] init];
閫氳繃瀛楅潰閲忓垱寤?/span>
// is same as
NSString *someString = [NSString stringWithCString:"Hello, world!" encoding:NSUTF8StringEncoding];
//=======
NSNumber *myBOOL = @YES;
NSNumber *myFloat = @3.14f;
NSNumber *myInt = @42;
NSNumber *myLong = @42L;
NSNumber *myInt2 = @(84 / 2);
OBJC 鏄竴涓姩鎬佽璦
id someObject = @"Hello, World!";
[someObject removeAllObjects];
// 涓嬮潰浠g爜浼?xì)漶旂敓缂栬瘧鏃堕敊璇?/span>
NSString *someObject = @"Hello, World!";
[someObject removeAllObjects];
姣旇緝瀵硅薄
if(firstPerson == secondPerson){
// the same object
}
// 濡傛灉瑕佹瘮杈冩暟鎹槸鍚︾浉鍚岋紝浣跨敤isEqual
if([firstPerson isEqual:secondPerson]) {
// is identical to second
}
浣跨敤nil
XYZPerson *somePerson;
// ------------
if(somePerson != nil){
// ====
}
// or
if(somePerson){
// ===
}
]]>
OBJC鏄疧SX鍜孖OS鐨勪富瑕佺紪紼嬭璦錛屽畠鏄疌鐨勮秴闆嗭紝鎻愪緵浜嗛潰鍚戝璞$殑鐗規(guī)у拰鍔ㄦ佽繍琛屾椂綾誨瀷淇℃伅銆侽BJC緇ф壙浜咰鐨勮娉曪紝鍩烘湰鏁版嵁綾誨瀷鍜屾祦紼嬫帶鍒訛紝闄勫姞浜嗗畾涔夌被鍜屾柟娉曠殑璇硶銆備篃涓哄姩鎬佺被鍨嬬粦瀹氭彁渚涗簡璇█綰у埆鐨勬敮鎸併?/span>
鍙彉鎬у喅瀹氬兼槸鍚﹀彲浠ユ洿鏀?br />涓浜涚被瀹氫箟瀵硅薄鏄痠mmutable鐨勶紝鎰忓懗鐫瀵硅薄鐨勫唴瀹逛笉鍙鍏跺畠瀵硅薄鏀瑰彉銆侼SString鍜孨SNumber鏄痠mmutable鐨?/span>
涓浜沬mmutable綾婚〉娓竚utable鐗堟湰銆傛瘮濡侼SString鐨凬SMutableString銆?/span>
灝界NSString鍜孨SMutableString鏄笉鍚岀殑綾伙紝瀹冧滑鏈夐潪甯稿鐨勭浉浼間箣澶?/span>
浠庡彟涓涓被緇ф壙浠庡彟涓涓被緇ф壙錛屽瓙綾葷戶鎵夸簡鐖剁被鎵鏈夌殑琛屼負(fù)鍜屽睘鎬с備篃鍙互瀹氫箟鑷繁鐨刡ehavior鍜宲roperties錛屾垨鑰卭verride鐖剁被鐨刡ehavior
NSMutableString緇ф壙浜嶯SString錛屽洜姝ゆ嫢鏈夋墍鏈塏SString鐨勫姛鑳斤紝涔熷鍔犱簡append錛宨nsert錛宺eplace錛宒elete substring絳夋柟娉?/span>
鏍圭被鎻愪緵鍩烘湰鍔熻兘濡傛灉浣犲畾涔変竴涓嚜宸辯殑綾伙紝搴旇鑷沖皯緇ф壙浜嶯SObject
綾葷殑鎺ュ彛瀹氫箟
鍩烘湰璇硶
2
3 @end
Properties鎺у埗璁塊棶涓涓璞$殑鍊?/span>
@property NSString *firstName; // 瀵硅薄鐢ㄦ寚閽?/span>
@property NSString *lastName;
@property NSNumber *yearOfBirth;
@property int yearOfBirth_1; // 鐢ㄥ熀鏈被鍨?/span>
@end
Property灞炴ф寚鏄庢暟鎹殑鍙闂у拰瀛樺偍鎯呭喌
@property (readonly) NSString* firstName;
@property (readonly) NSString* lastName;
@end
鏂規(guī)硶瀹氫箟
鏂規(guī)硶鍙互甯﹀弬鏁?/span>
鍙互鏈夊涓弬鏁?/span>
鍥犳錛屼笅闈㈢殑鍑芥暟絳懼悕涓嶅悓錛?/span>
-(void)someMethodWithFirstValue:(SomeType)info1 secondValue:(YetAnotherType)onfo2;
綾誨悕縐板繀欏誨敮涓綾誨悕蹇呴』鍞竴錛岀敋鑷沖拰搴撴垨鑰呮鏋墮噷鐨勭被涔熶笉鑳介噸鍚嶏紝寤鴻浣跨敤涓変釜瀛楃鐨勫墠緙銆?br />涓や釜瀛楁瘝鍓嶇紑錛屽NS錛孶I錛屽凡緇忚Apple淇濈暀
綾葷殑瀹炵幇
鍩烘湰璇硶
@implementation XYZPerson
@end
瀹炵幇鏂規(guī)硶
@interface XYZPerson : NSObject
- (void)sayHello;
@end
// implementation like this
@implementation XYZPerson
- (void)sayHello{
NSLog(@"Hello, World!");
}
@end
綾諱篃鏄竴涓璞?/span>
鍦∣BJC閲岋紝綾昏嚜宸變篃鏄竴涓狢lass綾誨瀷鐨勫璞°傜被綾誨瀷涓嶈兘閫氳繃澹版槑鐨勮娉曞畾涔塸roperty錛屼絾鏄畠鍙互鎺ユ敹娑堟伅銆?/span>
綾葷被鍨嬬殑鏂規(guī)硶鐨勫吀鍨嬬敤閫旀槸宸ュ巶鏂規(guī)硶錛岀敤鏉ヨ繘琛屽璞$殑鍒嗛厤鍜屽垵濮嬪寲錛屽NSString鐨?/span>宸ュ巶鏂規(guī)硶
+(id)stringWithString:(NSString *)aString;
+(id)stringWithFormat:(NSString *)format,.. . ;
+(id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)end error:(NSError **)error;
+(id)stringWithCString:(const char*)cString encoding:(NSStringEncoding)enc;
+ 鍙瘋〃紺鴻繖鏄竴涓被鐨勬柟娉?/span>
]]>
2 0x0000~0x007f 0xxxxxxx 0x00~0x7f
3 0x0080~0x07ff 110xxxxx 10xxxxxx 0xCx 0x8x ~ 0xDx 0xBx
4 0x0800~0xffff 1110xxxx 10xxxxxx 10xxxxxx 0xEx 0x8x 0x8x ~ 0xEx Bx Bx
]]>