Skip to content

Instantly share code, notes, and snippets.

@garsdle
garsdle / Injection1.swift
Last active March 22, 2018 21:01
Injection of a class into another
class Helper { /* I do helpful things */ }
class MainClass {
let helper: Helper
init(helper: Helper) {
self.helper = helper
}
}
@garsdle
garsdle / Injection2.swift
Last active March 22, 2018 21:45
Dependency Injection + protocol is magic
protocol Helper { }
class BestHelper: Helper { /* I do the best helping in accordance to Helper */ }
class MockHelper: Helper { /* Fake it till you make it */ }
class MainClass {
let helper: Helper
init(helper: Helper) {
self.helper = helper
@garsdle
garsdle / InitMapping.swift
Last active April 13, 2018 20:58
Init Mapping
struct User {
let id: Int
let name: String
init?(dictionary: [String: Any]) {
guard let id = dictionary["id"] as? Int else { return nil }
guard let name = dictionary["user_name"] as? String else { return nil }
self.id = id
self.name = name
@garsdle
garsdle / CodableMapping.swift
Last active April 13, 2018 21:00
Codable Mapping
struct User: Codable {
let id: Int
let name: String
enum CodingKeys: String, CodingKey {
case id
case name = "user_name"
}
}
@garsdle
garsdle / SeparateMapping.swift
Last active April 13, 2018 00:08
SeparateMapping
struct User {
let id: Int
let name: String
}
class UserMapper {
func map(_ dictionary: [String: Any]) -> User? {
guard let id = dictionary["id"] as? Int else { return nil }
guard let name = dictionary["user_name"] as? String else { return nil }
@garsdle
garsdle / MapperUnitTest.swift
Last active April 16, 2018 00:17
Map Unit Testing
func testJSONToUser() {
let userDictionary = mockUserAPI.getUserDictionary()
guard let user = UserMapper().map(userDictionary) else {
XCTFail("Failed to map User")
return
}
XCTAssert(user.id == 11235)
XCTAssert(user.name == "BobEsponja")
@garsdle
garsdle / DeepLink.swift
Last active March 15, 2019 01:20
An example of DeepLinking and makeIterator
struct AvatarView: View {
var image: UIImage?
var initials: String?
var body: some View {
OptionalView(image) { image in
Image(uiImage: image)
}
.fallbackView(initials) { initials in
Text(initials)
struct OptionalView<Value, Content>: View where Content: View {
var content: (Value) -> Content
var value: Value
init?(_ value: Value?, @ViewBuilder content: @escaping (Value) -> Content) {
guard let value = value else {
return nil
}
self.value = value
self.content = content
@garsdle
garsdle / ContentView.swift
Last active March 17, 2020 20:23
MirleftCard
struct ContentView: View {
@State var showNewCardView = false
@State var mirleftCards: [MirleftCard] = [
MirleftCard(image: Image("beach"), title: "The Beach", description: "The amazing view of the Grande Plage."),
MirleftCard(image: Image("sunset"), title: "Sunset", description: "The beautiful moroccan sunset."),
]
var body: some View {