objective-c - 加载plist时数组下标不是整数

标签 objective-c ios nsmutablearray plist nsdictionary

我对 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];
}

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

- (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;
}

最佳答案

正如 H2CO3 所说,此语法是对象下标的一部分,而对象下标是“Modern Objective-C”的一部分,该对象下标是与 Xcode 4.4 和 4.5 一起发布的。

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

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

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 下标的使用仅限于此方法。但该项目已被重构为使用“现代 Objective-C”,并且您可能会看到其他现代 Objective-C 构造,例如数组文字:

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 的 WWDC 2012 session - C,这样你就会知道坚持使用旧编译器会错过什么,也可以弄清楚如何破译你可能会在 Stack Overflow 或其他地方看到的现代 Objective-C 代码。

更多信息请参见:

关于objective-c - 加载plist时数组下标不是整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13752496/

相关文章:

java - Objective-C + Cocoa 相当于 Java + Swing 的事件调度线程是什么?

ios - 设计和自定义 View Controller

ios - NSURLSessionDownloadTask API 调用 Swift 语法说明

ios - UILabel 动画淡入淡出不起作用

objective-c - UITableView deleteRowsAtIndex 和数据源问题

ios - Swift:将字符串转换为 NSMutableArray

objective-c - LibXML2 剥离属性内的新行

ios - 文本对齐 drawInRect 不工作

ios - 在 nsarray of nsdictionaries 中搜索性能内存优化方式

objective-c - NSGraphicsContext currentContext 返回 nil