gpt4 book ai didi

objective-c - 使用 .plist 中的键在数组中创建 (Cocoa/iPhone SDK)

转载 作者:行者123 更新时间:2023-12-03 17:06:57 28 4
gpt4 key购买 nike

嘿伙计们,尝试以编程方式将(对象?)添加到我的 plist,这是我到目前为止所做的:

 NSString *documentsDirectory = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents/myfolder"]];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Favourites.plist"];

NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:writablePath];


[rootDict setValue:@"My Third PDF" forKey:@"Title"];
[rootDict writeToFile:writablePath atomically: YES];

这是我的plist:

<plist version="1.0">
<dict>
<key>Rows</key>
<array>
<dict>
<key>SaveName</key>
<string>first.pdf</string>
<key>Title</key>
<string>My first PDF</string>
</dict>
<dict>
<key>SaveName</key>
<string>second.pdf</string>
<key>Title</key>
<string>My Second PDF</string>
</dict>
</array>
</dict>
</plist>

我如何添加另一个(对象?),例如

<dict>
<key>SaveName</key>
<string>third.pdf</string>
<key>Title</key>
<string>My third PDF</string>
</dict>

到我的plist?谢谢!

最佳答案

您首先必须确保 Rows 数组是可变的:

[rootDict setObject:[[rootDict objectForKey:@"Rows"] mutableCopy] forKey:@"Rows"];

您需要创建一个具有该结构的字典,例如

NSDictionary *third = [NDSictionary dictionaryWithObjectsAndKeys:
@"third.pdf", @"SaveName",
@"My third PDF", @"Title",
nil];

并将其添加到数组中:

[[rootDict objectforKey:@"Rows"] addObject:third];

关于objective-c - 使用 .plist 中的键在数组中创建 <dict> (Cocoa/iPhone SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5148586/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com