In my singleton
class, I have a swift
enum here:
import UIKit
enum UserType {
case terant //
case normalUser //
case normalUserFinancialer //
}
@objc(UserStaticSwift)
class UserStaticSwift:NSObject, NSCoding {
The reported error
:
With the console
log:
libc++abi.dylib: terminating with uncaught exception of type NSException
In the 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")
The code
here 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 debug
when archiveRootObject
:
The console
log:
(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 have tested in simulator
and device
, in the simulator
the issue not exist, in real device
the issue comes out.
Try this for your Question
For More Information
Let Discuss this problem in details
For example, this is my Enum:
And this is my Object which is child of
NSObject
When you call
NSKeyedArchiver.archiveRootObject(::)
it will callfunc encode(with aCoder: NSCoder)
method and convert yourNSObject
to Data And when you try to Unarchive your object it will callinit(coder aDecoder: NSCoder)
method and convert Data toNSObject
by using Key.But in
Enum
case you can not encodeenum
directly B'Coz it is User Define data type butrawValue
is must be inbuilt data type like Int, String,Float.....So on. that's why when you try to encodeenum
you need to userawValue
I Hope you will get point.