IOS之學習筆記三 簡單物件和static和單例

2021-08-21 04:23:58 字數 1801 閱讀 6020

1、person.h

#import @inte***ce person : nsobject

-(void)setname:(nsstring *) name andage:(int) age;

-(void)say:(nsstring *)content;

-(nsstring *)info;

+(void)foo;

@end

person.m

#import "person.h"

@implementation persion

-(void)say:(nsstring *)content

-(nsstring *)info

+(void)foo

-(void)test

@end

-(void)setname:(nsstring *) _name andage:(int) _age

person *person = [[person alloc] init];

[person say:@"hello"];

[person setname:@"chenyu" andage:26];

nsstring *info = [person info];

nslog(@"info is %@", info);

[persion foo];

2、id型別可以代表所有物件的型別,可以任何類的物件賦值給id型別變數

id p = [[person alloc] init];

[p say:@"hello"];

3、oc沒有類變數,但是可以通過內部全域性變數來模擬類變數

oc也提供了static關鍵字,但是static不能用於修飾成員變數,只能修飾區域性變數,全域性變數和函式,static修飾區域性變數表示將該區域性變數儲存在靜態儲存區,static修飾全域性變數用於限制全域性變數只能在當前原始檔中訪問,static修飾函式用於限制函式只能在當前檔案中呼叫

模擬類變數

user.h檔案如下

#import @inte***ce user : nsobject

+(nsstring *)nation;

+(void)setnation:(nsstring *)newnation;

@end

user.m檔案如下

#import "user.h"

@implement user

static nsstring *nation = nil;

+(nsstring *)nation

+(void)setnation:(nsstring *)newnation

@end

int main(int argc, char* argc)

}

4、單例模式

singleton.h檔案如下

#import @inte***ce singleton : nsobject

+(id)instance;

@end

singleton.m檔案如下

@implemnet singleton

static id instance = nil;

+(id)instance

return instance;

}@end

int main(int argc, char* argc)

}

IOS之學習筆記一

2 autoreleasepool autoreleasepool自動釋放池,在裡面的 會自動釋放記憶體,不會記憶體洩漏 3 objective c 2.0引入arc 自動引用計數 機制和自動釋放池,降低記憶體管理難度 4 nslog 的ns是乙個字首,cocoa對其所有的函式 常量 型別前面都會增...

ios學習筆記(三)簡單動畫

先建立乙個uilabel uilabel label uilabel alloc initwithframe cgrectmake 0,0,50,50 label.backgroundcolor uicolor bluecolor self.window addsubview label uivie...

IOS之學習筆記六 可變形參

1 va list 這是乙個可變型別,用於定義指向可變引數列表的指標變數 2 va start 讓arglist指向乙個可變引數列表的第乙個引數 3 提取arglist當前指標指向的引數,並且將指標指向下乙個引數 4 釋放arglist指標,結束提取 args.h ifndef args h def...