从 plist 而不是 Code 数组读取:[NSCFArray objectForKey:]: 无法识别的选择器
我正在使用教程(现在无法访问)“UITableView - 搜索表视图”。
但我真的很难尝试从 plist 中读取内容。
我所做的改变:
- (void)viewDidLoad {
[super viewDidLoad];
//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];
TableViewAppDelegate *AppDelegate = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate];
listOfItems = [AppDelegate.data objectForKey:@"Countries"];
//Initialize the copy array.
copyListOfItems = [[NSMutableArray alloc] init];
//Set the title
self.navigationItem.title = @"Countries";
//Add the search bar
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
}
这就是我从 My AppDelegate.m 读取 plist 的方式
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
self.data = tempDict;
[tempDict release];
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
,这是我的 plist:
<plist version="1.0">
<dict>
<key>Countries</key>
<array>
<array>
<string>USA</string>
</array>
<array>
<string>UK</string>
</array>
</array>
</dict>
</plist>
我收到此错误:
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSCFArray objectForKey:]:发送到实例 0xe09120 的无法识别的选择器”
I am using tutorial (now inaccessible) "UITableView - Searching table view".
but I really have bad time try to read from plist.
that what I did change:
- (void)viewDidLoad {
[super viewDidLoad];
//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];
TableViewAppDelegate *AppDelegate = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate];
listOfItems = [AppDelegate.data objectForKey:@"Countries"];
//Initialize the copy array.
copyListOfItems = [[NSMutableArray alloc] init];
//Set the title
self.navigationItem.title = @"Countries";
//Add the search bar
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
}
and this how I read plist from My AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
self.data = tempDict;
[tempDict release];
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
and this my plist:
<plist version="1.0">
<dict>
<key>Countries</key>
<array>
<array>
<string>USA</string>
</array>
<array>
<string>UK</string>
</array>
</array>
</dict>
</plist>
and I get this error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFArray objectForKey:]: unrecognized selector sent to instance 0xe09120'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会将新分配的可变数组分配给成员。
这会泄漏上面的数组,并将一个自动释放的、不可变的数组分配给该成员。
稍后,当您访问listOfItems时,它可能已经被释放了。使用这个:
这一切都假设
[AppDelegate.data objectForKey:@"Countries"]
返回一个数组。鉴于 plist,它应该,但值得仔细检查。This assigns a newly allocated mutable array to a member.
This leaks the above array and assigns an autoreleased, immutable array to the member.
Later, when you access listOfItems, it may have been released. Use this:
This all assumes that
[AppDelegate.data objectForKey:@"Countries"]
is returning an array. Given the plist, it should, but it is worth double checking.