当我归档data
时,会出现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
console
日志:
fatal error: unexpectedly found nil while unwrapping an Optional value
userStatic
的一部分,archive
和unarchive
数据为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 :(得分:0)
在View.(VISIBILITY)
方法中
试试这个
init(coder aDecoder: NSCoder)
我认为它会对你有所帮助