作者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