从 plist 而不是 Code 数组读取:[NSCFArray objectForKey:]: 无法识别的选择器

发布于 2024-09-06 02:53:44 字数 1843 浏览 2 评论 0原文

我正在使用教程(现在无法访问)“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙味迷妹 2024-09-13 02:53:44

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

listOfItems = [[NSMutableArray alloc] init];

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

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

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

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

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

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文