iOS 11 and Swift 4 for Beginners by unknow

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



Copyright Disclaimer:
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.