0

** the readData.plist is:**


  <?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>


** The following is code:**


        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 I also use FileManager and several examples here to save data, some display "Successfully write", but the file does't change.

Blockquote


I am an iOS developer since Feb 12, 2020

Dapple
  • 1
  • 4
  • I read from .plist is no problem. – Dapple Feb 16 '20 at 03:34
  • You cannot write into the application bundle. It's read-only. And `NSCoding` is discouraged in Swift. Use `PropertyListEncoder` And never use `NSMutable...` collection types in Swift. All native types are mutable by default as `var`iable. And please conform to the naming convention and name variables and methods with starting lowercase letter. – vadian Feb 16 '20 at 06:17
  • Thank your advice of PropertyListEncoder and code writing method. I changed my code, but it still does't work ``` 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) ``` – Dapple Feb 17 '20 at 05:45
  • You have to create a struct with the 3 properties `currentChapter`, `currentLine` and `nameOfBook` and encode this to get a property list with the corresponding keys. – vadian Feb 17 '20 at 05:51
  • Could you tell me why I have to use a struct? but finally, I have to execute the specific function. – Dapple Feb 18 '20 at 18:47
  • Because it's more convenient, more efficient and *swiftier*. – vadian Feb 18 '20 at 18:51
  • Thank you very much , I got it. because I am new, so I write code in playground, then move to project use class or struct, but now my task to solve that question. – Dapple Feb 18 '20 at 20:10

2 Answers2

0

I think the file is not in a writable location, so you should create it in the documents directory. Then, it has to be read from the documents directory.

ChickenMinh
  • 39
  • 1
  • 2
  • 11
  • Thank you. I tried PropertyListEncoder() method under playground and project, it doesn't work. – Dapple Feb 17 '20 at 05:48
  • That's not what I'm thinking about. Read [this](https://stackoverflow.com/questions/25100262/save-data-to-plist-file-in-swift) – ChickenMinh Feb 17 '20 at 05:59
  • it doesn't work, because I am new to develop. my friend helps me ,but he can not find the reason. – Dapple Feb 18 '20 at 18:49
0

Blockquote

I used the following code, but it does't work too.

Blockquote


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)
Dapple
  • 1
  • 4