Recently, Apple announced and released a beta version of the new Swift programming language
for building iOS and OSX applications. Swift is a modern language with
the power of Objective-C without the "baggage of C." While we can't
argue that Objective-C has it's difficulties being tied closely to C,
but the real question is... How does Swift compare to a modern language
like C#?
Please, keep in mind that this post is not supposed to be an Apple vs Microsoft post. There are a lot of developers that use C# every day and the purpose of this post is to help them understand what Swift offers at a language level compared to C#. And, before you start the "apples and oranges" arguments, it's worth pointing out that using Xamarin you can develop iOS and OSX apps using C#.
Now let the code mostly speak for itself...
While type inference is nice, but when you don't immediately assign a
value to the variable you may need to explicitly specify the type of
the variable.
You may notice the lack of the semi-colon in Swift. Yes, Swift is a
mostly C-style syntax without requiring semi-colons. Swift does support
and require the use of semi-colons if you want to have multiple code
statements on the same line.
Also in the above type inference example you'll notice that when you
declare a variable and immediately assign a value that is the result of 2
other variables it will still infer the type.
The both also have similar methods of detecting if the beginning or ending of the string match's a specified string.
You may notice from the above example that parenthesis are not required with IF statements in Swift.
The range example of Swift is rather interesting in the method of shorthand it uses for it's definition.
Switch statements are rather similar in both languages except that in
Swift case statements don't automatically pass on to the next like in
C#. As a result C# requires the use of the break keywords to exit the
Switch statement, unless you want to fall through to the next case.
While in Swift you must use the "fallthrough" keyword to tell it to pass
on through to the next case statement. More information on this can be
found in the Swift documentation.
An additional feature that Swift supports with Switch statements is ranges within the Case statements. This is something that C# does not support.
The post Basic Comparison of Functions in C# and Swift goes into much more depth on Functions; as that is a much bigger comparison that could fit into this post.
One of the bigger differences that's worth pointing out explicitly is the difference in how each language handles Array's. Arrays in Swift are extremely similar to the List<> class in C#; which is what most developers use today in C# instead of arrays anyway (unless performance requires it.)
You can find more information about the Swift programming language on Apple's site at the following links:
https://developer.apple.com/swift/
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/
- Blog Post by: Chris Pietschmann
Please, keep in mind that this post is not supposed to be an Apple vs Microsoft post. There are a lot of developers that use C# every day and the purpose of this post is to help them understand what Swift offers at a language level compared to C#. And, before you start the "apples and oranges" arguments, it's worth pointing out that using Xamarin you can develop iOS and OSX apps using C#.
Now let the code mostly speak for itself...
Code Comments
Both languages support the same syntax for code comments; the familiar C-style comments.
1
2
3
4
| // code comment /* multi line code comment */ |
Declaring Constants and Variables
Swift, like C#, is a type safe language. It also supports type inference so you don't have to specify the type when declaring the variables as the compiler can infer (or detect) the type by evaluating the assignment of the variable. While C# is slightly more verbose when declaring constants; both languages are just as elegant at declaring variables using type inference.
1
2
3
4
5
6
7
8
9
10
11
12
13
| // Declare Constant // C# const int legalAge = 18; // Swift let legalAge = 18 // Declare Variable // C# var legalAge = 18; // Swift var legalAge = 18 |
1
2
3
4
5
6
| // Type Annotation //C# string firstName; // Swift var firstName: String |
Variable Names and Unicode
Both languages support the use of Unicode characters as variable names. Basically, you could use Emoticons or other non-ASCII characters as variable names if you want, but who does that anyway?Integer Bounds
Both languages have static constants for accessing the minimum and maximum bounds for the different Integer types.
1
2
3
4
5
6
7
8
| // Integer Bounds // C# var a = Int32.MinValue; var b = Int32.MaxValue; // Swift var a = Int32.min var b = Int32.max |
Type Inference
Both languages, as mentioned above, support type inference where the compiler is able to detect what type the declared variable is from it's immediate assignment.
1
2
3
4
5
6
7
8
9
10
| // 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
Both have similar methods of comparing strings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| // 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 } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| // 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
Both languages support similar methods of converting strings to Upper or Lower Case.
1
2
3
4
5
6
7
8
9
10
| // 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
Both languages support declaring and assigning Arrays using a single line of code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| // 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
Working with Arrays have slight differences between the languages.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
| // 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
Both languages support similar methods of declaring dictionaries.
1
2
3
4
5
6
7
8
9
10
11
12
| // 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
Working with Dictionaries have slight differences between the languages.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| // 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
The above examples for Arrays and Dictionaries already showed examples of using a For-In loop to iterate through the items in those collections. Here are some additional methods of iterating using a For Loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| // 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
Both languages support If...Then conditional statements. Swift is a little different that it doesn't require parenthesis around the match conditions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| // 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
Both languages support Switch statements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| // 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 } |
An additional feature that Swift supports with Switch statements is ranges within the Case statements. This is something that C# does not support.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| // 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
While Functions are a much bigger comparison to be made, here's a basic example:
1
2
3
4
5
6
7
8
9
10
| // Function with Parameter and Return Value // C# string sayHello( string name) { // do something } // Swift func sayHello(name: String) -> String { // do something } |
Conclusion
This concludes my basic comparison of C# and Apple Swift programming languages. The two languages are rather similar in many respects; at least in what I've compared thus far. More language feature comparisons will have to wait for future posts.One of the bigger differences that's worth pointing out explicitly is the difference in how each language handles Array's. Arrays in Swift are extremely similar to the List<> class in C#; which is what most developers use today in C# instead of arrays anyway (unless performance requires it.)
You can find more information about the Swift programming language on Apple's site at the following links:
https://developer.apple.com/swift/
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/
- Blog Post by: Chris Pietschmann