为什么委托声明前面没有*?
我只是注意到委托声明前面没有 * ...
我做了这样的事情:
@protocol NavBarHiddenDelegate;
@interface AsyncImageView : UIView {
NSURLConnection* connection;
NSMutableData* data;
UIActivityIndicatorView *indicator;
id <NavBarHiddenDelegate> delegate;
}
@property (nonatomic, assign) id <NavBarHiddenDelegate> delegate;
- (id)initWithUrl:(NSString*)url;
@end
@protocol NavBarHiddenDelegate
- (void)hideNavBar;
@end
它工作得很好,但因为我习惯于在我声明的对象前面总是有一个 * ,为什么不呢? ?!?
谢谢你,
戈泰。
I just noticed there is no * in front of the declaration for a delegate ...
I did something like this :
@protocol NavBarHiddenDelegate;
@interface AsyncImageView : UIView {
NSURLConnection* connection;
NSMutableData* data;
UIActivityIndicatorView *indicator;
id <NavBarHiddenDelegate> delegate;
}
@property (nonatomic, assign) id <NavBarHiddenDelegate> delegate;
- (id)initWithUrl:(NSString*)url;
@end
@protocol NavBarHiddenDelegate
- (void)hideNavBar;
@end
It works perfectly well but as I am used to always but a * in front of objects I declare, why not for this one ?!?
Thank you,
Gotye.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与代表无关。
由于历史原因,
id
类型有所不同;将其视为任何对象*
。每当你写id
时,就没有*
。如果所有 Objective-C 对象都有一个根类
Object
,那么您可以想象typedef Object * id;
— 但没有,所以id
是不同的(好吧,实际上,如果我没记错的话,它被定义为类似于 struct objc_object * 的东西,但你不应该担心该实现细节与尊重-等级)。This has nothing to do with delegates.
The type
id
is different, for historical reasons; think of it asany-object *
. Whenever you writeid
, there is no*
.If there were a single root class
Object
for all Objective-C objects then you could imagine thattypedef Object * id;
— but there isn't, soid
is different (well, actually, it's defined as something likestruct objc_object *
if I recall correctly, but you shouldn't worry about that implementation-detail-with-respect-to-this-level).因为
id
已经有了它隐含的*
。如果您忽略协议限制,则显然
不需要
*
。顺便说一句,如果协议放在 ObjC 类型上,那么您需要*
,例如Because
id
already has its implicit*
. If you ignore the protocol restriction thenbecomes
which obviously doesn't need a
*
. BTW, if the protocol is put on an ObjC type then you need the*
, e.g.