首页 > 解决方案 > 如何在 Swift 中为 NSObject 模型变量添加值?

问题描述

我创建了单独的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 我添加了我需要在 ProfileViewController 中显示的 firstName 值如何?

在 RegistrationViewControllerfirstName and phone中,我在 ProfileViewController 中添加了我需要的值:

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
}
}

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

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
}

}

请帮我写代码。

标签: iosswiftmodelviewcontrollernsobject

解决方案


您不能将 firstName 或 phone 设置为 nil 的 userModal。首先你应该创建一个实例,然后你可以通过你的控制器传递它。我们应该逐步更改代码:

class ProfileModel {
    var userId : String?
    var phone : String?
    var firstName : String?
    var email : String?
    var profileImageUrl : String?
    var userAddresses : [ProfileModelUserAddress]?

    init() {}
}

其次,您需要从两个 ViewController 类中访问 ProfileModel 实例。为此,您可以创建一个单例类:

class ProfileManager {
    static var shared = ProfileManager()

    var userModel: ProfileModel?

    private init() {}
}

然后你可以从你的两个 ViewController 中访问它:

class RegistrationViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var firstNameTextField: FloatingTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        let userModel = ProfileModel()
        userModel.firstName = self.firstNameTextField.text
        ProfileManager.shared.userModel = userModel
    }
}

其他风投:

class ProfileViewController: UIViewController {
    @IBOutlet var name: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        if let userModel = ProfileManager.shared.userModel,
            let firstName = userModel.firstName {
            name.text = firstName
        }
    }
}

根据需要修改它。


推荐阅读