简体   繁体   中英

How to set the 'UIDevice.currentDevice()' for object of notification?

When I add a notification for device orientation Changed, like this:

NSNotificationCenter.defaultCenter().addObserver(self,
        selector: Selector(layoutPages),
        name: UIDeviceOrientationDidChangeNotification as String!,
        object: UIDevice.currentDevice())

I get a error 'UIDevice is not identical to anyobject', so how to fixed it?

There is a problem with your second argument, it should be a string. Also, upcasting the UIDevice to AnyObject will help:

NSNotificationCenter.defaultCenter().addObserver(self,
    selector: Selector("layoutPages"),
        name: UIDeviceOrientationDidChangeNotification as NSString!,
      object: UIDevice.currentDevice() as AnyObject)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM