Thursday, 15 January 2015

swift - Size classes - traitCollectionDidChange crashes with nil value -



swift - Size classes - traitCollectionDidChange crashes with nil value -

i have swift project storyboard using size classes, viewcontroller conforms uitraitenvironment protocol , have implemented function traitcollectiondidchange.

when first launch app traitcollectiondidchange called seems reasonable - allows developers handle first size class presented application, previoustraitcollection variable passed nil. can't check value nil not optional, , accessing in way causes crash. changing parameter optional in function declaration causes build error, complaining have not implemented required methods in protocol.

for have managed work around creating separate function takes optional variable of same type can check against, know why happening.

if utilize @ all, here function overriding

override func traitcollectiondidchange(previoustraitcollection: uitraitcollection) { // accessing previoustraitcollection causes crash }

can shed lite on this?

you have declare type of argument optional, because accessing forces compiler seek , unpack address causes crash. replace with:

override func traitcollectiondidchange(previoustraitcollection: uitraitcollection?) { // accessing previoustraitcollection causes crash }

swift size-classes

No comments:

Post a Comment