Swift Optionals Quick Reference for Newbies by Santiapps.com

Swift Optionals Quick Reference

Optionals confuse me so I wrote this post and hope it can be of some help. OPTIONALS Takeaway #1: Optionals are used to declare a variable but are not assigned a value at start Takeaway #2: Optionals contain a Some or None value, they DONT contain a String or Array or whatever else. Takeaway #3: Therefore we MUST unwrap the value of an Optional to see what the prize is 🙂 In other words, to access its value! Takeaway #4: DECLARATION OPTIONS A) Using the following syntax: var someVar? – You will mostly use this… B) Using “var someVar!” <Implicitly unwrapped> – Unfortunately UIKit & other Apple frameworks will use a lot of… Read More

Continue Reading

Why, oh Why, did Apple take away ARC just to give us Optionals!?

I’m having trouble understanding optionals so here is a shot at explaining them. 🙂 var thisIsAnInt: Int Simple, this is an Int variable declaration. var couldBeAnInt: Int? This on the other hand is an optional variable declaration. This optional declaration doesn’t mean: “this is an int, which is optional”. It reads more like: This is an OPTIONAL variable.  It’s a type of variable in and of itself.  Its NOT NECESSARILY an Int.  It just may or may NOT contain an Int”. Woah! Ok so what is it for and when do you use it.  Well that part seems simple enough: If that variable can or could or may be nil… Read More

Continue Reading

iOS : Swift : Blocks = Closures

I’ve never really liked blocks in ObjC. When Swift came out it made things more complicated for me because I’ve never really liked C either. Finally when I had to deal with closures in Swift, well that’s just gonna piss a lot of people off! After a few days reviewing tons of material online, and I mean TONS!  I came to understand this: The only C-like exposure I had prior to ObjC was a little PHP.  So that allows me to understand a function, which is the equivalent of a method in ObjC: DECLARING func sayHello( ) {      println(“Hello World”) } CALLING sayHello( ) RESULT Hello World Even… Read More

Continue Reading