gpt4 book ai didi

ios - 从模型的属性中获取可选值

转载 作者:行者123 更新时间:2023-11-30 12:51:04 25 4
gpt4 key购买 nike

在我的代码中:

let userStatic:UserStaticSwift = UserStaticSwift.sharedInstance() // UserStaticSwift save the info

if userStatic.nor_businessName != "" {

self.nor_name.text = userStatic.nor_businessName
self.nor_vauth.isHidden = false
}else {
self.nor_name.text = "un bind terant"
self.nor_vauth.isHidden = true
}

结果出乎意料:

self.nor_name.text将设置为:"Optional(the real value)" ,但我想设置"the real value" .

但是userStatic的属性(property)是 optional ,我只想要值 the real value ,不是下面的:

(lldb) po userStatic.nor_businessName
"Optional(the real value)"

如何取出该值,而不是 Optional(value)

<小时/>

编辑-3

显示更多信息的附加信息:

@objc class UserStaticSwift:NSObject, NSCoding :

self.nor_status = UtilSwift.getIntFromAny(dic["status"] as Any)
self.nor_phone = UtilSwift.getStrFromAny(dic["phone"])
self.nor_name = UtilSwift.getStrFromAny(dic["name"])
self.nor_definition = UtilSwift.getStrFromAny(dic["definition"])
self.nor_log = UtilSwift.getStrFromAny(dic["logo"])
self.nor_businessName = UtilSwift.getStrFromAny(dic["businessName"]) // attention, is there if I use the `dic["businessName"] as Any`, then will get the Optional value, so I pass the `dic["businessName"]`

UtilSwift :

class func getStrFromAny(_ any:Any?) -> String {

if let nonNil = any, !(nonNil is NSNull) {

return String(describing: nonNil)
}
return ""
}
<小时/>

最佳答案

试试这个(在可选元素处添加 !):

po userStatic.nor_businessName!

更新:

如果上述方法不适合您,那么您应该尝试以下方法:

guard let value = userStatic.nor_businessName  else {
print("fails");
return;
}

self.nor_name.text = value;

关于ios - 从模型的属性中获取可选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com