swift - Convert dictionary containing `Any?` to `AnyObject`? -
i'm looking straightforward way of converting dictionary of type [string : any?]
dictionary of [string: anyobject]
. loop through elements individually, seems 'wrong' me.
if try cast original dictionary, crash.
let dict:[string : any?] = ["something" : "else"] // crashes fatal error: 'can't unsafebitcast // between types of different sizes' let newdict = dict as? [string: anyobject]
looping correct, , swift encourages this. efficient , swift-like (*) approach is:
var result = [string: anyobject]() (key, value) in dict { if let v = value as? anyobject { result[key] = v } }
(*) isn't "swift-like" because includes anyobject
, should never part of non-temporary data structure. proper swift implementation convert anyobject
real type.
note crash not appropriate , should open radar (bugreport.apple.com). as?
should never crash this. should either nil
or compiler failure.
Comments
Post a Comment