ios - [NSObject : AnyObject]?' does not have a member named 'subscript' error in Xcode 6 beta 6 -


i used below couple of code lines frame of keyboard when shown on screen. i've registered uikeyboarddidshownotification notification.

func keyboardwasshown(notification: nsnotification) {     var info = notification.userinfo     var keyboardframe: cgrect = info.objectforkey(uikeyboardframeenduserinfokey).cgrectvalue() } 

this used work in beta 5. downloaded latest xcode 6 version beta 6 , error occurred @ second line.

'[nsobject : anyobject]?' not have member named 'objectforkey'

after googling, came across this solution. , changed so,

var keyboardframe: cgrect = (info[uikeyboardframeenduserinfokey] nsvalue).cgrectvalue() 

but seems that's outdated now. because error now.

'[nsobject : anyobject]?' not have member named 'subscript'

i can't figure out error or how resolve it.

as mentioned in xcode 6 beta 6 release notes, large number of foundation apis have been audited optional conformance. these changes replace t! either t? or t depending on whether value can null (or not) respectively.

notification.userinfo optional dictionary:

class nsnotification : nsobject, nscopying, nscoding {     // ...     var userinfo: [nsobject : anyobject]? { }     // ... } 

so have unwrap it. if know userinfo not nil can use "forced unwrapping":

var info = notification.userinfo! 

but note crash @ runtime if userinfo nil.

otherwise better use optional assignment:

if let info = notification.userinfo {     var keyboardframe: cgrect = (info[uikeyboardframeenduserinfokey] as! nsvalue).cgrectvalue() } else {     // no userinfo dictionary present } 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -