gpt4 book ai didi

ios - 条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 'AnyObject - Approach

转载 作者:IT王子 更新时间:2023-10-29 05:36:46 24 4
gpt4 key购买 nike

以下代码抛出一条消息,指出“条件绑定(bind)的初始化程序必须具有可选类型,而不是‘AnyObject’”

    func parseData2(){
var data:NSData?


if let data2 = data {
do {
let details = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)

if let actualDetails = details where actualDetails.isKindOfClass(NSDictionary) {
print("Parse Data")
}

}catch {
print("Error \(error)")
}
}

}

为了解决上述错误,我使用了以下代码。

    func parseData2(){
var data:NSData?


if let data2 = data {
do {
let details:AnyObject = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)

if let actualDetails:AnyObject = details where actualDetails.isKindOfClass(NSDictionary) {
print("Parse Data")
}

}catch {
print("Error \(error)")
}
}

}

有没有比上述方法更好的方法,否则我的代码可能会崩溃?

考虑到nil 检查类型检查,然后是类型转换检查,我还想添加一段代码。 Swift 提供了很大的灵 active 但解决问题有点困难的原因。假设我有一个字典,cityDetails,我正在尝试获取 self.cityZipCode 和 self.cityIdentifier 的数据,它们是可选的,定义为 var cityZipCode:Int?和 var cityIdentifier:Int?

if let cityBasic = cityDetails["basicDetails"] where 
cityBasic!.isKindOfClass(NSDictionary) {

self.cityZipCode = (cityBasic as! NSDictionary)["zip"].integerValue ?? 0

self.cityIdentifier = (cityBasic as! NSDictionary)["cityId"].integerValue ?? 0

}

最佳答案

无需解包 try 的结果。它不是可选的。您确实需要将 try 的结果转换为 NSDictionary。使用 as? 对其进行向下转换。

最佳实践:完全访问返回的错误以实现良好的错误处理

func parseData2(){
var data:NSData?

if let data2 = data {
do {
let details = try NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)

if let detailsDict = details as? NSDictionary {
print("Parse Data")
} else if let detailsArray = details as? NSArray {
print("array")
}

} catch {
print("Error \(error)")
}
}
}

快速而肮脏:错误处理不适合我!

func parseData2(){
var data:NSData?

if let data2 = data {

let details = try? NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments)

if let detailsDict = details as? NSDictionary {
print("Parse Data")
} else {
print("details might be nil, or not an NSDictionary")
}
}
}

Bad Ass 模式:崩溃是一种特性

func parseData2(){
var data:NSData?

if let data2 = data {

let details = try! NSJSONSerialization.JSONObjectWithData(data2, options: .AllowFragments) as! NSDictionary

}
}

关于多个解包的一些额外信息:将下面的代码放在 Playground 上。

struct SomeStruct {
var anOptional : Int?
init() {
}
}

func unwrapWithIfLet() {

if let unWrappedStruct = myStruct, let unWrappedSomething = unWrappedStruct.anOptional {
print("multiple optional bindings succeeded")
// both unWrappedStruct and unWrappedSomething are available here
} else {
print("something is nil")
}
}

func unwrapWithGuard() {

guard let unWrappedStruct = myStruct, let unWrappedSomething = unWrappedStruct.anOptional else {
print("something is nil")
return
}
print("multiple optional bindings succeeded")
// both unWrappedStruct and unWrappedSomething are available here
}


var myStruct : SomeStruct?

//unwrapWithGuard()
//unwrapWithIfLet()

myStruct = SomeStruct()
myStruct!.anOptional = 1

unwrapWithGuard()
unwrapWithIfLet()

关于ios - 条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 'AnyObject - Approach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33381554/

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