Statement
If Statement
Objective-C
@implementation Foo
-(NSString *)method {
if(Condition) {
return @"OK";
} else {
return @"NG";
}
}
@end
Swift
class Foo {
func method() -> String! {
if Condition {
return "OK"
} else {
return "NG"
}
}
}
Switch Statement
Objective-C
@implementation Foo
-(NSString *)method {
switch(Condition) {
case Apple:
return @"apple";
case Orange:
return @"orange";
default:
return @"else";
}
}
@end
Swift
class Foo {
func method() -> String! {
switch(Condition) {
case Apple:
return "apple"
case Orange:
return "orange"
default:
return "else"
}
}
}
Switch Statement (sequential cases)
Objective-C
@implementation Foo
-(NSString *)method {
switch(Condition) {
case Apple:
case Orange:
return @"fruit";
default:
return @"else";
}
}
@end
Swift
class Foo {
func method() -> String! {
switch(Condition) {
case Apple,
Orange:
return "fruit"
default:
return "else"
}
}
}
For Statement
Objective-C
@implementation Foo
-(void)method {
for(int i=0;i<100;i++) {
NSLog("%d",i);
}
}
@end
Swift
class Foo {
func method() {
for var i:Int=0 ; i<100 ; i++ {
NSLog("%d",i)
}
}
}
For-In Statement
Objective-C
@implementation Foo
-(void)method {
for(int bar in Foo) {
NSLog("%d",bar);
}
}
@end
Swift
class Foo {
func method() {
for bar:Int in Foo {
NSLog("%d",bar)
}
}
}
While Statement
Objective-C
@implementation Foo
-(void)method {
while(flag) {
// Do Something
}
}
@end
Swift
class Foo {
func method() {
while flag {
// Do Something
}
}
}
Do-White Statement
Objective-C
@implementation Foo
-(void)method {
do {
// Do Something
} while(flag);
}
@end
Swift
class Foo {
func method() {
repeat {
// Do Something
} while flag
}
}
Try-Catch Statment
Since Swift's try-catch statement has different semantics from Objective-C's, @try
, @catch
and @finally
keywords are not be converted.
Objective-C
@implementation Foo
-(void)method {
@try {
// ...
}
@catch(Error *err) {
// ...
}
@finally {
// ...
}
}
@end
Swift
class Foo {
func method() {
@try {
// ...
}
@catch(err:Error!) {
// ...
}
@finally {
// ...
}
}
}
Jump and Labeled Statement
Objective-C
@implementation Foo
-(void)method {
for(int i = 0; i < 100; i++) {
if (i == 50) {
goto bar;
}
}
bar:
return;
}
@end
Swift
class Foo {
func method() {
for var i:Int=0 ; i < 100 ; i++ {
if i == 50 {
goto bar
}
}
bar:
return
}
}