为什么无法使用Swift将数据保存到plist文件中

问题描述 投票:0回答:1

** readData.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>currentChapter</key>
    <string>2</string>
    <key>currentLine</key>
    <string>2</string>
    <key>bookname</key>
    <string>ExcelBible</string>
</dict>
</plist>


**以下是代码:**


        let Chapter = currentChapter
        let Line = currentLine
        let name = bookname

        let dict:NSMutableDictionary = NSMutableDictionary()



        dict.setObject("1", forKey: "currentChapter" as NSCopying)
        dict.setObject("1", forKey: "currentLine" as NSCopying)
        dict.setObject("WordBible", forKey: "bookname" as NSCopying)

        //I also use dict[0] = "1", and setValue() method both of them are not work

        let plistPath = Bundle.main.path(forResource: "readData", ofType: "plist")

        dict.write(toFile: plistPath!, atomically: true)


Blockquote我也在这里使用FileManager和几个示例来保存数据,其中一些显示为“ Successfully write”,但文件没有更改。

Blockquote


[我是2020年2月12日,我是iOS开发人员

ios swift plist
1个回答
0
投票

我认为该文件不在可写位置,因此应在documents目录中创建它。然后,必须从文档目录中读取它。

© www.soinside.com 2019 - 2024. All rights reserved.