Reputation: 17585
I have searched and used
UIDevice.currentDevice().instancesRespondToSelector(Selector("userInterfaceIdiom"))
But couldn't worked for me. Show error as
'UIDevice' doesn't have member named 'instancesRespondToSelector`
Also tried this
UIDevice.instancesRespondToSelector(Selector("userInterfaceIdiom"))
But got error as
'UIDevice' doesn't have member named 'userInterfaceIdiom'
But UIDevice belongs to NSObject which have member instancesRespondToSelector
? Please point out my mistake or suggest correct way to do it?
Upvotes: 1
Views: 1094
Reputation: 126117
First, you don't need this particular selector check. The recommendation of calling [[UIDevice] currentDevice] userInterfaceIdiom]
only after a respondsToSelector
check dates back to iOS 3.x. At that time, iPhones running iOS 3.1 didn't have the userInterfaceIdiom
method — only iPads running iOS 3.2 did.
Not only is nobody trying to support iOS 3.x anymore, you can't deploy to it with Swift. All versions of iOS that you can target from Xcode 6 (whether you code in Swift or ObjC) support calling userInterfaceIdiom
, and for that matter, so do all versions you can target from Xcode 5.
Second, there's no need to write Selector("someSelectorName")
. The Selector
type adopts the StringLiteralConvertible
protocol, so anywhere you see an API that takes a Selector
, you can pass a string literal.
So, you can write either:
UIDevice.instancesRespondToSelector("userInterfaceIdiom")
or
UIDevice.currentDevice().respondsToSelector("userInterfaceIdiom")
But you shouldn't...
Finally, it's generally a bad idea to use userInterfaceIdiom
to make layout decisions — for one thing, it leads to the wrong layout decisions whenever you want to use the same view controller both as a portrait-full-screen view on iPhone and as the master view in a split view on iPad.
In iOS 8, the preferred way for a view controller to adapt to the frame it's presented in is to use traits. In your view controller code, write something like this:
if self.traitCollection.horizontalSizeClass == .Compact {
// lay out for iPhone portrait or iPad master-split / popover
} else {
// lay out for iPad detail-split / fullscreen
}
You can get more info about designing adaptive view controllers in WWDC 2104 session 216: Building Adaptive Apps with UIKit.
Upvotes: 2
Reputation: 866
I think it should be like this:
UIDevice.userInterfaceIdiom?()
Upvotes: 0
Reputation: 17585
From @holex hint, I realize answer as
UIDevice.respondsToSelector(Selector("userInterfaceIdiom"))
Upvotes: 1
Reputation: 24041
the instancesRespondToSelector
is still a class method and defintely not an instance method:
UIDevice.instancesRespondToSelector(Selector("userInterfaceIdiom"))
UPDATE#1
because the userInterfaceIdiom
is a property of the instance only:
UIDevice.currentDevice().respondsToSelector(Selector("userInterfaceIdiom"))
Upvotes: 4