作者offname (Loafer是了好)
看板MacDev
標題Re: [問題]請教 Objective-C 的語法
時間Fri Jun 4 16:26:24 2010
※ 引述《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
1F:推 copyshaft:offname 謝謝你. 受益良多,感謝再感謝. 06/04 20:00
2F:推 aecho:語法片段2的那個東西,只能用在Mac OS上,iPhone OS還沒支援 06/04 21:11
3F:→ aecho:那個…跟C#語法的delegate很像,就是Block of codes 06/04 21:11
4F:→ zonble:4.0 就會支援 block 了 06/04 21:45
5F:→ zonble:簡單講,block 就是 lambda 06/04 21:46