如何在Swift中为NSObject模型变量添加值?

问题描述 投票:0回答:1

我创建了单独的NSObject类,称为ProfileModel

如下所示:

class ProfileModel  : NSObject, NSCoding{

var userId : String!
var phone : String!
var firstName : String!
var email : String!
var profileImageUrl : String!
var userAddresses : [ProfileModelUserAddress]!

// Instantiate the instance using the passed dictionary values to set the properties values
init(fromDictionary dictionary: [String:Any]){
    userId = dictionary["userId"] as? String
    phone = dictionary["phone"] as? String
    firstName = dictionary["firstName"] as? String
    email = dictionary["email"] as? String
    profileImageUrl = dictionary["profileImageUrl"] as? String
}
/**
 * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
 */
func toDictionary() -> [String:Any]
{
    var dictionary = [String:Any]()
    if userId != nil{
        dictionary["userId"] = userId
    }
    if phone != nil{
        dictionary["phone"] = phone
    }
    if firstName != nil{
        dictionary["firstName"] = firstName
    }
    if email != nil{
        dictionary["email"] = email
    }
    if profileImageUrl != nil{
        dictionary["profileImageUrl"] = profileImageUrl
    }
    return dictionary
}

/**
 * NSCoding required initializer.
 * Fills the data from the passed decoder
 */
@objc required init(coder aDecoder: NSCoder)
{
    userId = aDecoder.decodeObject(forKey: "userId") as? String
    userType = aDecoder.decodeObject(forKey: "userType") as? String
    phone = aDecoder.decodeObject(forKey: "phone") as? String
    firstName = aDecoder.decodeObject(forKey: "firstName") as? String
    email = aDecoder.decodeObject(forKey: "email") as? String
    profileImageUrl = aDecoder.decodeObject(forKey: "profileImageUrl") as? String
}
/**
 * NSCoding required method.
 * Encodes mode properties into the decoder
 */
@objc func encode(with aCoder: NSCoder)
{
    if userId != nil{
        aCoder.encode(userId, forKey: "userId")
    }
    if phone != nil{
        aCoder.encode(phone, forKey: "phone")
    }
    if firstName != nil{
        aCoder.encode(firstName, forKey: "firstName")
    }
    if email != nil{
        aCoder.encode(email, forKey: "email")
    }
    if profileImageUrl != nil{
        aCoder.encode(profileImageUrl, forKey: "profileImageUrl")
    }

}
}

在RegistrationViewController中,我添加firstName值,我需要在ProfileViewController中显示如何?] >>

在RegistrationViewController中,我要在ProfileViewController中添加firstName and phone值:

class RegistrationViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var firstNameTextField: FloatingTextField!
var userModel : ProfileModel?

override func viewDidLoad() {
     let userID: String=jsonObj?["userId"] as? String ?? ""
     self.userModel?.firstName = self.firstNameTextField.text
      self.userModel?.phone = phoneTextField.text
}
}

这是name and number中的ProfileViewController,我为什么没有得到名字和电话值?:

class ProfileViewController: UIViewController {
@IBOutlet var name: UILabel!
@IBOutlet var number: UILabel!
var userModel : ProfileModel? 

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    name.text = userModel?.firstName
    number.text = userModel?.phone
}

}

请帮我提供代码。

我创建了一个名为ProfileModel的单独的NSObject类,如下所示:class ProfileModel:NSObject,NSCoding {var userId:String! var phone:字符串! var firstName:字符串! var email:字符串! ...

ios swift model viewcontroller nsobject
1个回答
0
投票

您不能将名字或电话设置为零的userModal。首先,您应该创建一个实例,然后可以将其通过控制器。我们应该逐步更改代码:

© www.soinside.com 2019 - 2024. All rights reserved.