Arrays are a very common component in many programming languages. Swift is no different. Arrays are an ordered list of objects of the same type in Swift. This is the case for arrays in most other programming languages, but Objective-C’s NSArray was capable of holding values of different types in the same NSArray. In that case, they were stored as id, and thus took a bit more work to change them back into whatever type they actually were. Swift simplifies it by enforcing that all values are of the same type. This in particular makes things safer, so you don’t have to check what type a variable is when you take it out of a Swift array.
[Read more…]
Swift Strings
Strings are a very common type of variable in many programming languages. Strings are sequences of characters, used to store text. Swift in particular works with them in a Unicode-compliant way. In this post we will discuss some of the higher level aspects of working with Strings in Swift. The Swift String is very different from its predecessor NSString, but they are bridged together in such a way that you can use a Swift string anywhere you would use an NSString. You can also use NSString methods on Swift Strings. In Swift 1.2, NSStrings are not automatically bridged to Swift Strings (but they were beforehand), they must be typecast using the “as” operator. Swift Strings can still be used where an NSString is expected though, so you only have to typecast an NSString to its Swift equivalent if you still have one.
Type Casting in Swift
In my previous post, Generic Functions in Swift, I used a type casting operator to check for inheritance. It was simple enough to mention there, but I felt I should cover type casting in Swift a bit more in-depth in its own post. Type casting is a way to convert an object of one type to another.
There is one term that is used a lot when talking about type casting, so it should probably be defined upfront, that term is downcast. According to Wikipedia, downcasting is the act of casting a reference of a base class to one of its derived classes. There is an opposite term to this one, the obviously named upcast. While this term is not used in Apple’s iBook, it is used in the WWDC video “Swift Interoperability In Depth.” It of course means to go the other way, casting a derived class back up to one of its base classes.
[Read more…]
Generic Functions in Swift
Now, with knowledge of Protocols in Swift in hand, let’s get to talking about one of the other big additions to the Swift language: Generics. I had previously discussed in my post Swift Optionals – Declaration, Unwrapping, and Binding how Swift optionals work under the hood. In that case, the OptionalValue enum is a generic type. Today we are going to cover a slightly easier aspect of generics, generic functions. Generic functions are functions that can take parameters of any type (with or without constraints based on protocol adoption), and perform some action with them.
[Read more…]
Protocols in Swift
I wanted to write a bit about Generics in Swift, but I realized that some of the major powers of Generics require the use of protocols, so I felt I should start by talking about them first.
In Swift, protocols are basically a named contract that your classes can conform to. If your class says it conforms to Equatable, then it better fulfill all of the required functionality to make it equatable. They are rather similar to interfaces in C# or Java. They list off the function prototypes and variable declarations, as well as stating whether they are required or optional, but don’t actually do anything. It is up to class, structs, or enumerations that claim to conform to the protocol to actually provide the functionality.
Defining a Protocol
This is pretty easy. It is very much like defining a class in Swift, except you write protocol instead, observe:
protocol Vehicle { //Functions and properties prototypes go here. }
Then you just fill it with what you want.
[Read more…]
- « Previous Page
- 1
- …
- 5
- 6
- 7
- 8
- 9
- Next Page »