NSKeyArchiver

How do I use NSKeyArchiver and NSKeyunArchiver to backup and restore an array of floats? Is there any sample code anywhere?

Answered by Claude31 in 331127022

I only knew there was a problem because I couldn't read the second file back.

So what is the situation ?


- do you write both files and get succes and success2 true ?

- Can you now read both files or not ?


Note: it normal you got it true even when you overwrote the first file.


Why don't you have a symmetric hanling for reading both ? The second is inside the if let of the first.

I would better write like this (and instrument the code to check what's going on.

Thanks to report result:


        let theURL = getDocumentsDirectory().appendingPathComponent("tarfile")
        if let data = NSMutableData(contentsOf: theURL) {
            print("data read on", theURL)
            let unarchiver = NSKeyedUnarchiver(forReadingWith: data as Data)
            if let readArray = unarchiver.decodeObject(forKey: "starray") as? [Float]  {
                unarchiver.finishDecoding()
                tarray = readArray
                print("tarray read ", tarray)
            }
        }               // MOVE CLOSING CURLY BRACE
       
        let theURL2 = getDocumentsDirectory().appendingPathComponent("tipfile")
        if let data2 = NSMutableData(contentsOf: theURL2) {
            print("data2 read on", theURL2)
            let unarchiver2 = NSKeyedUnarchiver(forReadingWith: data2 as Data)
            if let readArray2 = unarchiver2.decodeObject(forKey: "stipray") as? [Float]  {
               unarchiver2.finishDecoding()
                tipray = readArray2
                print("tipray read ", tipray)
            }
        }

Yes that's correct but for some reason the second write never overwrote the first file. I was getting TRUE on both writes but only the first one was correct so I thought there was a problem with the read. I was always able to read the first file that's how I know it was never overwritten. I never got the URL2 was not used error.


let theURL = getDocumentsDirectory().appendingPathComponent("tarfile")

let data = NSMutableData()

let archiver = NSKeyedArchiver(forWritingWith: data)

archiver.encode(tarray, forKey: "starray")

archiver.finishEncoding()

let success = data.write(to: theURL, atomically: true)

let theURL2 = getDocumentsDirectory().appendingPathComponent("tipfile")

let data2 = NSMutableData()

let archiver2 = NSKeyedArchiver(forWritingWith: data2)

archiver2.encode(tipray, forKey: "stipray")

archiver2.finishEncoding()

let success2 = data.write(to: theURL2, atomically: true)

First write: let success = data.write(to: theURL, atomically: true)

Second write: let success2 = data.write(to: theURL2, atomically: true)

Second write should have been data2.write(to: theURL2, atomically: true). I put the 2 after data. Once I did that both reads worked.

Yes that's correct. I wasn't able to read the second file until I corected this statement.

data.write(to: theURL2, atomically: true) to

data2.write(to: theURL2, atomically: true)

Great.


But please, mark the real correct answer, not your own comment.

NSKeyArchiver
 
 
Q