Unrecognized selector sent to instance while archiving data (NSCoding)

前端 未结 5 1897
名媛妹妹
名媛妹妹 2020-12-09 09:48
-(void)transformObjects:(NSMutableArray*)array key:(NSString*)key
{
    NSMutableArray* archiveArray = [[NSMutableArray alloc]initWithCapacity:array.count];

    for         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 10:25

    For Swift 4.1 (tested code)

    import UIKit
    import SwiftyJSON
    
    class UserObject: NSObject, NSCoding {
        var username: String? = ""
        var userID: String? = ""
        var user_email: String? = ""
        var name: String? = ""
        var age: String? = ""
        var gender: String? = ""
    
        override init() {
            super.init()
        }
    
        init(dictionary: JSON) {
            //User data initialize...
            username = dictionary["username"].stringValue
            userID = dictionary["iUserID"].stringValue
            user_email = dictionary["email"].stringValue
            name = dictionary["name"].stringValue
            age = dictionary["age"].stringValue
            gender = dictionary["gender"].stringValue
        }
    
        required public init(coder aDecoder: NSCoder) {
            username = aDecoder.decodeObject(forKey: "username") as? String
            userID = aDecoder.decodeObject(forKey: "iUserID") as? String
            user_email = aDecoder.decodeObject(forKey: "email") as? String
            name = aDecoder.decodeObject(forKey: "name") as? String
            age = aDecoder.decodeObject(forKey: "age") as? String
            gender = aDecoder.decodeObject(forKey: "gender") as? String
        }
    
        func encode(with aCoder: NSCoder) {
            aCoder.encode(username, forKey: "username")
            aCoder.encode(userID, forKey: "iUserID")
            aCoder.encode(user_email, forKey: "email")
            aCoder.encode(name, forKey: "name")
            aCoder.encode(age, forKey: "age")
            aCoder.encode(gender, forKey: "gender")
        }
    }
    

    Note : NSCoding protocol is important

提交回复
热议问题