Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
What to Upload to SlideShare
What to Upload to SlideShare
Loading in …3
×
1 of 8

Extensions

0

Share

Download to read offline

Extensions

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Extensions

  1. 1. Unit 5—Lesson 2: Extensions
  2. 2. Extensions extension SomeType { // new functionality to add to SomeType goes here }
  3. 3. Adding computed properties extension UIColor { static var favoriteColor: UIColor { return UIColor(red: 0.5, green: 0.1, blue: 0.5, alpha: 1.0) } }
  4. 4. Adding instance or type methods Apple ➔ Apples Song ➔ Songs Person ➔ People Tennis court ➔ Tennis courts
  5. 5. Adding instance or type methods extension String { func pluralized() -> String { // Complex code that takes the current value (self) and returns the plural version } } var apple = "Apple" var person = "Person" print(apple.pluralized()) print(person.pluralized()) Apples People
  6. 6. Organizing code class Restaurant { let name: String var menuItems: [MenuItem] . . . } extension Restaurant { func add(menuItem: MenuItem) func remove(menuItem: MenuItem) // Other methods related to the Restaurant }
  7. 7. Lab: Extensions Unit 5—Lesson 2 Open and complete the exercises in Lab - Extensions.playground
  8. 8. © 2017 Apple Inc. This work is licensed by Apple Inc. under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license.

×