日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Objective-C 日记⑤ 内存管理、协议、Category 视频笔记

發布時間:2023/12/18 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Objective-C 日记⑤ 内存管理、协议、Category 视频笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//內存管理第六課 #import <Foundation/Foundation.h> #import "Person.h" #import "Dog.h"int main(int argc,const char *argv) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];Person *xiaoLi=[[Person alloc] init];//dog1 1Dog *dog1=[[[Dog alloc] init] autorelease];//把Dog1這條狗放在自動釋放池里 上面的pool中//dog1 2xiaoLi.dog=dog1;//[dog1 release]; 有了autorelease 就不可以在release //因為dog 已經放到pool中 在release 則dog1會清除 [xiaoLi release][pool release];//對擁有的非0(還存在)變量都release一次 return (0); }//dog.h #import <Foundation/Foundation.h> @interface Dog:NSObject {int _ID; } @property int ID; @end//dog.m #import<dog.h>@synthesize ID=_ID;-(void) dealloc //對象減到0(對象銷毀時)自動調用 {NSLog(@"dog dealloc "); }//person.h #import<Foundation/Foundation.h> #import "Dog.h" @interface Person:Object {Dog *_dog; } @property (retain) Dog *dog; @end//person.m #import "Person.h" @implementation Person @Synthesize dog=_dog; -(void) dealloc // 對象減到0時自動調用這條 {self.dog=nil;//對狗的計數器減1NSLog(@"person dealloc");[super dealloc]; } @end

?

協議

//第七課:協議int main(int argc,char *argv[]) {NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init]; }//只有一個頭文件 以Protocol開頭 方法沒有方法體 繼匙自蠳SObject@protocol MyProtocol <NSObject>-(void) init;-(int) updat:(int) time; @end//@optional 可以不實現 缺省 //@required 必須凳迪幟 //eg: //@optional 可實現可不實現 // -(void) init;//要實現的協議的名 用尖括號 多個用逗號隔開@interface:Foo:NSObject<MyProtocol,MouseListerner,MousKey>實現協議id<MyProtocol> test=[[MyTest alloc] init];if([test respondsToSelector:@select(showInfo)]){}//案例 //protocol@protocol MyProtocol<NSObject>@optional -(void) print:(int) vlaue;//可選 @required -(int) printValue:(int)value1 andValue:(int)value2; @end //MyTest.h@interface MyTest:NSObject<MyProtocol>-(void) showInfo;@end//MyTest.m-(void) ShowInfo{NSLog(@"Show info is calling");} //來源于MyProtocol協議-(int) printValue:(int)value1 andValue:(int) value2{NSLog(@"print value1 and %d value2 %d",value1,value2);return 0;}-(void) print:(int)value{NSLog(@"print vlaue %d",value);}@end#import <Foundation/Foundation.h> # "MyTest.h" # "MyProtocol.h" int main(int argc,const char *argv[]) {@autorelease{MyTest *myTest=[[MyTest alloc] init];[myTest showInfo];//把print轉化成SEL類型的方法 OC所有函數都可以轉換成SELSEL sel=@selector(print:);// 有參數冒號注意if([myTest respondsToSelector:sel]){//判斷myTest是否響應sel方法(print:)[myTest print:20];}[myTest pirntValue:10 andValue:290];[myTest release];//用協議id<MyProtocol> myProtocol =[[MyTest alloc] init];if([myProtocol respondsToSelector:@selector(print:)]){[myProtocol print:111];}[myProtocol printValue:193 andValue:103];[myProtocol release];}return 0; }

?

第八章:代理模式

  

?

