├── Variables and Constants.playground
├── contents.xcplayground
├── timeline.xctimeline
└── section-1.swift
└── README.md
/Variables and Constants.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Exploring Swift
2 | ===============
3 |
4 | Accompanying code and playground files for my series of post about Swift on my blog where I explore obscure and interesting things about Apple's new language.
5 |
6 | * [Variables and Constants][1]
7 |
8 | [1]:http://totocaster.com/2014/exploring-swift-variables-constants/
9 |
--------------------------------------------------------------------------------
/Variables and Constants.playground/timeline.xctimeline:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Variables and Constants.playground/section-1.swift:
--------------------------------------------------------------------------------
1 | /*
2 | * Exploring Swift
3 | * Variables and Constants
4 | * by Toto Tvalavadze
5 | *
6 | * This playground is accompanying series of posts about
7 | * Swift programing language on http://totocaster.com
8 | *
9 | * http://totocaster.com/2014/exploring-swift-variables-constants/
10 | */
11 |
12 | let numberOfMaximumCarsInParking = 1000 // Changing this in future will cause compile error
13 | var numberOfFreeSpacesInParking = 123
14 |
15 | var carParkingName:String; // No initialisation is fine, but type has to be annotated
16 |
17 | carParkingName = "Underground on Some Street"
18 |
19 | // Comment line above and you'll get compile error in if statement
20 | // because you can not use uninitialised variable
21 | if carParkingName == "Underground on Some Street" {
22 | "You are here"
23 | }
24 |
25 | // Single line multiple declaration
26 | var x = 1, y = 2, z = "This is string" // Works for constants too
27 |
28 | let 🐶🐮 = "dogcow" // Please don't do this
29 | // let ✊🍆 = "fisteggplant" // this doesn't work for some reason
30 |
31 | println("Currently \(carParkingName) has \(numberOfFreeSpacesInParking) free parking places") // To see result, open console in Xcode or Assistant Editor in Playground
32 |
33 | let numbers = [x,y] // [1,2] as constant
34 | x = 5
35 | numbers // again [1,2] and no error
36 | numbers[0] = 5 // this will work
37 | numbers // [5,2]
38 |
39 |
40 | class MyNumber {
41 | init (value:Int = 0){
42 | self.valueNumber = value
43 | }
44 |
45 | var valueNumber:Int;
46 |
47 | var value:Int {
48 | get {
49 | return valueNumber
50 | }
51 | set {
52 | valueNumber = newValue
53 | }
54 | }
55 | }
56 |
57 | let a = MyNumber(value:4)
58 | var b = MyNumber(value:7)
59 |
60 | let myNumbres = [a,b] // [4,7] as constant
61 | a.value = 7 // a is instance, thus referance type
62 | myNumbres // [7,7]
63 |
64 |
65 |
66 |
67 | /* This is multiline comment
68 |
69 | /* So this is, but interesting part is one line below */
70 |
71 | This line, would be illegal in some languages,
72 | but in Swift also belongs to comment scope above.
73 | */
74 |
75 | let isSwiftMultilineCommentAwesome = true
76 |
77 |
78 |
--------------------------------------------------------------------------------