取消归档数据报错:线程1:EXC_BREAKPOINT(代码= EXC_BREAKPOINT,子码= XXXX)

问题描述:

当我存档我data,有来的issue,我从来没有遇见过:取消归档数据报错:线程1:EXC_BREAKPOINT(代码= EXC_BREAKPOINT,子码= XXXX)

The issue

thread信息:

0xda5434 <+120>: bl  0xc7a6f0     
; function signature specialization <preserving fragile attribute, 
Arg[1] = [Closure Propagated : reabstraction thunk helper from 
@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) 
->() to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> 
(@out()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) 
->()]> of generic specialization <preserving fragile attribute, 
()> of Swift.StaticString.withUTF8Buffer <A> ((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A 
-> 0xda5438 <+124>: trap 

The console log:

fatal error: unexpectedly found nil while unwrapping an Optional value 

userStatic一部分,archiveunarchive数据为userStatic

import UIKit 

enum UserType: Int { 
case terant // 商户 1 
case normalUser // 普通用户 2 
case normalUserFinancialer // 普通用户的财务 3 
} 

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

//static let sharedInstance = UserStaticSwift() 

var islogin: Bool = false // 是否登录 
// 用户信息 
var type:UserType? { 

    didSet { 

     if type == .terant { 
      forOcType = 1 
     }else if type == .normalUser { 

      forOcType = 2 
     }else { 
      forOcType = 3 
     } 
    } 
} 
var forOcType:Int = 0 // 类型,为了方便oc调用 
var username:String = "" 
var password:String = "" 
var userId: String = "" // 用户id 

// swiftSharedInstance is not accessible from ObjC 
class var swiftSharedInstance: UserStaticSwift { 
    struct UserStatic { 
     static let instance = UserStaticSwift() 
    } 
    return UserStatic.instance 
} 

// the sharedInstance class method can be reached from ObjC 
class func sharedInstance() -> UserStaticSwift { 
    return UserStaticSwift.swiftSharedInstance 
} 

... 
// MARK: - coder 
required init(coder aDecoder: NSCoder) { 
    super.init() 
    /* 最基本 */ 
    islogin = aDecoder.decodeObject(forKey: "islogin") as! Bool 
    //type = aDecoder.decodeObject(forKey: "type") as? UserType 
    // type = UserType(rawValue: aDecoder.decodeObject(forKey: "type") as! Int)! 
    /*if let type = UserTypeaDecoder.decodeObjectForKey("namesListArray") as? [String] { 
     namesListArray = namesList 
    } else { 
     namesListArray = [String] 
    }*/ 
    if let temp_type = aDecoder.decodeObject(forKey: "type") as? Int { 

     type = UserType(rawValue: temp_type) 
    }else { 

     type = nil 
    } 
    forOcType = aDecoder.decodeObject(forKey: "forOcType") as! Int 
    username = aDecoder.decodeObject(forKey: "username") as! String 
... 

func encode(with aCoder: NSCoder) { 

    /* 基础 */ 
    aCoder.encode(islogin, forKey: "islogin") 
    aCoder.encode(type!.rawValue, forKey: "type") 
    aCoder.encode(forOcType, forKey: "forOcType") 
    aCoder.encode(username, forKey: "username") 
    aCoder.encode(password, forKey: "password") 
    aCoder.encode(userId, forKey: "userId") 
.... 
// 增加信息 
func addUserInfo(type:UserType, dic:[String: Any], closure:(Void)->Void) { 

    // 商户 
    if type == .terant { 

     self.ter_status = UtilSwift.getIntFromAny(dic["status"] ?? "") 
     self.ter_logo = UtilSwift.getStrFromAny(dic["logo"]) 
     self.ter_orgPhoto = UtilSwift.getStrFromAny(dic["orgPhoto"]) 
... 

    closure() 
} 

的地方unarchive数据:

let paths:NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray 
    let path = paths.firstObject 
    let homePath = "\(path!)/\(Global.archive_userStaticData)" 

    print("unarchive_path:\(homePath)") 

    var userStatic:UserStaticSwift? = nil 

    if let loaded:Any = NSKeyedUnarchiver.unarchiveObject(withFile: homePath){ 

     userStatic = (loaded as? UserStaticSwift)! 
    }else { 

    } 

    if userStatic == nil { 

     return 
    } 

有人出现同样的问题?我很笨拙到issue,永远不会遇到这些信息。

+0

结帐我的答案[http://stackoverflow.com/questions/41116832/when-archive-object-in-所述-FUNC-encodewith-acoder-nscoder-方法-坠毁-与/ 41116908#41116908](http://stackoverflow.com/questions/41116832/when-archive-object-in-the-func-encodewith-acoder-nscoder -method-crash/with/41116908#41116908) –

+0

@jignesh Vadadoriya它不是一回事,这个问题是'unarchive'造成的,那是因为存档。 – aircraft

+0

结帐我的回答 –

init(coder aDecoder: NSCoder)方法 试试这个

required init(coder aDecoder: NSCoder) { 

    islogin = aDecoder.decodeBool(forKey: "islogin") 
    forOcType = aDecoder.decodeInteger(forKey: "forOcType") as! Int 
    if let temp_type = aDecoder.decodeInteger(forKey: "type") as? Int { 

     type = UserType(rawValue: temp_type) 
    }else { 

     type = nil 
    } 

    username = aDecoder.decodeObject(forKey: "username") as! String 
    password = aDecoder.decodeObject(forKey: "password") as! String 
    userId = aDecoder.decodeObject(forKey: "userId") as! String 


} 

我认为这将帮助你

+0

我在我的代码中这样做,你可以在我的问题的代码中看到它。 – aircraft

+0

我已添加decodeInteger这不是在你的问题。 –

+0

为Bool值我已添加decodeBool,fir Int值decodeInteger –