如何投射AnyObject?在Swift 1.2中使用String [英] How to cast AnyObject? to String in Swift 1.2

查看:99
本文介绍了如何投射AnyObject?在Swift 1.2中使用String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切都很好,直到我下载了新版本的Xcode 6.3和Swift 1.2。

Everything good until, I've downloaded the new version of Xcode 6.3 and Swift 1.2.

但是当我再次编译我的应用程序时,出现了很多错误。我已经处理了大部分问题,但是有一个特定的错误,我无法弄清楚解决方案

But when I compile my app again, a lots of error came out. I've dealt with most of them but there is a specific error that I couldn't figure it out the solutions

错误是AnyObject?无法转换为String ?

The error is "AnyObject? is not convertible to String?"

在更新Xcode之前,这绝对没问题:

Before updating Xcode this was absolutely fine :

let firstName = self.currentUser["firstName"] as? String


推荐答案

我已经找到了解决方案

let firstName = self.currentUser?["firstName"] as? String
if let firstname = firstName {
     // Continue
} else {
    // Handling the error.
}

这篇关于如何投射AnyObject?在Swift 1.2中使用String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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