如何添加 NSArray 作为 NSMutableDictionary 键?

发布于 2024-12-09 12:35:15 字数 1448 浏览 0 评论 0原文

我尝试执行以下操作:

NSMutableArray *myArr = [NSMutableArray arrayWithArray:[myDict objectForKey:@"myKey"]];
[myArr addObject:@"New string xxx"];
[myDict setObject:myArr forKey:@"myKey"];
// Write to file (done successfully)

如您所见,我得到一个数组,该数组存储在 plist 中,键为 @"myKey",向其中添加一些对象并希望将其重新记录在plist,当然还有一些额外的值。

问题是,我的 plist 上发生以下情况:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs$
<plist version="1.0">
<dict>
        <key>myKey</key>
        <array>
                <string>First entry on the plist</string>

                <array>
                    <string>First entry on the plist</string>
                    <string>New string xxx</string>
                </array>
        </array>
</dict>
</plist>

当然,所需的输出是:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs$
    <plist version="1.0">
    <dict>
            <key>myKey</key>
            <array>
                    <string>First entry on the plist</string>
                    <string>New string xxx</string>
            </array>
    </dict>
    </plist>

I tried doing the following:

NSMutableArray *myArr = [NSMutableArray arrayWithArray:[myDict objectForKey:@"myKey"]];
[myArr addObject:@"New string xxx"];
[myDict setObject:myArr forKey:@"myKey"];
// Write to file (done successfully)

As you can see, I get an array which is stored on a plist with the key @"myKey", add some objects to it and want to re-record it on the plist, with some extra values of course.

Problem is, the following happens on my plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs$
<plist version="1.0">
<dict>
        <key>myKey</key>
        <array>
                <string>First entry on the plist</string>

                <array>
                    <string>First entry on the plist</string>
                    <string>New string xxx</string>
                </array>
        </array>
</dict>
</plist>

The desired output is, of course:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs$
    <plist version="1.0">
    <dict>
            <key>myKey</key>
            <array>
                    <string>First entry on the plist</string>
                    <string>New string xxx</string>
            </array>
    </dict>
    </plist>

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

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

发布评论

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

评论(2

眼睛会笑 2024-12-16 12:35:15

您是否尝试过在再次设置之前调用[myDict removeObjectForKey:@"myKey"]

Have you tried calling [myDict removeObjectForKey:@"myKey"] before setting it again?

雨巷深深 2024-12-16 12:35:15

其工作正常

NSMutableArray *myArr = [NSMutableArray arrayWithArray:[myDict objectForKey:@"myKey"]];
[myArr addObject:@"New string xxx"];
[myDict setObject:myArr forKey:@"myKey"];
[myDict writeToFile:@"/path/p.text" atomically:NO];
[myArr addObject:@"new object"];
[myDict setObject:myArr forKey:@"myKey"];
[myDict writeToFile:@"/path/p.text" atomically:NO];

输出:

<?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>myKey</key>
    <array>
        <string>New string xxx</string>
        <string>new object</string>
    </array>
</dict>
</plist>

its working fine

NSMutableArray *myArr = [NSMutableArray arrayWithArray:[myDict objectForKey:@"myKey"]];
[myArr addObject:@"New string xxx"];
[myDict setObject:myArr forKey:@"myKey"];
[myDict writeToFile:@"/path/p.text" atomically:NO];
[myArr addObject:@"new object"];
[myDict setObject:myArr forKey:@"myKey"];
[myDict writeToFile:@"/path/p.text" atomically:NO];

Output:

<?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>myKey</key>
    <array>
        <string>New string xxx</string>
        <string>new object</string>
    </array>
</dict>
</plist>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文