{"id":14276,"date":"2015-04-19T04:45:23","date_gmt":"2015-04-18T18:45:23","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=14276"},"modified":"2016-02-11T20:34:41","modified_gmt":"2016-02-11T10:34:41","slug":"swift-operator-overloading-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/swift-operator-overloading-primer-tutorial\/","title":{"rendered":"Swift Operator Overloading Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Xcode\/iOS8\/Swift\/OperatorOverloading\/Overloading.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Swift Operator Overloading Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Xcode\/iOS8\/Swift\/OperatorOverloading\/Overloading.jpg\" title=\"Swift Operator Overloading Primer Tutorial\" id='isoo' onmouseover=\"  this.src=this.src.replace('.jpg','.xpng').replace('.png','.xjpg').replace('.x','.');     \" \/><\/a><p class=\"wp-caption-text\">Swift Operator Overloading Primer Tutorial<\/p><\/div>\n<p>With more inspiration from <i>Swift Fundamentals<\/i> <i><font size=2>The Language of iOS Development<\/font><\/i> by Mark Lassoff (ISBN 9780990402053), we continue on from yesterday&#8217;s <a target=_blank title='Swift Dictionaries and Arrays Primer Tutorial' href='#sdaapt'>Swift Dictionaries and Arrays Primer Tutorial<\/a> as shown below, by talking about <a target=_blank title='Operator overloading information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Operator_overloading'>Operator Overloading<\/a> today, we touch on some of the following elements of the Xcode (IDE) use of the Swift language (the Language of iOS Development) &#8230;<\/p>\n<ul>\n<li><a target=_blank title='Struct information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Struct'>struct<\/a><\/li>\n<li><a target=_blank title='Function information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Subroutine'>func<\/a><\/li>\n<li>if statements<\/li>\n<li>for loops<\/li>\n<\/ul>\n<p> &#8230; as we mentioned yesterday, in Xcode you can test such Swift functionality in the <a target=_blank title='Swift playground information from Apple' href='https:\/\/developer.apple.com\/swift\/'>&#8220;Playground&#8221;<\/a> (functionality) section.<\/p>\n<p>So take a look at today&#8217;s Swift programming source code you could call <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/Mac\/Xcode\/iOS8\/Swift\/OperatorOverloading\/Overloading.swift_GETME' title='Overloading.swift'>Overloading.swift<\/a> where we create a &#8220;struct&#8221; for the concept of a <a target=_blank title='Fraction information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Fraction_%28mathematics%29'>&#8220;Fraction&#8221;<\/a> (as we may have learnt in mathematics at school) as per &#8230;<\/p>\n<p><code><br \/>\nstruct Fraction {<br \/>\n    var numerator = 0<br \/>\n    var denominator = 1 }<br \/>\n<\/code><\/p>\n<p> &#8230; and then in the program we initialize two such &#8220;Fraction&#8221; struct<font size=1>ures<\/font> &#8230;<\/p>\n<p><code><br \/>\nvar fraction1 = Fraction(numerator:2, denominator:5)<br \/>\nvar fraction2 = Fraction(numerator:7, denominator:10)<br \/>\n<\/code><\/p>\n<p>Now a computer program presented with this &#8220;numerator over denominator&#8221; concept of a &#8220;Fraction&#8221; won&#8217;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 func<font size=1>tions<\/font> reduce calculations to these basic numerical data types which are the same as the data types of the &#8220;members&#8221; of the &#8220;Fraction&#8221; struct<font size=1>ure<\/font>) &#8230; you have to show (the computer how to do) it.   So we write code to show the computer program how you + &#8211; * \/ fractions by using Operator Overloading func<font size=1>tions<\/font> to define this &#8230; and here is the one for add, or &#8220;+&#8221; &#8230;<\/p>\n<p><code><br \/>\nfunc + (f1: Fraction, f2: Fraction) -> Fraction {<br \/>\n    var denomproposed = f1.denominator * f2.denominator<br \/>\n    var numproposed = f1.numerator * f2.denominator + f2.numerator * f1.denominator<br \/>\n    for var i = (denomproposed \/ 2); i>1; i-- {<br \/>\n        if (denomproposed % i) == 0 && (numproposed % i) == 0 {<br \/>\n            denomproposed \/= i<br \/>\n            numproposed \/= i<br \/>\n        }<br \/>\n    }<br \/>\n    return Fraction(numerator: numproposed, denominator: denomproposed) }<br \/>\n<\/code><\/p>\n<p> &#8230; and then we are able to go &#8230;<\/p>\n<p><code><br \/>\nprintln(fraction1 + fraction2)<br \/>\nprintln(fraction1 - fraction2)<br \/>\nprintln(fraction1 * fraction2)<br \/>\nprintln(fraction1 \/ fraction2)<br \/>\n<\/code><\/p>\n<p> &#8230; to result in &#8230;<\/p>\n<p><code><br \/>\n(numerator 11, denominator 10)<br \/>\n(numerator -3, denominator 10)<br \/>\n(numerator 7, denominator: 25)<br \/>\n(numerator 4, denominator: 7)<br \/>\n<\/code><\/p>\n<p> &#8230; quicker than we could have done it at school, though school comes back into it working out how to write the code.<\/p>\n<p>You may recall we talked about Operator Overloading previously with <a target=_blank title='C++ Xcode OOP Operator Overloading Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=2699'>C++ Xcode OOP Operator Overloading Tutorial<\/a> and we talked about Fractions too when we presented <a target=_blank title='HTML\/Javascript Canvas Fractions Game Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=7258'>HTML\/Javascript Canvas Fractions Game Tutorial<\/a>.<\/p>\n<p>Hope some ideas spring eternal for you reading this <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Xcode\/iOS8\/Swift\/OperatorOverloading\/Overloading.jpg\" title='Click picture'>tutorial<\/a>, and hope to see you back tomorrow.<\/p>\n<hr>\n<p id='sdaapt'>Previous relevant <a target=_blank title='Swift Dictionaries and Arrays Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=13789'>Swift Dictionaries and Arrays Primer Tutorial<\/a> as shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Xcode\/iOS8\/Swift\/Dictionary\/\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Swift Dictionaries and Arrays Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/Xcode\/iOS8\/Swift\/Dictionary\/Swift_Dictionary-77of.jpg\" title=\"Swift Dictionaries and Arrays Primer Tutorial\" id='isdaapt' onmouseover=\"  this.src=this.src.replace('-73','-x74').replace('-74','-x75').replace('-75','-x76').replace('-76','-x77').replace('-77','-x78').replace('-78','-x73').replace('-x','-');     \" \/><\/a><p class=\"wp-caption-text\">Swift Dictionaries and Arrays Primer Tutorial<\/p><\/div>\n<p>With some inspiration from <i>Swift Fundamentals<\/i> <i><font size=2>The Language of iOS Development<\/font><\/i> by Mark Lassoff (ISBN 9780990402053), today, we touch on some of the following elements of the Xcode (IDE) use of the Swift language &#8230; you guessed it &#8230; The Language of iOS Development &#8230;<\/p>\n<ul>\n<li>var<font size=1>iable<\/font> data types String and Int and Bool<\/li>\n<li>array<\/li>\n<li>dictionary<\/li>\n<li>if statements<\/li>\n<li>for loops<\/li>\n<li>string concatenation (in println statements)<\/li>\n<\/ul>\n<p> &#8230; in Xcode you can test such Swift functionality in the <a target=_blank title='Swift playground information from Apple' href='https:\/\/developer.apple.com\/swift\/'>&#8220;Playground&#8221;<\/a> (functionality) section.<\/p>\n<p>You see the results of your coding as they build up over to the right of the Xcode window, pretty much instantaneously.  To &#8220;drill&#8221; down to results of a loop click the &#8220;Value History&#8221; round buttons, where this takes you to graphical representations of the data &#8230; quite cute really.<\/p>\n<p>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 <a target=_blank href='http:\/\/www.apple.com\/au\/' title='Apple App Store for Australia'>Australia<\/a>).<\/p>\n<p>You may wonder with the arrays (in <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/Mac\/Xcode\/iOS8\/Swift\/Dictionary\/section-1.swift_GETME' title='section-1.swift'>section-1.swift<\/a>) why there is a separate &#8220;tophomeawaypoints:[String: Int]&#8221; dictionary and &#8220;bothomeawaypoints:[String: Int]&#8221; one as well (also for &#8220;top8:[String]&#8221; array and a &#8220;bot10:[String]&#8221; by association).   Well, this was done, really, following the advice of the Swift interpreter, saying that the structure was &#8220;too complex&#8221; to have the one dictionary and one array here.<\/p>\n<p>So take a look at today&#8217;s Swift programming source code you could call <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/Mac\/Xcode\/iOS8\/Swift\/Dictionary\/section-1.swift_GETME' title='section-1.swift'>section-1.swift<\/a> where we analyze the AFL <a target=_blank title='AFL 2014 Home and Away Ladder' href='http:\/\/www.bigfooty.com\/forum\/threads\/afl-2014-ladder-predictions.1038901\/page-99'>2014<\/a> Home and Away Ladder hoping that Collingwood has a better 2015!<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d13789' onclick='var dv=document.getElementById(\"d13789\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=iOS\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d13789' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d14276' onclick='var dv=document.getElementById(\"d14276\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=swift\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d14276' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>With more inspiration from Swift Fundamentals The Language of iOS Development by Mark Lassoff (ISBN 9780990402053), we continue on from yesterday&#8217;s Swift Dictionaries and Arrays Primer Tutorial as shown below, by talking about Operator Overloading today, we touch on some &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/swift-operator-overloading-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[296,459,626,752,764,997,1227,1319,1473],"class_list":["post-14276","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-data-type","tag-fractions","tag-ios","tag-mathematics","tag-member","tag-programming","tag-swift","tag-tutorial","tag-xcode"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/14276"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=14276"}],"version-history":[{"count":2,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/14276\/revisions"}],"predecessor-version":[{"id":20112,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/14276\/revisions\/20112"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=14276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=14276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=14276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}