Skip to content

Instantly share code, notes, and snippets.

@kmkrn
kmkrn / .swift
Created October 24, 2020 16:49
Drawing a triangle with Path
struct ContentView: View {
var body: some View {
Path { path in
path.move(to: CGPoint(x: 100, y: 100))
path.addLine(to: CGPoint(x: 100, y: 300))
path.addLine(to: CGPoint(x: 300, y: 300))
path.addLine(to: CGPoint(x: 100, y: 50))
}
.fill(Color.yellow)
@kmkrn
kmkrn / gist:946a856130f8dd8a64dc35b42ff72bda
Created February 12, 2019 19:33
A single-bordered UITextField
import UIKit
@IBDesignable class SingleBorderedTextField: UITextField {
private var bottomBorder = UIView()
private (set) var lineStrength: CGFloat = 1
@IBInspectable var lineWidth: CGFloat {
get {
return self.lineStrength
@kmkrn
kmkrn / RealmTransactionExample.swift
Created December 31, 2017 13:51
Realm transaction
realm.beginWrite()
cat.name = "Matilda"
try! realm.commitWrite()
@kmkrn
kmkrn / RealmMigration.swift
Created December 31, 2017 13:24
Realm migration example
let config = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
print("migration succeeded")
})
Realm.Configuration.defaultConfiguration = config
@kmkrn
kmkrn / RealmExample.swift
Created December 31, 2017 13:05
Realm objects instances are auto-updating
class Cat: Object {
@objc dynamic var catId: Int = 0
@objc dynamic var name: String = ""
@objc dynamic var breed: String = ""
@objc dynamic var weight: Double = 0
override static func primaryKey() -> String? {
return "catId"
}
@kmkrn
kmkrn / AppDelegate.swift
Last active December 31, 2017 01:56
LinkingObjects
let cats = realm.objects(Cat.self).filter("name = 'Tigger'")
for cat in cats {
realm.beginWrite()
cat.owner = cat.person.first
try! realm.commitWrite()
if let owner = cat.owner {
print("\(owner.firstName) \(owner.lastName)")
}
}
@kmkrn
kmkrn / AppDelegate.swift
Last active December 31, 2017 01:06
Realm migration
let config = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
print("migration succeeded")
})
Realm.Configuration.defaultConfiguration = config
@kmkrn
kmkrn / Cat.swift
Last active December 31, 2017 00:22
Foreign keys in Realm
@objc dynamic var owner: CatPerson? = nil
let person = LinkingObjects(fromType: CatPerson.self, property: "cats")
@kmkrn
kmkrn / AppDelegate.swift
Last active December 30, 2017 16:45
Deleting objects from realm
let realm = try! Realm()
try! realm.write {
realm.deleteAll()
}
let serializer = JSONSerializer()
serializer.serialize(input: "CatPeople")
import Foundation
import RealmSwift
class JSONSerializer {
func serialize(input sourceName: String) {
let path = Bundle.main.path(forResource: sourceName, ofType: nil)
let url = URL(fileURLWithPath: path!)
let jsonDecoder = JSONDecoder()
do {
let data = try Data(contentsOf: url)