Skip to content

Instantly share code, notes, and snippets.

@AppleCEO
AppleCEO / directionToHead.swift
Last active May 22, 2019 11:40
directionToHead
var directionToHead: CompassPoint
directionToHead = .south
switch directionToHead {
case .north:
print("Lots of planets have a north")
case .south:
print("Watch out for penguins")
case .east:
print("Where the sun rises")
case .west:
let somePlanet = Planet.earth
switch somePlanet {
case .earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
// Prints "Mostly harmless"
@AppleCEO
AppleCEO / Beverage.swift
Created May 21, 2019 14:32
Enum example
enum Beverage: CaseIterable {
case coffee, tea, juice
}
let numberOfChoices = Beverage.allCases.count
print("\(numberOfChoices) beverages available")
// Prints "3 beverages available"
@AppleCEO
AppleCEO / Beverage.swift
Created May 21, 2019 14:35
enum example
for beverage in Beverage.allCases {
print(beverage)
}
// coffee
// tea
// juice
enum JsonType {
case int(Int)
case string(String)
case bool(Bool)
case object([String: JsonType])
case array([JsonType])
}
@AppleCEO
AppleCEO / jsonExample.swift
Last active May 22, 2019 07:14
jsonType's example
var jsonInt: JsonType = JsonType.int(3)
jsonInt = JsonType.int(4)
var jsonString = JsonType.string("hi")
var jsonBool = JsonType.bool(false)
var jsonObject = JsonType.object(["예제": jsonBool])
var jsonArray = JsonType.array([jsonInt, jsonString, jsonBool, jsonObject])
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
@AppleCEO
AppleCEO / productBarcode.swift
Created May 22, 2019 07:29
associated value example
var productBarcode = Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
@AppleCEO
AppleCEO / DevideCharacter.swift
Created May 22, 2019 07:32
rawValue example
enum DevideCharacter: Character {
case squareBracketOpen = "["
case squareBracketClose = "]"
case curlyBracketOpen = "{"
case curlyBracketClose = "}"
case colon = ":"
case whiteSpace = " "
case comma = ","
}
@AppleCEO
AppleCEO / rawValue.swift
Created May 22, 2019 07:37
rawValue example
if input.first == DevideCharacter.squareBracketOpen.rawValue {
print("첫 글자는 열리는 대괄호")
}