Swift Operator Overloading Primer Tutorial
โœ‚๐Ÿƒ๐Ÿพโ€โ™€๏ธ๐Ÿƒ๐Ÿผโ€โ™‚๏ธ
๐Ÿ“–

Swift Operator Overloading Primer Tutorial

Swift Operator Overloading Primer Tutorial

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.

Swift Dictionaries and Arrays Primer Tutorial

Swift Dictionaries and Arrays Primer Tutorial

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 entry was posted in eLearning, Tutorials and tagged , , , , , , , , . Bookmark the permalink.

One Response to Swift Operator Overloading Primer Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *