【发布时间】: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 405 和 session 413。另外,一个小细节,但您的主题行是指非数字数组下标。实际上是
NSDictionary具有非数字下标的对象。NSArray对象具有数字下标(这本身就是一项新功能)。
标签: objective-c ios nsmutablearray plist nsdictionary