@property代表?

时间:2010-06-22 14:58:17

标签: iphone objective-c cocoa-touch

如果我可以,一个简单的问题,任何人都可以解释我在下面缺少的东西,我假设第三个与它有效吗?

@interface ...
// These work
@property(assign) SomeClass *someDelegate;

@property(assign) id someDelegate;

// This gives warning
@property(assign) id  <SomeClassDelegate> someDelegate;

@implementation ...
@synthesize someDelegate;

[self setSomeDelegate:[[SomeClass alloc] init]];
[someDelegate setDelegate:self];

warning: method '-setDelegate:' not found (return type defaults to 'id')

EDIT_001:

// SomeClass.h

#import <Foundation/Foundation.h>

@class SomeClass;

@protocol SomeClassDelegate <NSObject>
@optional
-(void)didHappen:(SomeClass *)someClass;
@required
-(void)willUse:(SomeClass *)someClass withThis:(BOOL)flag;
@end

@interface SomeClass : NSObject {
    id <SomeClassDelegate> delegate;
}
@property(assign) id <SomeClassDelegate> delegate;
-(void)otherActions;
@end
欢呼加里。

2 个答案:

答案 0 :(得分:3)

Go protocols!

@protocol MyDelegateProtocol
- (NSNumber*) someFunction:(NSArray*) anArray;
@end

@interface MyClass : NSObject {
  id<MyDelegateProtocol> delegate;
}

@property id<MyDelegateProtocol> delegate

@end

然后在你的@implementation中:

@synthesize delegate;

据我所知,可可方式: - )

干杯

的Nik

答案 1 :(得分:0)

除非它存在并且您没有显示它,否则您没有setDelegate方法。不过,你有一个setSomeDelegate方法。