As promised last time, here is part two of my post about Swift optionals. Here we will discuss a few more advanced aspects on how to deal with optionals, optional chaining and implicitly unwrapped optionals. These are a few more tricks to get values out of optionals, while keeping Swift code much cleaner than its predecessors. Optional chaining makes it easy to drill into multiple levels of optional values. Implicitly unwrapped optionals, while not as safe as normal optionals, are often used under the assumption that the value that it is describing is never, or usually not, nil.
Swift Optionals – Declaration, Unwrapping, and Binding
Continuing our conversations about variables in Swift, lets move on to one of the more important additions in this realm to Swift, Optionals. In Swift, all normal variables must have a value. In Objective-C, C-based variables (primitives) always had to have a value, but object types could either be a valid object, or a pointer to nil. Swift optionals allow you to make any type in Swift into a nil-able type (though this nil is not exactly the same as an Objective-C nil).
[Read more…]
Computed Properties in Swift
Continuing the discussion about variables in Swift, let us move on to properties, which are basically Swift constants or variables used in a class, structure, or enumeration. More specifically, they are stored values for a class, structure, or enumeration, that have getter and setter methods that can be modified. You can leave them alone, and they will do what they should (return the value or set a new one). The getters and setters can be overridden, like to override the setter to allow you to check if the input value is valid before storing it. Getters and setters are also called “accessors” and “mutators” respectively. Take your pick, but for me, I’m going to just call them getters and setters.
There are a few more aspects to properties, but today, we will be talking about a specific type of property. [Read more…]
Swift Variables and Constants
How do you store data in a program? Well, you store it in variables, but you already knew that didn’t you? Here is my first post about Swift variables, where we discuss their declaration and definition. In particular, we will mostly be discussing the concept of Swift variables and constants.
- « Previous Page
- 1
- …
- 7
- 8
- 9