Tag Archives: swiftlang

Difference between Struct and Class in Swift

One of the common question asked is the difference between Struct andClass. The main difference is that objects on Struct are passed by value and objects on Class are passed by reference.

Tuples in Swift: Create, Read, and Return

Tuples in Swift: Create, Read, and Return

I was looking for a bit of information on Tuples in Swift for an app I was working on, and decided it would be best to gather what I learn into one, easy to access place.

Tuples are a compound type in Swift, which basically means that they can hold multiple values.  A compound type can either contain what are called “named types”, which include classes, structures, and enumerations (also protocols, but since they don’t store values directly, I felt I should mention them separately), as well as other compound types.  That means a tuple can hold other tuples.  Another compound type a tuple can contain is the “function type,” which is a different way of referring to the type that describes closures (specifically the “() >() ” style of type, which functions and methods also conform to.  That also goes the other way, that a function type can hold other compound types, like the Tuple, and other closures, which we’ve seen in my previous postClosures and Capturing Values in Swift.

While technically inaccurate, you can conceptually think of Tuples kind of like a class or structure that you can make on the fly, without having to define a full fledged class or structure (nested or otherwise).  According to Apple’s iBook though, they should probably only be used for temporary values, like to return them from a function.  It doesn’t go into detail why, but if I had to guess, they were probably optimized for quick creation, at the cost of how it stores them.  Nonetheless, one of the great new capabilities of Swift is multiple return types, and tuples are what Swift uses to achieve this, since they are technically returning a single value (the tuple itself).

One thing people are sometimes curious about tuples is how it is pronounced.  Well, according toDictionary.com, there are two valid pronunciations.  In dictionary pronunciation code: /ˈtjʊpəl and ˈtʌpəl/.  For anybody curious, ʊ is the sound of the “oo” in “took”, ə (a schwa) is how the a sounds in “about”, and ʌ (a wedge) is the sound of the “u” in “gut.”  That means that those are pronounced (roughly) as  too-puhl and tuh-puhl, respectively.  I was digging around for examples of where they were from, only to find out both pronunciations happen for those examples as well, but they are basically from the end of words like “quadruple,” “quintuple,” and “octuple” (notice the “tuple” at the end of each).  I personally prefer too-puhl, myself.

With readers coming from the US, the UK, and many other places, I suppose the pronunciation will vary.  For those that listen to the Accidental Tech Podcast, apparently Dictionary.com agrees with both sides:  huhv-er (/ˈhʌv ər) and hov-er (ˈhɒv-/ ər).  For those of you that don’t, you really should go check it out.

Anyway though, you didn’t come here to learn about English phonetics, it’s time to learn how to use tuples in Swift!

Core Data in Swift Tutorial (Part 1)

Core Data in Swift Tutorial (Part 1)

This post compatible with Xcode 6 GM

Core Data is the de facto standard way to persist and manage data in both iPhone and Mac applications. So it’s only natural that we should take some time to learn about it when building apps.

The first thing to know about Core Data before diving in is that it is not a relational database, and although it uses SQLite as a backing engine, is not an ORM to a relational database either. The SQLite backend is more of an implementation detail, and in fact binary files or plists can be used instead.

The official Apple documentation describes Core Data like this:

“The Core Data framework provides generalized and automated solutions to common tasks associated with object life-cycle and object graph management, including persistence.”

Before we get too technical about what Core Data is, I think it’s useful to dive in and start playing with the API a bit.

Create a new Xcode 6 project using a single-view template, Swift as the language, and with Core Data enabled. I’ll call the project MyLog.

Swift Enumerations – Intermediate

Swift Enumerations – Intermediate

What is a Swift enumeration? Well to quote the official documentation

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

OK, so seriously, what is an enumeration?

Well it turns out that the documentation is pretty accurate but makes them sound more complicated than they really are (because programmers like to do that), the easiest way to learn what they are is to just play with them. Let’s open a new Swift playground and start playing.

To create an enumeration or as it is referred to in the language enum we do so like this, lets make anenum that holds possible options for exits from a house. We will call it DoorUsed

Which function does Swift call? Part 3: Protocol Composition

Which function does Swift call? Part 3: Protocol Composition

Previously, we looked at how Swift functions can be overloaded just by return type, and how Swift picks between different possible overloads based on a best-match mechanism.

All this was working towards the reason why 1...5 returns a Range rather than a ClosedInterval. And to understand that, we’ll have to look at how generics fit into the matching criteria.

But before we do, a brief diversion into protocol composition.

Protocols can be composed by putting zero1 or more protocols between the angle brackets of a protocol<> statement. The Apple Swift book says:

Building Your First Swift App Video

Building Your First Swift App Video

So far the Swift blog has focused on advanced programming topics, including the design principles of the Swift language. We thought it would be helpful to provide content for programmers who are new to Swift and just trying Xcode for the first time. To make it more approachable for everyone, we put together a very short video that demonstrates how to build an iOS app in Swift from scratch, in less than ten minutes.

Using Multiple protocols in generic functions and everywhere | #swift #swiftlang #Applescript #iOS #Xcode