
1. Variables and constants
(1) Variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a variable | |
NSInteger score = 556; | |
// var score = 556 | |
NSString *name = @"Taylor"; | |
// var name = "Taylor" | |
BOOL loggedIn = NO; | |
// var loggedIn = false |
(2) Constant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a constant | |
const NSInteger score = 556; | |
// let score = 556 | |
NSString * const name = @"Taylor"; | |
// let name = "Taylor" | |
const BOOL firstRun = YES; | |
// let firstRun = true | |
/* | |
Constants are used infrequently in Objective-C, | |
but extremely common in Swift | |
*/ |
(3) Variable array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a variable array | |
NSMutableArray *items = [NSMutableArray new]; | |
// var items = [String]() | |
NSMutableArray<NSString *> *results = [NSMutableArray new]; | |
// var results = Array<String>() |
(4) Constant array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a constant array | |
NSArray *grades = @[@90, @85, @97]; | |
// let grades = [90, 85, 97] | |
NSArray *names = @[@"Talyor", @"Adele", @"Justin"]; | |
// let names = ["Talyor", "Adele", "Justin"] |
(5) Adding a value type to an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adding a value type to an array | |
NSMutableArray *array = [NSMutableArray new]; | |
// var array = [CGRect]() | |
[array addObject:[NSValue valueWithRect:CGRectMake(0, 0, 32, 64)]]; | |
// array.append(CGRect(x: 0, y: 0, width: 32, height: 64)) | |
/* | |
Value types must be wrapped in a reference type before being added to a collection. | |
*/ |
(6) Dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a dictionary | |
NSDictionary *houseNumbers = @{@"Paul": @7, @"Jess": @56, @"Peter": @332}; | |
// let houseNumbers = ["Paul": 7, "Jess": 56, "Peter": 332] |
(7) Enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define an enum | |
typedef NS_ENUM(NSInteger, ShapeType) { | |
kCircle, | |
kRectangle, | |
kHexagon | |
}; | |
/* | |
enum ShapeType: Int { | |
case circle | |
case rectangle | |
case hexagon | |
} | |
*/ |
(8) Appending a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *first = @"Hello, "; | |
// let first = "Hello, " | |
NSString *second = [first stringByAppendingString:@" world!"]; | |
// let second = first + "world!" |
(9) Adding to a number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSInteger rating = 4; | |
// var rating = 4 | |
rating++; | |
// rating += 1 | |
rating += 3; | |
// rating += 3 |
(10) String interpolation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *account = @"twostraws"; | |
// let account = "twostraws" | |
NSString *str = [NSString stringWithFormat:@"Follow me on Twitter: %@", account]; | |
// let str = "Follow me on Twitter: \(account)" |
(11) Printing debug information
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *username = @"twostraws"; | |
// let username = "twostraws" | |
NSLog(@"Username is %@", username); | |
// print("Username is \(username)") |
참조:
https://www.hackingwithswift.com/articles/114/objective-c-to-swift-conversion-cheat-sheet
Objective-C to Swift conversion cheat sheet
Here is the Rosetta Stone for Objective-C to Swift
www.hackingwithswift.com
'iOS > Objective-C' 카테고리의 다른 글
[Objective-C] Objective-C에서의 메모리 관리 (0) | 2023.04.12 |
---|---|
[Objective-C] GCD / Paul Hudson의 Objective-C cheat sheet (0) | 2023.04.12 |
[Objective-C] Classes / Paul Hudson의 Objective-C cheat sheet (0) | 2023.04.12 |
[Objective-C] Functions / Paul Hudson의 Objective-C cheat sheet (0) | 2023.04.12 |
[Objective-C] Control flow / Paul Hudson의 Objective-C cheat sheet (0) | 2023.04.12 |