When an archive object in func encode code (with aCoder: NSCoder) crashed with fast enumeration in real mode

In my class singleton, I have an listing swifthere:

import UIKit

enum UserType {
    case terant  // 
    case normalUser  // 
    case normalUserFinancialer  // 
}

@objc(UserStaticSwift)
class UserStaticSwift:NSObject, NSCoding {

Reported error:

error

With consolelog:

libc ++ abi.dylib: termination with an uncaught exception of type NSException

In encode:

func encode(with aCoder: NSCoder) {

    /* 基础 */
    aCoder.encode(islogin, forKey: "islogin")
    aCoder.encode(type!, forKey: "type")  // crash here in real device 
    aCoder.encode(forOcType, forKey: "forOcType")
    aCoder.encode(username, forKey: "username")
    aCoder.encode(password, forKey: "password")
    aCoder.encode(userId, forKey: "userId")

codehere I archive my userStatic:

    userStatic.addUserInfo(type: userStatic.type!, dic: userInfoDic, closure: { (void) in

                // success then archive `userStatic`
                let paths:NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
                let path = paths.firstObject
                let homePath = "\(path)/\(Global.archive_userStaticData)"

                let _ = NSKeyedArchiver.archiveRootObject(userStatic, toFile: homePath)

            })

My debugwhen archiveRootObject:

Console

Magazine console:

(lldb) po NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray

error: Couldn't materialize: couldn't get the value of void: extracting data from value failed

error: errored out in DoExecute, couldn't PrepareToExecuteJITExpression
(lldb) po homePath

error: Couldn't materialize: couldn't get the value of void: extracting data from value failed

error: errored out in DoExecute, couldn't PrepareToExecuteJITExpression

I tested in simulatorand device, in the simulatorproblem does not exist, the real deviceproblem comes out.

+4
source share
1 answer

Try this for your question.

func encode(with aCoder: NSCoder) {
    aCoder.encode(type.rawValue, forKey: "type")
}

, Enum:

enum PieceType : Int {
    case empty
    case notEmpty
}

, NSObject

class Piece: NSObject, NSCoding {
    var islogin: Bool
    var type: PieceType
    var username: String!
    var password: String!

    override init() {
        islogin = false
        type = PieceType.empty
        username = ""
        password = ""
    }

    required init(coder aDecoder: NSCoder) {
        islogin = aDecoder.decodeBool(forKey: "islogin")
        type = PieceType(rawValue: aDecoder.decodeObject(forKey: "type") as! Int)!
        username = aDecoder.decodeObject(forKey: "username") as! String
        password = aDecoder.decodeObject(forKey: "password") as! String

    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(islogin, forKey: "islogin")
        aCoder.encode(type.rawValue, forKey: "type")
        aCoder.encode(username, forKey: "username")
        aCoder.encode(password, forKey: "password")

    }

}

NSKeyedArchiver.archiveRootObject(::), func encode(with aCoder: NSCoder) NSObject Data. init(coder aDecoder: NSCoder) NSObject Key.

Enum Enum B'Coz, User Define, rawValue , Int, String, Float..... . Enum rawValue

, .

+8

Source: https://habr.com/ru/post/1663593/


All Articles