iOS 11 and Swift 4 for Beginners by unknow
Author:unknow
Language: eng
Format: epub
Publisher: Ray Wenderlich
let date = SimpleDate(month: "October") date.monthsUntilWinterBreak(from: date) // 2
If you think about this code for a minute, you’ll realize that the method’s definition is awkward. There must be a way to access the content stored by the instance instead of passing the instance itself as a parameter to the method. It would be so much nicer to call this:
date.monthsUntilWinterBreak() // Error!
Introducing self
A structure definition is like a blueprint, whereas an instance is a real object. To access the value of an instance, you use the keyword self inside the structure. The Swift compiler passes it in to your method as secret parameter. The method definition transforms into this:
// 1 func monthsUntilWinterBreak() -> Int { // 2 return months.index(of: "December")! - months.index(of: self.month)! }
Here’s what changed:
Now there’s no parameter in the method definition.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
A Swirl of Ocean by Melissa Sarno(50035)
The Book of Dreams (Saxon Series) by Severin Tim(32893)
Cecilia; Or, Memoirs of an Heiress — Volume 1 by Fanny Burney(32023)
Cecilia; Or, Memoirs of an Heiress — Volume 3 by Fanny Burney(31438)
Cecilia; Or, Memoirs of an Heiress — Volume 2 by Fanny Burney(31386)
Call Me by Your Name by André Aciman(19874)
Eleanor and Park by Rainbow Rowell(15083)
Always and Forever, Lara Jean by Jenny Han(14440)
Norse Mythology by Gaiman Neil(12791)
Crooked Kingdom: Book 2 (Six of Crows) by Bardugo Leigh(11946)
Shadow Children #03 - Among the Betrayed by Margaret Peterson Haddix(11587)
Among the Betrayed by Margaret Peterson Haddix(11392)
For the Love of Europe by Rick Steves(11339)
Twisted Palace by Erin Watt(10821)
Six of Crows by Leigh Bardugo(9660)
They Both Die at the End by Adam Silvera(9450)
P.S. I Still Love You by Jenny Han(9298)
Fangirl by Rainbow Rowell(8771)
Thirteen Reasons Why by Jay Asher(8420)
