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

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -