从plist insted中读取Code数组

时间:2023-02-06 10:56:07

am Using UITableView - Searching table view

我正在使用UITableView - 搜索表视图

its really nice easy tutorial but i really have bad time try to read from plist

它非常好的简单教程,但我真的很难尝试从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

这是我如何从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];

}

}

and this my plist

这是我的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'

*由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'* - [NSCFArray objectForKey:]:无法识别的选择器发送到实例0xe09120'

help please am stock with this

请帮帮忙

1 个解决方案

#1


0  

This assigns a newly allocated mutable array to a member.

这会将新分配的可变数组分配给成员。

listOfItems = [[NSMutableArray alloc] init];

This leaks the above array and assigns an autoreleased, immutable array to the member.

这会泄漏上面的数组并为该成员分配一个自动释放的不可变数组。

listOfItems = [AppDelegate.data objectForKey:@"Countries"];

Later, when you access listOfItems, it may have been released. Use this:

稍后,当您访问listOfItems时,它可能已被释放。用这个:

[listOfItems addObjectsFromArray:[AppDelegate.data objectForKey:@"Countries"]];

This all assumes that [AppDelegate.data objectForKey:@"Countries"] is returning an array. Given the plist, it should, but it is worth double checking.

这都假定[AppDelegate.data objectForKey:@“Countries”]正在返回一个数组。鉴于plist,它应该,但值得仔细检查。

#1


0  

This assigns a newly allocated mutable array to a member.

这会将新分配的可变数组分配给成员。

listOfItems = [[NSMutableArray alloc] init];

This leaks the above array and assigns an autoreleased, immutable array to the member.

这会泄漏上面的数组并为该成员分配一个自动释放的不可变数组。

listOfItems = [AppDelegate.data objectForKey:@"Countries"];

Later, when you access listOfItems, it may have been released. Use this:

稍后,当您访问listOfItems时,它可能已被释放。用这个:

[listOfItems addObjectsFromArray:[AppDelegate.data objectForKey:@"Countries"]];

This all assumes that [AppDelegate.data objectForKey:@"Countries"] is returning an array. Given the plist, it should, but it is worth double checking.

这都假定[AppDelegate.data objectForKey:@“Countries”]正在返回一个数组。鉴于plist,它应该,但值得仔细检查。