100 Days of Swift – Day 2

Continuation of Hacking With Swift 100 Days of Swift

Day 2 – Arrays, Dictionaries, Sets and Enums

Arrays are a standard, type declaration is not. instead of int[] in swift we do [Int]
Sets are quite simple, just use the named reserved function Set. …


This content originally appeared on DEV Community and was authored by Davmi Jose Valdez Ogando

Continuation of Hacking With Swift 100 Days of Swift

Day 2 - Arrays, Dictionaries, Sets and Enums

  1. Arrays are a standard, type declaration is not. instead of int[] in swift we do [Int]

  2. Sets are quite simple, just use the named reserved function Set. Have in mind that these guys are unordered, not random, just unordered.

  3. Talking about functions, in swift you only use func nameOfFunction(property: type), as a definer, but when you call them you need to do it weirdly as nameOfFunction(property: value).

  4. Tuples like in .Net are fixed size, but have the advantage that can be named (iirc Named tuples only come later in .Net 8 or something).

  5. Dictionaries are declared like in JavaScript, which btw accessing a none existing key instead of throwing an error returns nil (null in other languages, let's not talk about undefined), if you don't want nil to show you can use dictionary["nonExistingKey", default: "fallback string"]. Lastly, for type declaration we go as [String: otherType] ex [String: Double].

  6. Enums are declared differently, but just a bit. Inside each option of an enum you'd have to add the reserved keyword case, which results in enum Directions { case north; case south } and for specific values inside enums, works exactly like .Net. ALERT: the semicolon is only needed since this is an enum declared as a one liner, in a line break enum the semicolon is not required. Enums in swift can also have an associated value, where we can create something like enum Weather { case sunny; case windy(speed: Int) } and we would get windy(speed: 25) as a result.

Side note:
I need to look up for swift naming standards eventually.


This content originally appeared on DEV Community and was authored by Davmi Jose Valdez Ogando


Print Share Comment Cite Upload Translate Updates
APA

Davmi Jose Valdez Ogando | Sciencx (2022-03-30T21:35:24+00:00) 100 Days of Swift – Day 2. Retrieved from https://www.scien.cx/2022/03/30/100-days-of-swift-day-2/

MLA
" » 100 Days of Swift – Day 2." Davmi Jose Valdez Ogando | Sciencx - Wednesday March 30, 2022, https://www.scien.cx/2022/03/30/100-days-of-swift-day-2/
HARVARD
Davmi Jose Valdez Ogando | Sciencx Wednesday March 30, 2022 » 100 Days of Swift – Day 2., viewed ,<https://www.scien.cx/2022/03/30/100-days-of-swift-day-2/>
VANCOUVER
Davmi Jose Valdez Ogando | Sciencx - » 100 Days of Swift – Day 2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/30/100-days-of-swift-day-2/
CHICAGO
" » 100 Days of Swift – Day 2." Davmi Jose Valdez Ogando | Sciencx - Accessed . https://www.scien.cx/2022/03/30/100-days-of-swift-day-2/
IEEE
" » 100 Days of Swift – Day 2." Davmi Jose Valdez Ogando | Sciencx [Online]. Available: https://www.scien.cx/2022/03/30/100-days-of-swift-day-2/. [Accessed: ]
rf:citation
» 100 Days of Swift – Day 2 | Davmi Jose Valdez Ogando | Sciencx | https://www.scien.cx/2022/03/30/100-days-of-swift-day-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.