Skip to content

Instantly share code, notes, and snippets.

@jamesrochabrun
Last active February 20, 2019 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesrochabrun/c4fa6ce816168337310400ae2ace1ecd to your computer and use it in GitHub Desktop.
Save jamesrochabrun/c4fa6ce816168337310400ae2ace1ecd to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
//Extensions
//in Swift we can use extensions to add functionality to an existing class, structure, enumeration or even a protocol type.
//TYPE EXTENSIONS
extension Int {
//computed property
var isOdd: Bool {
return self % 2 != 0
}
}
3.isOdd
2.isOdd
//rules
//can add computed properties
//cannot add stored properties or property observers
//can add new type and instance methods to a type
//can define nested types, so you can add a new struct inside of an existing class or struct.
//you can also add convenience initializers
//you can not add a designated initializer for the same reason you can add stored properties, if not you have to rewrite the implementation and thats not posible because the type is closed source
//example
extension String {
func add(number: Int) -> Int? {
guard let convertedNumber = Int(self) else{
return nil
}
return convertedNumber + number
}
}
String.add("3")
//Protocol Conformance Through extension
//1 defining the protocol
protocol UniquelyIdentifiable {
var uuid: Int { get }
}
//using composition
//defining the extension of the type and conform it in the protocol
extension UIView: UniquelyIdentifiable {
//computed property
var uuid: Int {
return hash
}
}
// now you can access to the value of the computed property inside the extension
let view = UIView()
view.uuid
view.hash
//another example
protocol PrettyPrintable {
var prettyDescription: String { get }
}
struct User {
let name: String
let ID: Int
}
extension User: PrettyPrintable {
var prettyDescription: String {
return "\(self.name) \(self.ID)"
}
}
//PRTOCOL EXTENSIONS
//when you extend a protocol you can provide implementations to any method or property requirements that you have defined
protocol RandomNumberGenerator {
func random() -> Int
}
extension RandomNumberGenerator {
//defining the implementation of the protocol in the extension
func random() -> Int {
return Int(arc4random())
}
}
class Generator: RandomNumberGenerator {
//you can "override" the implementation of the protocol declared in the extension
func random() -> Int {
return 2
}
}
//here the compiler its ok beacuse a default implementation has been defined in the protocol and the conforming type generator can use that.
let generator = Generator()
generator.random()
//another example
protocol Person {
var firstName: String { get }
var lastName: String { get }
var fullName: String { get }
}
//REMEMBER THAT WE CAN ONLY PROVIDE DEFAULT IMPLEMENTATIONS FOR A PROTOCOL THROUGH AN EXTENSION
extension Person {
var fullName: String {
return "\(firstName) \(lastName)"
}
}
struct Player: Person {
//here the compiler it's not giving errors for the missing var declared in the protocol beacuse it has a default implementation in an extension, try to comment the extension and the error will show up.
let firstName: String
let lastName: String
}
//now create an instance
let player = Player(firstName: "richard", lastName: "geere")
print(player.fullName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment