With more inspiration from *Swift Fundamentals* *The Language of iOS Development* by Mark Lassoff (ISBN 9780990402053), we continue on from yesterday’s Swift Dictionaries and Arrays Primer Tutorial as shown below, by talking about Operator Overloading today, we touch on some of the following elements of the Xcode (IDE) use of the Swift language (the Language of iOS Development) …

… as we mentioned yesterday, in Xcode you can test such Swift functionality in the “Playground” (functionality) section.

So take a look at today’s Swift programming source code you could call Overloading.swift where we create a “struct” for the concept of a “Fraction” (as we may have learnt in mathematics at school) as per …

struct Fraction {

var numerator = 0

var denominator = 1 }

… and then in the program we initialize two such “Fraction” structures …

var fraction1 = Fraction(numerator:2, denominator:5)

var fraction2 = Fraction(numerator:7, denominator:10)

Now a computer program presented with this “numerator over denominator” concept of a “Fraction” won’t automatically know how to add two of these, or take one from another, or multiply two together, or divide one by another (though it will for the basic numerical data types, and the Operator Overload functions reduce calculations to these basic numerical data types which are the same as the data types of the “members” of the “Fraction” structure) … you have to show (the computer how to do) it. So we write code to show the computer program how you + – * / fractions by using Operator Overloading functions to define this … and here is the one for add, or “+” …

func + (f1: Fraction, f2: Fraction) -> Fraction {

var denomproposed = f1.denominator * f2.denominator

var numproposed = f1.numerator * f2.denominator + f2.numerator * f1.denominator

for var i = (denomproposed / 2); i>1; i-- {

if (denomproposed % i) == 0 && (numproposed % i) == 0 {

denomproposed /= i

numproposed /= i

}

}

return Fraction(numerator: numproposed, denominator: denomproposed) }

… and then we are able to go …

println(fraction1 + fraction2)

println(fraction1 - fraction2)

println(fraction1 * fraction2)

println(fraction1 / fraction2)

… to result in …

(numerator 11, denominator 10)

(numerator -3, denominator 10)

(numerator 7, denominator: 25)

(numerator 4, denominator: 7)

… quicker than we could have done it at school, though school comes back into it working out how to write the code.

You may recall we talked about Operator Overloading previously with C++ Xcode OOP Operator Overloading Tutorial and we talked about Fractions too when we presented HTML/Javascript Canvas Fractions Game Tutorial.

Hope some ideas spring eternal for you reading this tutorial, and hope to see you back tomorrow.

Previous relevant Swift Dictionaries and Arrays Primer Tutorial as shown below.

With some inspiration from *Swift Fundamentals* *The Language of iOS Development* by Mark Lassoff (ISBN 9780990402053), today, we touch on some of the following elements of the Xcode (IDE) use of the Swift language … you guessed it … The Language of iOS Development …

- variable data types String and Int and Bool
- array
- dictionary
- if statements
- for loops
- string concatenation (in println statements)

… in Xcode you can test such Swift functionality in the “Playground” (functionality) section.

You see the results of your coding as they build up over to the right of the Xcode window, pretty much instantaneously. To “drill” down to results of a loop click the “Value History” round buttons, where this takes you to graphical representations of the data … quite cute really.

So these are some of the bits and pieces of knowledge you can develop further to build up ideas for the creation of iOS mobile applications, which you may eventually want to sell on the Apple App Store (eg. for Australia).

You may wonder with the arrays (in section-1.swift) why there is a separate “tophomeawaypoints:[String: Int]” dictionary and “bothomeawaypoints:[String: Int]” one as well (also for “top8:[String]” array and a “bot10:[String]” by association). Well, this was done, really, following the advice of the Swift interpreter, saying that the structure was “too complex” to have the one dictionary and one array here.

So take a look at today’s Swift programming source code you could call section-1.swift where we analyze the AFL 2014 Home and Away Ladder hoping that Collingwood has a better 2015!

If this was interesting you may be interested in this too.

If this was interesting you may be interested in this too.

This is the right website for anyone who would like to understand this topic.

You realize so much its almost tough to argue with you (not that I actually will need toβ¦HaHa).

You definitely put a fresh spin on a topic that’s been discussed for years.

Great stuff, just great!