我在我的 iPad 应用程序上使用 XCode 5 和 iOS 7。我正在尝试保存用户选择的行索引。在 -didSelectRowAtIndexPath 中,我使用此代码标记行:
// initialize singleton
SingletonSelectedCellIndexes *selectedCellIndexes = [SingletonSelectedCellIndexes sharedSelectedCellIndexes]; // initialize
// get the cell that was selected
UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath];
if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark) {
[(NSMutableSet *)selectedCellIndexes addObject:indexPath];
}
为什么我在单例的 addObject 上收到此错误?:
“SingletonSelectedCellIndexes”没有可见的@interface 声明选择器“addObject:”
这是定义单例(NSMutableSet)的代码:
//++++++++++++++++++++++++++++++++++++++++++++++++++++
//-- SingletonSelectedCellIndexes
@implementation SingletonSelectedCellIndexes {
}
@synthesize selectedCellIndexes; // rename
// sharedSelectedCellIndexes
+ (id)sharedSelectedCellIndexes {
static dispatch_once_t dispatchOncePredicate = 0;
__strong static id _sharedObject = nil;
dispatch_once(&dispatchOncePredicate, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
-(id) init {
self = [super init];
if (self) {
selectedCellIndexes = [[NSMutableSet alloc] init];
}
return self;
}
@end
更新- 这是 .h 文件:
//-- singleton: selectedCellIndexes
@interface SingletonSelectedCellIndexes : NSObject {
}
@property (nonatomic, retain) NSMutableSet *selectedCellIndexes;
+ (id)sharedSelectedCellIndexes;
@end