1

在我的主要 iOS 项目中,我有以下情况:
在 file1.m 我有:

NSString* s[] = {@"1", @"2", @"3", @"4"};

在 file2.m 我有:

extern NSString** s;

在同一个 file2.m 中,下面的方法失败,“无法识别的选择器'长度'发送到实例':

- (void) someMethod {
    int len = [s[0] length];
}

如果我创建一个新的演示项目并复制上面的代码,一切都会按预期工作。
我的主要项目可能有什么问题?我被困在这里2天试图找到问题。
同样的问题出现在 int 数组中,不同之处在于当我尝试访问数组中的元素时收到错误的访问异常,尽管 XCode 调试器显示数组内容很好。

4

3 回答 3

1

Please verify that the global array name declared with extern matches the name of the array that you reference in the call of length (i.e. the s in extern NSString** s; and [s[0] length] is the same name), and that there is no other variable named s in the scope of the call of length that would "shadow" the global. Since the code works fine in a smaller project, naming collision is the most likely possibility.

于 2013-05-31T14:38:46.967 回答
0

extern NSString** s;

您只需声明一个指向 NSString 对象指针的指针,正确的声明应该是:

extern NSString *s[];
于 2013-05-31T14:35:18.967 回答
0

I have some ideas what could be happening there.

  1. You are overwriting the array somewhere. It's always helpful to declare constant C arrays with const

  2. Variable name collision
    You have two globals with the same name but different objects. Using globals with names like s can lead to collisions easily. Make sure to use prefixes for globals, exactly in the same way as you are doing for classes, enums, etc.

于 2013-05-31T14:36:44.143 回答