Created
December 9, 2016 05:24
-
-
Save jamesrochabrun/2e7b8e43263e5a9868c5ba963e096264 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
//ENUMS | |
enum Day { | |
case monday | |
case tuesday | |
case wednesday | |
case thursday | |
case friday | |
case saturday | |
case sunday | |
} | |
enum DayType { | |
case weekend, weekday | |
} | |
func dayType(for day:Day) -> DayType { | |
switch day { | |
case .saturday,.sunday: | |
return .weekend | |
case .monday, .tuesday, .wednesday, .thursday, .friday: | |
return .weekday | |
} | |
} | |
dayType(for: Day.saturday) | |
func isNotificationMuted(on type:DayType) -> Bool { | |
switch type { | |
case .weekend: return true | |
case .weekday: return false | |
} | |
} | |
isNotificationMuted(on: .weekday) | |
//ENUMSAND CLASSES EXAMPLE | |
class Point { | |
var x: Int | |
var y: Int | |
init(x: Int, y: Int) { | |
self.x = x | |
self.y = y | |
} | |
} | |
enum Direction { | |
case left | |
case right | |
case up | |
case down | |
} | |
class Robot { | |
var location: Point | |
init() { | |
self.location = Point(x: 0, y: 0) | |
} | |
func move(_ direction: Direction) { | |
// Enter your code below | |
switch direction { | |
case .up: | |
self.location.y += 1 | |
case .down: | |
self.location.y -= 1 | |
case .right: | |
self.location.x += 1 | |
case .left: | |
self.location.x -= 1 | |
} | |
} | |
} | |
//ASSOCIATED VALUES ENUMS | |
//strings characters, integres or floats and any kinf of object | |
//use this when you have an enum with dynamic values | |
enum ColorComponent { | |
case rgb(red: Float, green: Float, blue: Float, alpha: Float) | |
case hsb(hue: CGFloat,saturation: CGFloat,brightness: CGFloat, alpha: CGFloat) | |
func color() -> UIColor { | |
switch self { | |
case .rgb(let red, let green, let blue, let alpha): | |
return UIColor(colorLiteralRed: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha) | |
case .hsb(let hue, let saturation, let brightness, let alpha): | |
return UIColor(hue: hue/360.0, saturation: saturation/100.0, brightness: brightness/100.0, alpha: alpha) | |
} | |
} | |
} | |
let red = ColorComponent.rgb(red:61.0,green: 20.0 , blue: 10.0, alpha:1).color() | |
let colornew = ColorComponent.hsb(hue:61.0,saturation:20.0,brightness:10.0,alpha:1) | |
enum mobilePhone { | |
case iphone(String) | |
case android(String) | |
case windowsPhone(String) | |
case Blackberry(String) | |
} | |
let iphone = mobilePhone.iphone("7 Plus") | |
enum Barcode { | |
case upc(Int, Int, Int, Int) | |
case qrcode(String) | |
} | |
var productBarCode = Barcode.upc(2, 8, 8, 8) | |
productBarCode = .qrcode("abc") | |
//ucomment this to see the error | |
//productBarCode.upc | |
//ENUM METHODS | |
// Example of UIBarButtonItem instance | |
//UI TO THINK ABOUT | |
enum BarButton { | |
case done(title: String) | |
case edit(title: String) | |
func button() -> UIBarButtonItem { | |
switch self { | |
case .done(let title): | |
return UIBarButtonItem(title: title, style: .done, target: nil, action: nil) | |
case .edit(let title): | |
return UIBarButtonItem(title: title, style: .plain, target: nil, action: nil) | |
} | |
} | |
} | |
let button = BarButton.done(title: "Save").button() | |
//ENUM WITH RAW VALUES | |
//are dafault values and muts be of the same type | |
//strings characters, integres or floats only | |
//use this when you have an enum with default values | |
enum Coin: Double { | |
case penny = 0.01 | |
case nickel = 0.05 | |
case dime = 0.1 | |
case quarter = 0.25 | |
} | |
let coins : [Coin] = [.penny, .dime, .dime, .quarter, .quarter, .nickel] | |
func sum(having coins:[Coin]) -> Double { | |
var total: Double = 0 | |
for coin in coins { | |
total += coin.rawValue | |
} | |
return total | |
} | |
//for integers the integer autoincrements by one | |
enum Dia: Int { | |
case monday = 1 | |
case tuesday | |
case wednesday | |
case thursday | |
case friday | |
case saturday | |
case sunday | |
} | |
//with strings you get the name of the case key converted to a string | |
enum HTTP: String { | |
case post | |
case get | |
case put | |
case delete | |
} | |
HTTP.delete.rawValue | |
//initializing an enum with a raw value | |
enum HTTPStatuCode: Int { | |
case success = 200 | |
case forbidden = 403 | |
case unauthorized = 401 | |
case notFound = 404 | |
} | |
let statusCode = 200 | |
//so check this out if you pass the Int (the raw value) you get the key | |
//this is a failable initializer , that means that any variable returned will be an optional because can be nil, and because of that we need to wrap it in a if let or guard statement | |
if let httpStatusCode = HTTPStatuCode(rawValue: statusCode) { | |
print(httpStatusCode) | |
} | |
//code challenge | |
enum Compass: Int { | |
case north = 1 | |
case south | |
case east | |
case west | |
} | |
let direction = Compass(rawValue: 2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment