【问题标题】:Array subscript is not an integer when loading plist加载plist时数组下标不是整数
【发布时间】:2012-12-06 21:02:05
【问题描述】:

我是 iOS/Objective C 的新手,但多年来我一直在使用多种不同的语言进行编程。

我们公司已对现有的 iOS 应用程序进行修改。现在我正在尝试加载一个 plist 文件并用它的内容填充一个 UITableView 。我的代码创建单元时出错,我不确定发生了什么。

这是我的plist文件,很简单:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <dict>
            <key>title</key><string>Un</string>
            <key>subtitle</key><string>Subtitle</string>
        </dict>
        <dict>
            <key>title</key><string>Deux</string>
            <key>subtitle</key><string>Subtitle</string>
        </dict>
    </array>
</plist>

这里是我将 plist 文件加载到内存中的地方:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:@"resources" ofType:@"plist"];
    resources = [NSMutableArray arrayWithContentsOfFile:plistCatPath];
}

这是我尝试访问属性的地方。我在编译时遇到错误的地方添加了 cmets:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int index = (int) indexPath.row;
    UITableViewCell *cell =[factory cellOfKind:@"cell" forTable:tableView];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    UILabel *lblTitle = (UILabel*)[cell viewWithTag:2];
    UILabel *lblSubtitle = (UILabel*)[cell viewWithTag:3];
    NSDictionary *resource = resources[index]; // incompatible types in initialization
    NSString *row_title = resource[@"title"]; // array subscript is not an integer
    NSString *row_subtitle = resource[@"subtitle"]; // array subscript is not an integer
    [lblTitle setText: row_title];
    [lblSubtitle setText: row_subtitle];
    return cell;
}

【问题讨论】:

  • 您确定您使用的是最新版本的clang? GCC 和旧的 Clang 无法识别这种结构。
  • @H2CO3 我几乎可以肯定我不是。由于现有项目的源代码较旧,并且只能访问 10.6.8 的 mac,我正在运行 XCode 3.2.6 并为 iOS 4.2 进行编译。除非有必要,否则我一直拒绝升级任何或所有这些。您会推荐“另一种构造”(如果可能的话)还是因为其他原因我需要升级?
  • 还有另一种结构,看我的回答。
  • @raydowe 简而言之,这是 Xcode 4.4 和 4.5 中引入的“现代 Objective-C”。请参阅 WWDC 2012 session 405session 413。另外,一个小细节,但您的主题行是指非数字数组下标。实际上是 NSDictionary 具有非数字下标的对象。 NSArray 对象具有数字下标(这本身就是一项新功能)。

标签: objective-c ios nsmutablearray plist nsdictionary


【解决方案1】:

正如 H2CO3 所说,这种语法是对象下标的一部分,它是与 Xcode 4.4 和 4.5 一起发布的“现代 Objective-C”的一部分。

有关非数字下标的讨论,请访问Objective-C Literals 网站并向下滚动到标题为“下标方法”(Apple 有时称之为“装箱语法”或“装箱语法”)的部分,您'会看到那里讨论的。我在答案的底部包含了其他参考资料。

第一个例子是数组样式下标,用于在数组中查找值:

NSArray *resources = ... // however it was defined
NSDictionary *resource = resources[index];

编译器将其翻译为:

NSDictionary *resource = [resources objectAtIndexedSubscript:index];

如果你不想在你的机器上升级 Xcode(和编译器),你可以替换,你可以简单地将数组样式的下标替换为传统的 objectAtIndex 语法:

NSDictionary *resource = [resources objectAtIndex:index];

同样,您向我们展示的第二个示例是使用非数字下标。这是字典式下标

NSString *row_title = resource[@"title"];

编译器将转换为:

NSString *row_title = [resource objectForKeyedSubscript:@"title"];

同样,如果您不想升级您的 Xcode(及其编译器),传统的等价物是:

NSString *row_title = [resource objectForKey:@"title"];

底线,也许你会很幸运,NSArrayNSDictionary 的下标的这种使用仅限于这种方法。但该项目已被重构为使用“Modern Objective-C”,您可能会看到其他 Modern Objective-C 构造,例如 array literals

NSArray *array = @[@"one", @"two", @"three"];

如果您在这个项目中的任何地方都有它,那么您的旧编译器将无法理解该数组文字语法,您必须将其替换为:

NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];

同样,您可能会看到字典文字

NSDictionary *dictionary = @{@"key1" : @"value1", @"key2" : @"value2"};

您必须将其替换为:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];

最后,你可能也有数字文字,例如:

NSNumber *number = @4;
NSNumber *done = @YES;

相当于

NSNumber *number = [NSNumber numberWithInt:4];
NSNumber *done = [NSNumber numberWithBool:YES];

根据您的项目有多少现代 Objective-C,您可能需要执行相当多的编辑。或者,如果您可以升级到 Xcode 4.5 或更高版本,您将能够顺利编译此代码。

Xcode 4.5 中还有很多其他非常有用的构造,包括新的NSDictionary 枚举方法、类型化枚举、字符串文字等。您应该观看有关 Modern Objective-C 的 WWDC 2012 会议,以便您通过坚持使用旧编译器,您将知道您错过了什么,而且您还可以弄清楚如何破译您可能会在 Stack Overflow 或其他地方看到的现代 Objective-C 代码。

欲了解更多信息,请参阅:

【讨论】:

  • 感谢您非常详尽的解释。底部的链接已经证明非常有用。
【解决方案2】:

这是因为显然您使用的旧编译器无法识别新的对象字面量语法。如果您不想升级,请在NSArrayNSDictionary 上使用适当的实例方法:

NSDictionary *resource = [resources objectAtIndex:index];
NSString *row_title = [resource objectForKey:@"title"];
NSString *row_subtitle = [resource objectForKey:@"subtitle"];

如果您选择这种方法,您将保持向后兼容性,这很好。

【讨论】:

  • 谢谢。这就解释了为什么我发现的大部分示例代码的语法都存在问题。
猜你喜欢
  • 2011-08-02
  • 2011-06-05
  • 2011-10-22
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多