【问题标题】:Unable to add object to NSMutableSet (error: no visible interface)无法将对象添加到 NSMutableSet(错误:没有可见界面)
【发布时间】:2013-09-21 21:46:56
【问题描述】:

我在我的 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

【问题讨论】:

    标签: ios objective-c nsmutableset


    【解决方案1】:

    您需要先显示 SingletonSelectedCellIndexes 类的 .h 文件,然后我们才能回答。

    你的 .m 文件没有显示 addObject 方法,所以我敢打赌你的 .h 文件也没有。

    如果你想调用一个类的方法,你需要声明并实现那个方法。

    【讨论】:

    • 我决定使用不同的方法来完成我所需要的。感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2023-03-24
    相关资源
    最近更新 更多