更新到用“ Optional()”包装的Xcode 10值

时间:2018-12-04 06:24:30

标签: ios swift xcode xcode10

所以最近我从Xcode 9切换到Xcode 10 ,用于我的iOS应用开发。我注意到的第一件事是,当我尝试打印出一个变量时,值是用可选的包装的,而在Xcode 9中却从未发生过。例如,这是我测试的代码。

let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
let build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String
let parameters = ["branch_id": loginData.profile.branchId, 
                  "version": "\(version).\(build)", 
                  "os_name" : "ios", 
                  "user_id" : loginData.uid]
print(parameters)

输出类似于:

  

[“ os_name”:“ ios”,   “ branch_id”:可选(“ 2”),“ version”:“ 1.5.8.3”,   “ user_id”:可选(“ 1141”)]

我试图用感叹号强制拆开代码

"branch_id": loginData.profile.branchId!

甚至与合并运算符更好

"branch_id": loginData.profile.branchId ?? "0"

它可以工作,但是我喜欢30多个代码行,但都存在相同的问题,我需要一个接一个地做吗?还是有办法改变这种行为?

仅供参考,我在项目中使用Swift 4。 编辑:仅供参考,这已在iOS 12上进行了测试,而之前在Xcode 9中已在iOS 11上进行了测试

编辑:

要回答matt条评论,询问有关loginData.profile.branchId来自何处的信息,

因此,数据是从数据模型获取的,而我使用以下代码来获取数据:

let request = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
let fetchResults = try context.fetch(request) as? [NSManagedObject]
let loginData = fetchResults![0]
let profile = loginData.value(forKey: "profile") as! NSManagedObject
self.profile = Profile()
self.profile.branchId = profile.value(forKey: "branchId") as? String

3 个答案:

答案 0 :(得分:2)

通过if-let语句使用可选的展开功能

let request = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
        if let fetchResults = try context.fetch(request) as? [NSManagedObject]{
            let loginData = fetchResults[0]
            let profile = loginData.value(forKey: "profile") as! NSManagedObject
            self.profile = Profile()
            if let branchId = profile.value(forKey: "branchId") as? String{
                self.profile.branchId = branchId
            }
        }

if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String, let build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String{
            let branchId = loginData.profile.branchId ?? ""
            let branchId = loginData.uid ?? ""
            let parameters = ["branch_id": branchId,
                              "version": "\(version).\(build)",
                "os_name" : "ios",
                "user_id" : login_tty.uid]
            print(parameters)
        }
  

永远不要用力解包!直接导致崩溃   而是使用if letguard let

安全地打开包装

答案 1 :(得分:1)

如果要打印可选值,Xcode将打印与单词optional(“值”)重合的值。

如果要避免这种情况,则必须修改值。 您有3种方法可以做到这一点:

  1. 谨慎的方法,使用警卫队let或if let:

    if let branchId = profile.value(forKey: "branchId") as? String {
          //here branchId != nil
      }
    
      guard let branchId = profile.value(forKey: "branchId") as? String else { return }
    
  2. 力量解开的方式:

    让branchId = profile.value(forKey:“ branchId”)为!字符串

这样,如果值为nil,应用程序将崩溃,因此请小心

  1. 使用默认值:

    让branchId = profile.value(forKey:“ branchId”)为?字符串?? “默认值”

答案 2 :(得分:0)

我最终使用了{strong> coalescing运算符,即?? "",并用以下代码实现了该代码:

var parameters = [String: Any]()
if loginData.profile != nil {
    if loginData.profile.branchId != "" {
        let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
        let build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as! String
        parameters = ["branch_id": loginData.profile.branchId ?? "", 
                      "version" : "\(version).\(build)", 
                      "os_name" : "ios", 
                      "user_id" : loginData.uid ?? ""
                      ]
    }
}
相关问题