Swift和C#的基本语法对比
Code
Comments
- // code comment
- /* multi line
- code comment */
Declaring Constants and Variables
- // Declare Constant
- // C#
- const int legalAge = 18;
- // Swift
- let legalAge = 18
- // Declare Variable
- // C#
- var legalAge = 18;
- // Swift
- var legalAge = 18
- // Type Annotation
- //C#
- string firstName;
- // Swift
- var firstName: String
Variable Names and
Unicode
Integer Bounds
- // Integer Bounds
- // C#
- var a = Int32.MinValue;
- var b = Int32.MaxValue;
- // Swift
- var a = Int32.min
- var b = Int32.max
Type Inference
- // Type Inference
- // C#
- var a = 3; // integer
- var b = 0.14 // double
- var c = a + b; // double
- // Swift
- var a = 3 // integer
- var b = 0.14 // double
- var c = a + b // double
String Comparison
- // String Comparison
- // C#
- var a = "One";
- var b = "One";
- if (a == b) {
- // both variables are considered equal
- }
- // Swift
- var a = "One"
- var b = "One"
- if a == b {
- // both variables are considered equal
- }
- // C#
- var s = "Some Value";
- if (s.StartsWith("Some")) {
- // the string starts with the value
- }
- if (s.EndsWith("Value")) {
- // the string ends with the value
- }
- // Swift
- var s = "Some Value"
- if s.hasPrefix("Some") {
- // the string starts with the value
- }
- if s.hasSuffix("Value") {
- // the string ends with the value
- }
String Upper or Lower Case
- // String Upper and Lower Case
- // C#
- var s = "some Value";
- var upperS = s.ToUpper();
- var lowerS = s.ToLower();
- // Swift
- var s = "some Value"
- var upperS = s.uppercaseString
- var lowerS = s.lowercaseString
Declaring Arrays
- // Declare Arrays on single line
- // String Array
- // C#
- var arr = new string[] { "One", "Two" };
- // Swift
- var arr = ["One", "Two"]
- // Integer Array
- // C#
- var arr = new int[] { 1, 2 };
- // Swift
- var arr = [1, 2];
Working with Arrays
- // Iterating Over Array
- // C#
- foreach (var item in arr) {
- // do something
- }
- // Swift
- for item in arr {
- // do something
- }
- // Get Item at Index
- // C#
- var item = arr[0];
- // Swift
- var item = arr[0]
- // Set Item at Index
- // C#
- arr[0] = "Value";
- // Swift
- arr[0] = "Value"
- // Is Array Empty?
- // C#
- if (arr.Length == 0) {
- // array is empty
- }
- // Swift
- if arr.isEmpty {
- // array is empty
- }
- // Add Item to Array
- // C#
- Array.Resize(ref arr, arr.Length + 1);
- arr[arr.Length - 1] = "Three";
- // Swift
- arr.append("Three")
- // or
- arr += "Three"
- // Remove Item at Index
- // C#
- var list = arr.ToList();
- list.RemoveAt(0);
- var newArr = list.ToArray();
- // Swift
- var newArr = arr.removeAtIndex(0)
Declaring Dictionaries
- // Declaring Dictionaries
- // C#
- var dict = new Dictionary<string, string>();
- var dict2 = new Dictionary<string, string>
- {
- { "TYO", "Tokyo" },
- { "DUB", "Dublin" }
- };
- // Swift
- var dict = Dictionary<String, String>()
- var dict2 = ["TYO": "Tokyo", "DUB": "Dublin"]
Working with Dictionaries
- // Iterate over Dictionary
- // C#
- foreach(var item in dict) {
- var key = item.Key;
- var value = item.Value;
- }
- // Swift
- for (key, value) in dict {
- // key variable contains key of item
- // value variable contains value of item
- }
- // Get Item in Dictionary by Key
- // C#
- var item = dict["TYO"];
- // Swift
- var item = dict["TYO"]
- // Set Item in Dictionary by key
- // or add if key doesn‘t exist
- // C#
- dict["LHR"] = "London";
- // Swift
- dict["LHR"] = "London"
- // Remove Item in Dictionary by key
- // C#
- dict.Remove("LHR");
- // Swift
- dict.removeValueForKey("DUB")
For Loops
- // Iterate from 1 through 5
- // C#
- // using increment
- for(var i = 1; i <= 5; i++) {
- // do something with i
- }
- // Swift
- // using range
- for i in 1...5 {
- // do something with i
- }
- // using increment
- for var i = 0; i <= 5; ++i {
- // do something with i
- }
Conditional Statements
- // If Then Else Conditional Statement
- // C#
- if (i > 6) {
- // do something
- } else if (i > 3 && i <= 6) {
- // do something
- } else {
- // do something
- }
- // Swift
- if i > 6 {
- // do something
- } else if i > 3 && i <= 6 {
- // do something
- } else {
- // do something
- }
Switch Statement
- // Switch statement
- // C#
- var word = "A";
- switch(word) {
- case "A":
- // do something
- break;
- case "B":
- // do something
- break;
- default:
- // do something
- break;
- }
- // Swift
- var word = "A"
- switch word {
- case "A":
- // do something
- case "B":
- // do something
- default:
- // do something
- }
- // Switch Case Ranges
- // C#
- switch (i) {
- case 1:
- case 2:
- case 3:
- // do something
- break;
- case 4:
- // do something
- break;
- default:
- // do something
- break;
- }
- // Swift
- switch i {
- case 1...3:
- // do something
- case 4:
- // do something
- default:
- // do something
- }
Functions
- // Function with Parameter and Return Value
- // C#
- string sayHello(string name) {
- // do something
- }
- // Swift
- func sayHello(name: String) -> String {
- // do something
- }
Conclusion
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。