Objective C 单例 class 成员
Objective C singleton class members
这适用于 Mac OS X 应用程序。我创建了一个单例 class,但我不确定如何添加 class 成员(不确定这是否正确)。我收到错误 Property 'chordDictionary' not found on object of type '__strong id'
,我不确定为什么。我想创建一个我可以通过这个 class 访问的 NSDictionary。这是我的代码:
#import "ChordType.h"
@interface ChordType()
@property NSDictionary *chordDictionary;
@end
@implementation ChordType
+ (instancetype)sharedChordData {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
sharedInstance.chordDictionary = @{@"" : @"047", @"m" : @"037", @"dim" : @"036", @"aug" : @"048",}; //error is on this line
});
return sharedInstance;
}
@end
将 sharedInstance
声明为 ChordType *
而不是 id
或调用 setChordDictionary:
方法而不是使用 属性 语法。您不能对 id
.
类型的变量使用 属性 语法
任一:
static ChordType *sharedInstance = nil;
或:
[sharedInstance setChordDictionary:@{@"" : @"047", @"m" : @"037", @"dim" : @"036", @"aug" : @"048"}];
将这些添加到您的 ChordType 头文件中 class
@property NSDictionary *chordDictionary;
+ (ChordType *)sharedChordData;
然后用这个
修改你的代码
+ (ChordType *)sharedChordData {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
sharedInstance.chordDictionary = @{@"" : @"047", @"m" : @"037", @"dim" : @"036", @"aug" : @"048",}; //error is on this line
});
return sharedInstance;
}
然后你可以像这样访问属性,
[ChordType sharedChordData].chordDictionary = @{@"" : @"047", @"m" : @"037", @"dim" : @"036", @"aug" : @"048"};
通过这种方式,您通过其共享实例 shareChordData sharedChordData 以 public 属性 的身份访问 chordDictionary基本上是一个静态方法,可以通过 class 成员访问。
这适用于 Mac OS X 应用程序。我创建了一个单例 class,但我不确定如何添加 class 成员(不确定这是否正确)。我收到错误 Property 'chordDictionary' not found on object of type '__strong id'
,我不确定为什么。我想创建一个我可以通过这个 class 访问的 NSDictionary。这是我的代码:
#import "ChordType.h"
@interface ChordType()
@property NSDictionary *chordDictionary;
@end
@implementation ChordType
+ (instancetype)sharedChordData {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
sharedInstance.chordDictionary = @{@"" : @"047", @"m" : @"037", @"dim" : @"036", @"aug" : @"048",}; //error is on this line
});
return sharedInstance;
}
@end
将 sharedInstance
声明为 ChordType *
而不是 id
或调用 setChordDictionary:
方法而不是使用 属性 语法。您不能对 id
.
任一:
static ChordType *sharedInstance = nil;
或:
[sharedInstance setChordDictionary:@{@"" : @"047", @"m" : @"037", @"dim" : @"036", @"aug" : @"048"}];
将这些添加到您的 ChordType 头文件中 class
@property NSDictionary *chordDictionary;
+ (ChordType *)sharedChordData;
然后用这个
修改你的代码+ (ChordType *)sharedChordData {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
sharedInstance.chordDictionary = @{@"" : @"047", @"m" : @"037", @"dim" : @"036", @"aug" : @"048",}; //error is on this line
});
return sharedInstance;
}
然后你可以像这样访问属性,
[ChordType sharedChordData].chordDictionary = @{@"" : @"047", @"m" : @"037", @"dim" : @"036", @"aug" : @"048"};
通过这种方式,您通过其共享实例 shareChordData sharedChordData 以 public 属性 的身份访问 chordDictionary基本上是一个静态方法,可以通过 class 成员访问。