以下代码崩溃:
@interface AppDelegate (PrivateMethods)
@property (nonatomic, strong) NSString * name;
@end
@implementation AppDelegate
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.name = @"foobar";
...
错误是:
'-[AppDelegate setName:]: unrecognized selector sent to instance 0x6d73df0'
当我改变
@interface AppDelegate (PrivateMethods)
到
@interface AppDelegate ()
然后就好了,请问是什么原因呢?
更新:正如下面回答的那样,因为我必须为此目的使用类扩展,所以现在这个问题变成了:使用类扩展来声明私有方法是否可以接受?
例如
@interface AppDelegate ()
- (void) start;
@property (nonatomic, strong) NSString * name;
@end