Re: [問題]請教 Objective-C 的語法

看板MacDev作者 (Loafer是了好)時間14年前 (2010/06/04 16:26), 編輯推噓2(204)
留言6則, 4人參與, 最新討論串2/2 (看更多)
※ 引述《copyshaft (冒煙的洋蔥)》之銘言: : 版上的朋友大家好: : 今天在研究一個Sample project看到了以下的語法, : #import <Cocoa/Cocoa.h> : @protocol MBBrotViewDelegate <NSObject> : -(void)mouseDownAtPoint:(NSPoint)point; : @end : @interface MBBrotView : NSView { : id <MBBrotViewDelegate> delegate; : BOOL mouseIsDown; : } : @property id <MBBrotViewDelegate> delegate; : @end : 想請教各位的問題如下: : 1. : @protocol MBBrotViewDelegate <NSObject> : 是指後續使用MBBrotViewDelegate這個protocol的物件還要 : implement NSObject這個protocol?? : (我的了解是NSObject是一個物件,這個宣告的 <> 讓我困惑) 有一個protocol 名字也叫NSObject : 2. : id <MBBrotViewDelegate> delegate; : 這個宣告,它的意思是是否是說delegate這個物件使用 : MBBrotViewDelegate這個protocol delegate這個變數,需要一個有實作<MBBrotViewDelegate> protocol的物件 id 代表不管任何型別只要有實作<MBBrotViewDelegate>的method就可以了 如果非要給型別的話 NSView <MBBrotViewDelegate> * delegate; 代表一定要有一個NSView * 的物件,也實作<MBBrotViewDelegate> : 等同以下的宣告??? : @interface delegate : NSObject <MBBrotViewDelegate> : {} : @end : 二 語法片段2: : [[NSNotificationCenter defaultCenter] : addObserverForName:NSApplicationDidFinishLaunchingNotification : object:nil queue:nil : usingBlock:^(NSNotification *notif) { : currentImageLayer.frame = NSRectToCGRect(brotView.bounds); : [brotView.layer addSublayer:currentImageLayer]; : }]; : 在下是第一次看到 ^(NSNotification *notif) {} : 直覺很像 pointer of function : 查閱官方文件(NSNotificationCenter)也沒看到usingBlock的相關資訊 : 不知版上是否有前輩能大略說明一下 ^() 的用途 : 在下資質駑頓還望版上前輩能不吝指教,謝謝。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.85.163.134

06/04 20:00, , 1F
offname 謝謝你. 受益良多,感謝再感謝.
06/04 20:00, 1F

06/04 21:11, , 2F
語法片段2的那個東西,只能用在Mac OS上,iPhone OS還沒支援
06/04 21:11, 2F

06/04 21:11, , 3F
那個…跟C#語法的delegate很像,就是Block of codes
06/04 21:11, 3F

06/04 21:45, , 4F
4.0 就會支援 block 了
06/04 21:45, 4F

06/04 21:46, , 5F
簡單講,block 就是 lambda
06/04 21:46, 5F

06/04 23:01, , 6F
06/04 23:01, 6F
文章代碼(AID): #1C2BYp-Q (MacDev)
文章代碼(AID): #1C2BYp-Q (MacDev)