从 Plist 中读取信息以在 TableView 中使用

发布于 2024-11-04 05:50:29 字数 2165 浏览 3 评论 0原文


我检查了其他帖子,但似乎没有回答我的问题,所以我开始了:)
我有一个 Plist,包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>001</key>
    <dict>
        <key>Name</key>
        <string>Name1</string>
        <key>Number</key>
        <string>001</string>
    </dict>
    <key>002</key>
    <dict>
        <key>Name</key>
        <string>Name2</string>
        <key>Number</key>
        <string>002</string>
    </dict>
</dict>
</plist>

我试图将此代码转换为数组并用于 TableView 源。然而,即使我看过其他帖子,也没有人能帮助我(我可以看到,这是我的第一个真正的 iOS 项目,所以我可能无法 100% 地操作他们的代码)。我希望从这个 Plist 生成表格,并且由于它将至少有 100 个项目(通过 Plist 添加更多项目),所以我希望生成它。
然而,我正在努力做的主要事情是让数组从 Key1 获取信息,然后是 Key2,Key3... 就像下面的手动代码一样(我必须手动输入,我将添加更多信息,所以我希望它来自 Plist):

NSDictionary *item1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                          @"Name1", @"Name", @"001", @"Number", nil]; 
NSDictionary *item2 = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"Name2", @"Name", @"002", @"Number", nil];
NSDictionary *item3 = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"Name3", @"Name", @"003", @"Number", nil];

我想要的一件事是能够更改表格显示的顺序(数字和字母之间),我认为这没有很大的相关性,但我想把它放在那里,以防万一我犯了一些意味着无法完成的错误?
感谢您的阅读,
约瑟夫·达菲

编辑:已解决,但新问题,请阅读下文。 现已解决,谢谢! 感谢您提供的信息,它非常有用!但是,当我输入以下内容时:

- (void)viewDidLoad
{
    NSBundle *bundleLocation = [NSBundle mainBundle];
    NSString *arrayLocation = [bundleLocation pathForResource:@"plistname" ofType:@"plist"];
    NSArray *plistcontentsArray = [[NSArray alloc] initWithContentsOfFile:arrayLocation];
    self.pokemonNames = pokemonArray;

    [bundleLocation release];
    [plistArray release];

    [super viewDidLoad];
}

一切正常。但是当我添加 [arrayLocation release]; 时,它会因“exc_bad_access”而崩溃。有什么想法吗? :)

I've checked the other posts, but non seem to answer my question, so here I go :)
I've got a Plist, with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>001</key>
    <dict>
        <key>Name</key>
        <string>Name1</string>
        <key>Number</key>
        <string>001</string>
    </dict>
    <key>002</key>
    <dict>
        <key>Name</key>
        <string>Name2</string>
        <key>Number</key>
        <string>002</string>
    </dict>
</dict>
</plist>

I'm trying to get this code to converted to an array and used for a TableView source. However, even thought I've seen other posts, none help me out (That I can see, this is my first true iOS Project, so I might not be 100% at manipulating their code). I want the table to be generated from this Plist, and since it's going to have at least 100 items (With more being added via the Plist), I'd like for it to be generated.
However, the main thing I'm struggling to do is get the array to get the information from Key1, then Key2, Key3... like it would in the following manual code (Which I have to type out manually, and I'll be adding more info, so I want it to come from the Plist):

NSDictionary *item1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                          @"Name1", @"Name", @"001", @"Number", nil]; 
NSDictionary *item2 = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"Name2", @"Name", @"002", @"Number", nil];
NSDictionary *item3 = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"Name3", @"Name", @"003", @"Number", nil];

One thing I would like is to be able to change the order the table is shown in (Between number and alphabetic), which I don't think is hugely relevant, but it's something I'd like to throw in there, incase I'm making a mistake with something meaning that can't be done?
Thank you for reading,
Joseph Duffy

Edit: Solved, but new issue, please read below. Now solved, thank you!
Thank you for the information, it has been most useful! However, when I put the following:

- (void)viewDidLoad
{
    NSBundle *bundleLocation = [NSBundle mainBundle];
    NSString *arrayLocation = [bundleLocation pathForResource:@"plistname" ofType:@"plist"];
    NSArray *plistcontentsArray = [[NSArray alloc] initWithContentsOfFile:arrayLocation];
    self.pokemonNames = pokemonArray;

    [bundleLocation release];
    [plistArray release];

    [super viewDidLoad];
}

Everything works fine. But when I add [arrayLocation release]; it crashes with "exc_bad_access". Any ideas? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

乱了心跳 2024-11-11 05:50:29

只需在属性列表中使用“array”元素而不是“dict”元素即可。例如:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Name</key>
        <string>Name1</string>
        <key>Number</key>
        <string>001</string>
    </dict>
    <dict>
        <key>Name</key>
        <string>Name2</string>
        <key>Number</key>
        <string>002</string>
    </dict>
</array>
</plist>

您还只是要存储名称还是要在嵌套字典中存储更多信息?如果没有,那么这可以简化为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Name1</string>
    <string>Name2</string>
</array>
</plist>

要加载属性列表中的数组,只需使用以下命令。 (将“Array”更改为属性列表的名称,并且很可能使用 NSArray 的实例变量。)

NSBundle * mainBundle = [NSBundle mainBundle];
NSString * arrayLocation = [mainBundle pathForResource:@"Array" ofType:@"plist"];
NSArray * array = [[NSArray alloc] initWithContentsOfFile:arrayLocation];

并从可以使用的数组中提取信息:

NSString * name = [array objectAtIndex:0];

至于按字母顺序排列,您可以找到如何做到这一点这里:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW5

Simply use the "array" element instead of the "dict" element in your property list. For example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Name</key>
        <string>Name1</string>
        <key>Number</key>
        <string>001</string>
    </dict>
    <dict>
        <key>Name</key>
        <string>Name2</string>
        <key>Number</key>
        <string>002</string>
    </dict>
</array>
</plist>

Also are you just going to store names or are you going to store more information in the nested dictionaries? If not then this can be reduced down to:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>Name1</string>
    <string>Name2</string>
</array>
</plist>

To load the array in the property list simply use the following. (Changing "Array" to the name of the property list and more than likely using an instance variable for the NSArray.)

NSBundle * mainBundle = [NSBundle mainBundle];
NSString * arrayLocation = [mainBundle pathForResource:@"Array" ofType:@"plist"];
NSArray * array = [[NSArray alloc] initWithContentsOfFile:arrayLocation];

and to pull the information out of the array you can use:

NSString * name = [array objectAtIndex:0];

As for doing alphabetical order you can find how to do that here:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/20000132-SW5

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