- iOS 9 Game Development Essentials
- Chuck Gaffney
- 184字
- 2021-08-05 16:36:56
Objective-C and Swift comparison
Here's a quick review of our Swift code with a comparison to the Objective-C equivalent.
Objective-C
Here's a sample code in Objective-C:
const int MAX_ENEMIES = 10; //constant float playerPower = 1.3; //variable //Array of NSStrings NSArray * stageNames = @[@"Downtown Tokyo", @"Heaven Valley", @" Nether"]; //Set of various NSObjects NSSet *items = [NSSet setWithObjects: Weapons, Armor, HealingItems,"A", nil]; //Dictionary with an Int:String key:value NSDictionary *inventory = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:1], @"Buster Sword", [NSNumber numberWithInt:43], @"Potion", [NSNumber numberWithInt:22], @"Strength", nil];
Swift
Here's the equivalent code in Swift:
let MAX_ENEMIES = 10 //constant var playerPower = 1.3 //variable //Array of Strings let stageNames : [String] = ["Downtown Tokyo","Heaven Valley","Nether"] //Set of various NSObjects var items = Set([Weapons, Armor, HealingItems,"A"]) //Dictionary with an Int:String key:value var playerInventory: [Int : String] = [1 : "Buster Sword", 43 : "Potion", 22: "StrengthBooster"]
In the preceding code, we used some examples of variables, constants, arrays, sets, and dictionaries. First, we see their Objective-C syntax and then the equivalent declarations using Swift's syntax. From this example, we can see how compact Swift is compared with Objective-C.
推薦閱讀
- 從零構(gòu)建知識(shí)圖譜:技術(shù)、方法與案例
- Java范例大全
- 軟件測(cè)試項(xiàng)目實(shí)戰(zhàn)之性能測(cè)試篇
- 游戲程序設(shè)計(jì)教程
- 老“碼”識(shí)途
- Apache Karaf Cookbook
- Visual Basic程序設(shè)計(jì)實(shí)踐教程
- Building Android UIs with Custom Views
- Android項(xiàng)目實(shí)戰(zhàn):手機(jī)安全衛(wèi)士開發(fā)案例解析
- Processing創(chuàng)意編程指南
- Lift Application Development Cookbook
- Instant Zurb Foundation 4
- Python一行流:像專家一樣寫代碼
- Python應(yīng)用與實(shí)戰(zhàn)
- React.js實(shí)戰(zhàn)