I have been using Objective-C for years and I am now making my first steps using Swift.
In my existing projects I have been using quite a few different macros like
#define IS_IPAD ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
#define IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
Since Swift does not support such macros Apple recommends to use functions and generics instead. This is also the solution which is recommended in other threads (e.g. here).
So I could simply create some Tools
class and add the macros as methods to it:
class Tools {
static func IS_IPAD() -> Bool {
return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
}
}
class OtherClass {
func doSomething() {
if (Tools.IS_IPAD())
...
}
}
However, the examples I have found seem to use functions without any class, like in the thread linked above:
func FLOG(message:String, method:String = __FUNCTION__) {
println("\(method): \(message)")
}
FLOG("Illegal value: \(value)")
Since I am new to Swift I wonder if this is only to keep the examples short or if it is possible to define and use such macro like function without a class.