【问题标题】:[__NSCFString superview]: unrecognized selector sent to instance[__NSCFString superview]:发送到实例的无法识别的选择器
【发布时间】:2012-11-08 13:43:51
【问题描述】:

我正在尝试将 NSMutableArray* 复制到 NSArray* 中,但它不起作用并且会生成 [__NSCFString superview]: unrecognized selector sent to instance 错误。代码如下:

//where gc is a NSDictionary*, recentKey is a NSString*, and _objects is a NSArray*
//gc is an singleton is used to save chache data, with NSKeyedUnarchiver class
//_objects is used to reload the UITableView's data

NSArray *savedNews = [[NSArray alloc] initWithArray:[gc objectForKey:recentkey]];

//this not works...why??
_objects = [[NSArray alloc] initWithArray:savedNews];

分辨率:

是的,正如赫尔曼所说,错误是外部的。 savedNews 数组使用带有 NSEncoding 的类,但出现错误:

- (void)encodeWithCoder:(NSCoder *)encoder {
//...where element was NSString* and not "UIImageView"
// element should be imgView
    if (imgView) [encoder encodeObject:element forKey:@"imgView"];
}

谢谢大家。

【问题讨论】:

  • 哪一行失败了?你确定你在上下文中发布了所有代码吗?
  • _objects 是一个 NSArray*。失败的行是_objects的分配。

标签: objective-c ios memory nsarray unrecognized-selector


【解决方案1】:

在您的应用程序中某处是获取的 NSString 对象的父视图。 我猜您将 NSString 对象分配给了需要 UIView 的东西。 可能是这样的:

someButton.textLabel = someString; // wrong - but should generate a compiler warning

而不是

someButton.textLabel.text = someString; // correct

这与您的阵列问题没有直接关系。

【讨论】:

  • 正如赫尔曼所说,您的问题与您提供的代码无关。您必须仔细检查 UI 相关代码。
  • 只有在重新分配 _objects 时才会发生。更改数据的事实是否会影响 UITableView 或某些视图?
  • 首先,您应该准确确定问题出现在代码执行的哪个点。不要寻找像“它只发生在......”这样的副作用。你设置异常断点了吗?如果没有 - 去做吧。如果您在中断点上没有看到错误消息,那么再按一次或两次继续按钮,您将更接近问题的根本原因。
【解决方案2】:

首先,检查字典中该键的对象是什么。

NSLog(@"GC Object type for recentkey:%@", [[gc objectForKey:recentkey] class]);

您只能将 NSArray 传递给 initWithArray: 因此,如果该对象还不是 NSArray 但您希望该对象位于数组中。这样做..

id obj = [gc objectForKey:recentkey]; //Because I have no idea what this is
NSArray *savedNews = [NSArray arrayWithObject:obj];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多