//Dod.h 文件 #import<Foundation/Foundation.h> @protocol DogBark;// 協議前向聲明 @class Dog;//@class 表示前向聲明一個類 @interface Dog:NSObject {NSTimer *timer;// 定時器int backCount;//叫的次數int _ID;id <DogBark> delegate;//任何類型 存儲主人//告訴系統這個delegate是DogBark類型 } @property int ID;@property (assign) id <DogBark> delegate;//指針賦值 @end//方法二用協議來做 //定義一個人和狗通訊的方式protocol @protocol DogBark<NSObject> -(void) bark:(Dog *)thisDog count:(int)count; @end//Dod.m文件 #import "Dog.h" @implementation Dog @synthesize ID=_ID; @synthesize delegate; //構造函數ID 初始化 -(id) init {self=[super init];if(self){timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];//創建一個定時器 每隔1.0秒就調用[self updateTimer:nil] }return self; } -(void) updateTimer:(id) arg {backCount++;NSLog(@"dog bark %d",backCount);//通知狗的主人[_dog setDelegate:self]; //把self傳遞到delegate中[delegate bark:self count:barkCount];//調用delegate里面的bark:count:方法//向主人匯報 }@end//Person.h #import<Foundation/Foundation.h> #import "Dog.h" @interface Person:NSObject <DogBark> //實現協議 {DOg *_dog; }@property (retain) DOg *dog; @end//Person.m #import "Person.h" @implementation Person @synthesize dog=_dog;-(void) bark:(Dog *) thisDog count:(int)count { //當狗叫的時候來調用xiaoli人的這個方法NSLog(@"person this dog %d bark %d",[thisDog ID],count); }-(void) dealloc//當對象清零時 調用 {self.dog=nil;[super dealloc]; }// 重寫setDog 告知狗的主人是誰 -(void) setDog:(Dog *)dog {if(_dog!=dog){[_dog release];_dog=[dog retain];//通知_dog的主人是self } }-(Dog *) dog {return _dog; } @end#import<Foundation/Foundation.h> #import "Person.h" #import "Dog.h" int main(int argc,const char *argv[]) {@autoreleasepool{Person *xiaoLi=[[Person alloc] init];Dog *dog[[Dog alloc] int];[dog setID:10];[xiaoLi setDog:dog];[dog release];while(1){[[NSRunLoop currentRunLoop] run];}[xiaoLi release];}return 0; }

?第九章:Category

  

//Category 實際上是對類的擴展 //1實現繼承之外的擴展方法機制(給一個類擴展動態的或者靜態的一些方法進去)
//不能完全替代繼承(原因就是下面的缺點不能擴展字段和變量) 但可以完成繼承不能完成的。寫起來比繼承稍微麻煩但比繼承好用
//2可以做函數私有化 //eg: //interface Foo(Private)//Private 可以不寫 //-(void) test2; //@end //@implementation Foo //-(void) test{ // [self test2] //} //-(void) test2 //{ // NSLog(@"test2") //} //@end//缺點:類別Category只能擴展函數,消息,不能擴展字段,變量等//命名規范 //一般Category命名為: // 要擴展類名+擴展變量.[hm] //比如: // NSString+ReverseString.h // NSString+ReverseString.m // // UIImageView+WebCache.h // UIImageView+WebCache.m#import <Foundation/Foundation.h> @implementation NSString (ReverseString) -(id) reverseString {NSUInteger len=[self length];//self 表示字符串本身NSMutableString *retStr=[NSMutableString stringWithCapacity:len];while(len>0){unichar c=[self characterAtIndex:--len];//從后去一個字符unicharNSLog(@"c is %C",c);//C國際字符NSString *s=[NSString stringWithFormat:@"%C",c];[retStr appendString:s]}return retStr; } @end#import<Foundation/Foundation.h> #import "NSString+ReverseString.h"int main(int argc,const char *argv[]) {@autoreleasepool{NSString *string="Hello Word";NSString *retString=[string reverseString];NSLog(@"%@",retString);}return 0; }

?

?

?

轉載于:https://www.cnblogs.com/PEPE/archive/2012/08/25/2655577.html

總結

以上是生活随笔為你收集整理的Objective-C 日记⑤ 内存管理、协议、Category 视频笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。