Greg Robertson Greg Robertson - 1 year ago 107
iOS Question

How to save [[Bool]] in Swift (Encode for archive array of Boolean arrays)

I am having no difficulty archiving Int, String, Bool and Date but archiving an array of Boolean arrays is not working, here is a portion of the code:

class PlayerInfo: NSObject, NSCoding
public var score: Int = 0
public var lastPlayed: Date = Date()
public var visited: [[Bool]] = []
public var ringFound: Bool = false

init(dictionary: [String : AnyObject])
score = dictionary[ArchiveKeys.Score] as! Int
lastPlayed = dictionary[ArchiveKeys.LastPlayed] as! Date
visited = dictionary[ArchiveKeys.Visited] as! [[Bool]]
ringFound = dictionary[ArchiveKeys.RingFound] as! Bool

func encode(with archiver: NSCoder)
archiver.encode(score, forKey: ArchiveKeys.Score)
archiver.encode(lastPlayed, forKey: ArchiveKeys.LastPlayed)
archiver.encode(visited, forKey: ArchiveKeys.Visited)
archiver.encode(ringFound, forKey: ArchiveKeys.RingFound)

required init(coder unarchiver: NSCoder)
score = unarchiver.decodeInteger(forKey: ArchiveKeys.Score)
lastPlayed = unarchiver.decodeObject(forKey: ArchiveKeys.LastPlayed) as! Date
visited = unarchiver.decodeObject(forKey: ArchiveKeys.Visited) as! [[Bool]]
ringFound = unarchiver.decodeBool(forKey: ArchiveKeys.RingFound)

class func retrieveCurrentGame() -> PlayerInfo!
let filePath: String = retrievePreviousGameFilePath(fileName: currentGameName)

if let game = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? PlayerInfo
print("retrieving previous game - found a game for previous day")
return game
print("retrieving previous game - can't find game data")
return nil

class func retrievePreviousGameFilePath(fileName: String) -> String
let kFileNameExtension: String = ".game"

let fileNameWithExtension: String = fileName + kFileNameExtension
let fileManager = FileManager.default
let url = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
print("url= " + (url.appendingPathComponent(fileNameWithExtension)?.path)!)
return (url.appendingPathComponent(fileNameWithExtension)?.path)!


public func saveCurrentGame()
let fileName: String = currentGameName + ".game"
let fileManager = FileManager.default
let url = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
print("url= " + (url.appendingPathComponent(fileName)?.path)!)
let filePath: String = (url.appendingPathComponent(fileName)?.path)!
NSKeyedArchiver.archiveRootObject(self, toFile: filePath)


I am unable to retrieve the [[Bool]] visited with my code. Is there a better way to encode?


I revised the encoding to:

let visitedData: Data = NSKeyedArchiver.archivedData(withRootObject: visited)
archiver.encode(visitedData, forKey: ArchiveKeys.Visited)
// archiver.encode(visited, forKey: ArchiveKeys.Visited)

and decoding to:

let visitedData: Data = unarchiver.decodeObject(forKey: ArchiveKeys.Visited) as! Data
visited = NSKeyedUnarchiver.unarchiveObject(with: visitedData) as! [[Bool]]
// visited = unarchiver.decodeObject(forKey: ArchiveKeys.Visited) as! [[Bool]]

But this still does not work?

Any other ideas?

Answer Source

that simple example is pretty much a working solution for [[Bool]], you may take a look on that and check it how that works.

class MyClass: NSObject, NSCoding {

    public var boolArray: [[Bool]]? = nil

    // MARK: Init

    override init() {
        self.boolArray = [[true, false, false], [false, true, false], [true, true, false]] // or any random sequece...
        debugPrint("It was inited.")

    // MARK: - Archiving / Unarchiving

    func archived() -> Data {
        return NSKeyedArchiver.archivedData(withRootObject: self)

    class func unarchived(fromData data: Data) -> MyClass? {
        return NSKeyedUnarchiver.unarchiveObject(with: data) as? MyClass

    // MARK: - <NSCoding>

    private enum Key {
        case boolArray

    func encode(with aCoder: NSCoder) {
        aCoder.encode(self.boolArray, forKey: String(describing: Key.boolArray))
        debugPrint("It was encoded.")

    required init?(coder aDecoder: NSCoder) {
        self.boolArray = aDecoder.decodeObject(forKey: String(describing: Key.boolArray)) as? [[Bool]]
        debugPrint("It was decoded.")


and it is time to test it:

let myClass = MyClass()

let archivedData: Data = myClass.archived()
// do something with the data here... e.g. permanent storing
// ...

// ...
// restore back the data here from e.g. background storage device
let unarchivedObject: MyClass = MyClass.unarchived(fromData: archivedData)


and the console should look like:

"It was inited"
Optional([[true, false, false], [false, true, false], [true, true, false]])
"It was encoded."
"It was decoded."
Optional([[true, false, false], [false, true, false], [true, true, false]])

which means after unarchiving you can get back your values in one piece.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download