Miscellaneous
Assignment analysis
Objective-C
@implementation MyClass
-(void)test {
NSString *foo = @"FOO";
NSString *bar = @"BAR";
foo = @"baz";
// ...
return foo;
}
@end
Swift
class MyClass {
func test() {
var foo:String! = "FOO"
let bar:String! = "BAR"
foo = "baz"
// ...
return foo
}
}
NULL check idiom
Objective-C
@implementation MyClass
-(void)test:(NSString *)str {
if(!str) {
NSLog(@"str is NULL");
}
}
@end
Swift
class MyClass {
func test(str:String!) {
if (str == nil) {
NSLog("str is NULL")
}
}
}
class Keyword
Objective-C
void test() {
NSString *str;
// Get type from class.
[NSString class];
NSString.class;
// Get dynamic type from instance.
[str class];
str.class;
}
Swift
func test() {
var str:String!
// Get type from class.
String.self
String.self
// Get dynamic type from instance.
str.dynamicType
str.dynamicType
}
isKindOfClass
Objective-C
void test(id obj) {
if([obj isKindOfClass:[NSString class]]) {
// Do something
}
}
Swift
func test(obj:AnyObject!) {
if (obj is NSString) {
// Do something
}
}
instancetype
Objective-C
@implementation Foo
-(instancetype)method {
// ...
return self;
}
@end
Swift
class Foo {
func method() -> Self {
// ...
return self
}
}
@available
Objective-C
@implementation MyClass
-(void)test {
if(@available(iOS 10.0, *)) {
// >= iOS10
} else {
// <= iOS9
}
}
@end
Swift
class MyClass {
func test() {
if #available(iOS 10.0, *) {
// >= iOS10
} else {
// <= iOS9
}
}
}