更新到 Xcode 10 使得值被“Optional()"包裹起来; [英] Updating to Xcode 10 made value wrapped with "Optional()"

查看:52
本文介绍了更新到 Xcode 10 使得值被“Optional()"包裹起来;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我从 Xcode 9 切换到 Xcode 10 来开发我的 iOS 应用程序.我注意到的第一件事是,当我尝试打印出一个变量时,值被可选 包裹,而 在 Xcode 9 中它从未发生过.例如这里是我测试的代码.

let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as!细绳让 build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as!细绳让参数 = ["branch_id": loginData.profile.branchId,"version": "\(version).\(build)","os_name": "ios",user_id":loginData.uid]打印(参数)

输出如下:

<块引用>

["os_name": "ios",branch_id":可选(2"),版本":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(entityName: "User")让 fetchResults = 尝试 context.fetch(request) as?[NSManagedObject]让 loginData = fetchResults![0]让 profile = loginData.value(forKey: "profile") as!NS管理对象self.profile = Profile()self.profile.branchId = profile.value(forKey: "branchId") as?细绳

解决方案

使用 Optional Unwrapping with if-let 语句

let request = NSFetchRequest(entityName: "User")if let fetchResults = try context.fetch(request) as?[NSManagedObject]{让 loginData = fetchResults[0]让 profile = loginData.value(forKey: "profile") as!NS管理对象self.profile = Profile()如果让 branchId = profile.value(forKey: "branchId") 作为?细绳{self.profile.branchId = branchId}}if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as?字符串,让 build = Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as?细绳{让 branchId = loginData.profile.branchId ??"让 branchId = loginData.uid ??"让参数 = ["branch_id": branchId,"version": "\(version).\(build)","os_name": "ios",user_id":login_tty.uid]打印(参数)}

<块引用>

永远不要使用强制解包,我的意思是!直接,可能会导致崩溃,而是使用 if letguard let

安全地解包

So recently I switch from Xcode 9 to Xcode 10 for my iOS app development. the first thing I noticed that when I tried to print out a variable, the value is wrapped with optional, while in Xcode 9 it never happened. for example here is the code that I test.

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)

and the output was like :

["os_name": "ios", "branch_id": Optional("2"), "version": "1.5.8.3", "user_id": Optional("1141")]

I've tried to force unwrap the code with exclamation mark

"branch_id": loginData.profile.branchId!,

or even better with coalescing operator

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

It works, but I have like, 30+ lines of code with the same problem, do I need to do it one by one? Or is there a way to change this behaviour?

FYI I'm using Swift 4 for my project. Edit : FYI this was tested on iOS 12, while before in Xcode 9 was tested in iOS 11

Edit:

To answer matt comment asking info about where loginData.profile.branchId come from, here it is.

So, the data is fetched from data model, and I use this code to fetch it:

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

解决方案

Use Optional Unwrapping with if-let statement

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

Never use force unwrapping, i mean ! directly, it may result in crash, instead safely unwrap using if let and guard let

这篇关于更新到 Xcode 10 使得值被“Optional()"包裹起来;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