无法强制转换类型为';swift.bool.type';斯威夫特·布尔
我现在有一本字典:无法强制转换类型为';swift.bool.type';斯威夫特·布尔,swift,types,boolean,Swift,Types,Boolean,我现在有一本字典: var myfirstcard: [String: Any] = ["thisimagebool": Bool.self] 当我将API调用中的Bool添加到 thisimagebool = apicallbool as! Bool 还有。尝试使用type(of:)它返回thismagebool是一个swift.bool.type 我不明白为什么这个Bool会变成swift.Bool.type而不是swift.Bool 谢谢您的帮助。Bool.self指的是Bool的
var myfirstcard: [String: Any] = ["thisimagebool": Bool.self]
当我将API调用中的Bool添加到
thisimagebool = apicallbool as! Bool
还有。尝试使用type(of:)
它返回thismagebool
是一个swift.bool.type
我不明白为什么这个Bool会变成swift.Bool.type
而不是swift.Bool
谢谢您的帮助。
Bool.self
指的是Bool的类型,而不是实际布尔值的实例
我会先声明字典而不指定值,然后从API获得值后再设置它
// Initialise dictionary but do not assign keys or values.
// Dictionary could also be [String: Bool] type.
var myFirstCard: [String: Any] = [:]
// Assign value from API
myFirstCard["thisImageBool"] = boolFromAPI // true or false
Bool.self
指的是Bool
的元类型。如果Bool
是一个饼干切割器,Bool()
,true
,false
,等等都是类似于该饼干切割器切出的饼干Bool.self
指的是饼干切割器本身。感谢您的快速响应将其更改为Bool(),摆脱了swift.Bool.type我感谢您的帮助最好明确说明您的意思是true
还是false
,而不是Bool()
(这恰好是false
,但不太明显)谢谢你给我展示了一个更好的分配字典的方法,bool.self指的是bool的类型,而不是值