如何在 swift 中将字典分配给 AnyObject
How to assign a Dictionary to AnyObject in swift
我一直在玩 Swift。我在类型方面遇到过多个错误,尤其是在使用 Swift 和我的旧 Objective-C 类 时。这种方法的问题是:我期待一个由 Objective-C.
中的 NSDictionarys 组成的数组
var curArr:[Dictionary<String, AnyObject>] = self.getItemsForCurrentStack()
var arrToReturn:[(Dictionary<String, AnyObject?>)] = []
for obj in curArr{
arrToReturn.append(["image": UIImage(named: obj["imageName"] as! String), "color": UIColor(red:obj["colorDic"]!["red"] as! CGFloat, green:obj["colorDic"]!["green"] as! CGFloat, blue:obj["colorDic"]!["blue"] as! CGFloat, alpha:1.0), "percentage": obj["percantage"]])
}
return arrToReturn
returns Swift 中的词典(NSDictionaries)。但最后一行抛出一个错误:
Dictionary' is not identical to 'AnyObject'
我试过使用 as! [AnyObject]
但这会引发另一个错误:
'AnyObject' is not a subtype of 'Dictionary'
我没有得到第二个错误,因为这不一定是子类型,而是相反。关于如何解决这个问题的任何想法?经过几个小时的谷歌搜索和研究,我没有找到答案。
Dictionary
是 Swift 中的结构,而 AnyObject
是
/// The protocol to which all classes implicitly conform.
根据您要执行的操作,您可能希望在代码中使用 Any
,或者使用 as NSDictionary
.
将字典转换为 NSDictionary
澄清后编辑:
如果从字典本身拆分 append
调用,您会看到更好的错误消息:
因此,问题是您的字典包含一些 Optional
值,但 Optional 是一个结构,不能转换为 Obj-C。您可以通过转换为 UIImage!
和 AnyObject!
(ImplicitlyUnwrappedOptional)或使用 as!
.
来修复
您的代码在 playground 中运行良好,没有编译或运行时错误。这是我为填充 curArr
:
添加的内容
var curArr:[Dictionary<String, AnyObject>] = [
[
"imageName" : "photo.jpg",
"colorDic" : ["red" : 1.0, "green" : 0.0, "blue" : 0.0],
"percentage" : 0.5
]
]
所有这些强制展开我认为你只需要确保 self.getItemsForCurrentStack()
returns 确实是你所期望的。
操场成绩:
[
"percentage": {Some 5.0e+-1},
"image": nil,
"color": {Some r 1,0 g 0,0 b 0,0 a 1,0}
]
我的建议是重构为对象 - 这将使您的代码更具可读性!
在Swift3
code:
var curArr:[Dictionary<String, Any>] = [
[
"imageName" : "photo.jpg",
"colorDic" :[
"red" : 1.0,
"green" : 0.0,
"blue" : 0.0
],
"percentage" : 0.5
]
]
我一直在玩 Swift。我在类型方面遇到过多个错误,尤其是在使用 Swift 和我的旧 Objective-C 类 时。这种方法的问题是:我期待一个由 Objective-C.
中的 NSDictionarys 组成的数组var curArr:[Dictionary<String, AnyObject>] = self.getItemsForCurrentStack()
var arrToReturn:[(Dictionary<String, AnyObject?>)] = []
for obj in curArr{
arrToReturn.append(["image": UIImage(named: obj["imageName"] as! String), "color": UIColor(red:obj["colorDic"]!["red"] as! CGFloat, green:obj["colorDic"]!["green"] as! CGFloat, blue:obj["colorDic"]!["blue"] as! CGFloat, alpha:1.0), "percentage": obj["percantage"]])
}
return arrToReturn
returns Swift 中的词典(NSDictionaries)。但最后一行抛出一个错误:
Dictionary' is not identical to 'AnyObject'
我试过使用 as! [AnyObject]
但这会引发另一个错误:
'AnyObject' is not a subtype of 'Dictionary'
我没有得到第二个错误,因为这不一定是子类型,而是相反。关于如何解决这个问题的任何想法?经过几个小时的谷歌搜索和研究,我没有找到答案。
Dictionary
是 Swift 中的结构,而 AnyObject
是
/// The protocol to which all classes implicitly conform.
根据您要执行的操作,您可能希望在代码中使用 Any
,或者使用 as NSDictionary
.
澄清后编辑:
如果从字典本身拆分 append
调用,您会看到更好的错误消息:
因此,问题是您的字典包含一些 Optional
值,但 Optional 是一个结构,不能转换为 Obj-C。您可以通过转换为 UIImage!
和 AnyObject!
(ImplicitlyUnwrappedOptional)或使用 as!
.
您的代码在 playground 中运行良好,没有编译或运行时错误。这是我为填充 curArr
:
var curArr:[Dictionary<String, AnyObject>] = [
[
"imageName" : "photo.jpg",
"colorDic" : ["red" : 1.0, "green" : 0.0, "blue" : 0.0],
"percentage" : 0.5
]
]
所有这些强制展开我认为你只需要确保 self.getItemsForCurrentStack()
returns 确实是你所期望的。
操场成绩:
[
"percentage": {Some 5.0e+-1},
"image": nil,
"color": {Some r 1,0 g 0,0 b 0,0 a 1,0}
]
我的建议是重构为对象 - 这将使您的代码更具可读性!
在Swift3
code:
var curArr:[Dictionary<String, Any>] = [
[
"imageName" : "photo.jpg",
"colorDic" :[
"red" : 1.0,
"green" : 0.0,
"blue" : 0.0
],
"percentage" : 0.5
]
]