What I'm Thinking

One thing one often finds themselves needing to do to an array is access the first and last elements. Of course, this can be done by referencing [0] for the first, or using count for the last, but those are both a bit inelegant. Now with Xcode 6 Beta 5 we have two new convenience properties, first & last. var africanAnimals = ["Lion", "Thomson's Gazelle", "Elephant", "Zebra"] africanAnimals.first // Lion ... Read More
Accessing a type method is different from accessing an instance method. If you want to say Foo.bar() You’ll need to say this if you’re in a struct static func bar() { } or this if you’re in a class. class func bar() { }
When you use a setter in your class, Swift will autovivify a default variable - newValue. However, following the logic of even a simple example can be confusing when employing this class PassengersToVehicleRatio { var numberOfVehicles: Int = 5 // assume 4 passengers per vehicle var passengerTotal: Int { get { return 4 * numberOfVehicles } set { numberOfVehicles = newValue / 4 } } } var passengers = PassengersToVehicleRatio() ... Read More
In plenty of other languages looping over both the keys and the values in a dictionary is non-trivial; it requires some special fooling around with references rather clunkily. One of my favourite things about Swift is that it allows you to evaluate both the keys and the values of a dictionary at one time, with finesse. Consider let worldCupGroupsABGoals = [ "Brazil": [3, 0, 4], "Mexico": [1, 0, 3], "Croatia": [1, 4, 1], "Cameroon": [0, 0, 1], "Netherlands": [5, 3, 2], "Chile": [3, 2, 0], "Spain": [1, 0, 3], "Australia": [1, 2, 0], ] var mostGoals = 0 var countryMostGoalsInSingleMatch: String? ... Read More
Traditionally one would have to say for var i = 0; i < 4; ++i { forLoopA += i } Now in Swift one can use the Half-Open Range Operator index to say for i in 0..<4 { forLoopB += i }