ios - 如何在 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
}
}
请帮我写代码。
解决方案
您不能将 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
}
}
}
根据需要修改它。
推荐阅读
- makefile - 没有规则来制作目标*,*需要
- java - 单元测试 Spring 抛出 BeanNotOfRequiredTypeException
- python - 如何优化下面的代码以更快地运行,我的数据框大小几乎是 100,000 个数据点
- android - 在 android studio 中将实时数据库添加到我的应用程序 - 无法解决:firebase-database-15.0.0
- laravel - 十月CMS后台登录
- c++ - 使用 2 个指针反转链表
- python - Python - 比较包含的两个字典值并显示匹配项
- c++ - C++ OpenCV 进行人脸识别,直到相机被移除
- javascript - 样式属性不适用于带后缀或前缀的输入
- inno-setup - 相对于 WizardForm 位置定位自定义表单