[英]Why can't I save data in plist file with Swift
** 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和幾個例子來保存數據,有的顯示“成功寫入”,但是文件沒有變化。
塊引用
我從 2020 年 2 月 12 日起成為 iOS 開發人員
我認為該文件不在可寫位置,因此您應該在文檔目錄中創建它。 然后,必須從文檔目錄中讀取它。
塊引用
我使用了以下代碼,但它也不起作用。
塊引用
let currentChapter = "10"
let currentLine = "10"
let nameOfBook = "WordBible"
let readData = [currentChapter, currentLine, nameOfBook]
//
let dataFilePaths = FileManager.default.urls(for: .documentDirectory,
in: .userDomainMask)[0].appendingPathComponent("gameSave.plist")
let data = try PropertyListEncoder().encode(readData)
try? data.write(to: dataFilePaths, options: Data.WritingOptions.atomic)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.