How to define SDK and Deployment Target in iOS

Issue #33 I see that my answer to the question What’s the meaning of Base SDK, iOS deployment target, Target, and Project in xcode gets lots of views, so I think I need to elaborate more about it Good read iOS 7 TechTalk - Architecting Modern Apps, Part 2 Configuring a Project for SDK-Based Development Base SDK We can’t configure this anymore, as Xcode will use the latest SDK. For Xcode 7, the SDK is iOS 9 If we upgrade Xcode, it will use the newer version of the SDK....

May 10, 2017 · 4 min · 701 words · Khoa

How to group digits in Swift

Issue #26 When working on Scale I think it’s good to have a way to group the digit so that it is easier to reason Luckily, Swift already supports this. See The Swift Programming Language - Numeric Literals Numeric literals can contain extra formatting to make them easier to read. Both integers and floats can be padded with extra zeros and can contain underscores to help with readability. Neither type of formatting affects the underlying value of the literal...

May 10, 2017 · 1 min · 180 words · Khoa

How to make lighter AppDelegate in iOS

Issue #24 There is Lighter View Controllers, and there is Lighter AppDelegate, too Since working with iOS, I really like the delegate pattern, in which it helps us defer the decision to another party. The iOS application delegates its event to AppDelegate, which over time will be a big mess. Usually, the AppDelegate is where you put your root view controller setup, crash tracking, push notification, debugging, … and we just somehow violent the Single Responsibility principle....

May 10, 2017 · 3 min · 453 words · Khoa

A set of NSObject

Issue #21 The other day I was trying t http://mgrebenets.github.io/swift/2015/06/21/equatable-nsobject-with-swift-2 http://stackoverflow.com/questions/33319959/nsobject-subclass-in-swift-hash-vs-hashvalue-isequal-vs https://useyourloaf.com/blog/swift-hashable/

May 5, 2017 · 1 min · 12 words · Khoa

How to handle RefreshControl in iOS

Issue #20 The other day I was doing refresh control, and I saw this Swift Protocols with Default Implementations as UI Mixins extension Refreshable where Self: UIViewController { /// Install the refresh control on the table view func installRefreshControl() { let refreshControl = UIRefreshControl() refreshControl.tintColor = .primaryColor refreshControl.addTarget(self, action: #selector(handleRefresh(_:)), for: .valueChanged) self.refreshControl = refreshControl if #available(iOS 10.0, *) { tableView.refreshControl = refreshControl } else { tableView.backgroundView = refreshControl } } } Protocol extension is cool but somehow I’m not a fan of it....

May 4, 2017 · 1 min · 181 words · Khoa

Swift snippets

Issue #17 I always forget how to write correct #available( or #if swift(>=3.0) or just lazy to write required init?(coder aDecoder: NSCoder) every time I make a subclass. That’s why I made SwiftSnippets to save time for these tedious tasks. Installation is easy with script, so you should give it a try. I can’t recommend this enough, it saves me tons of time

May 2, 2017 · 1 min · 63 words · Khoa

Composition in Realm

Issue #13 There is time we have models that have some kind of inheritance, like Dog, Cat, Mouse can be Animal. We can use composition to imitate inheritance, we just need to make sure it has unique primary key Cat and Dog These are pretty much basic Realm objects class Dog: Object { dynamic var id: Int = 0 required convenience init(json: [String: Any]) { self.init() id <- json.integer(key: "id") } } class Cat: Object { dynamic var id: Int = 0 required convenience init(json: [String: Any]) { self....

May 2, 2017 · 1 min · 177 words · Khoa

How to execute an action only once in Swift

Issue #10 There is time we want to execute an action only once. We can surely introduce a flag, but it will be nicer if we abstract that out using composition. Then we have class Once { var already: Bool = false func run(@noescape block: () -> Void) { guard !already else { return } block() already = true } } Usage class ViewController: UIViewController { let once = Once() override func viewDidAppear(animated: Bool) { super....

May 2, 2017 · 1 min · 133 words · Khoa

Primary key in Realm

Issue #4 Realm is great. But without primary key, it will duplicate the record, like https://github.com/realm/realm-java/issues/2730, http://stackoverflow.com/questions/32322460/should-i-define-the-primary-key-for-each-entity-in-realm, … So to force ourselves into the good habit of declaring primary key, we can leverage Swift protocol Create primary constrain protocol like this protocol PrimaryKeyAware { var id: Int { get } static func primaryKey() -> String? } and conform it in out Realm object class Profile: Object, PrimaryKeyAware { dynamic var firstName: String = "" dynamic var lastName: String = "" dynamic var id: Int = 0 override static func primaryKey() -> String?...

April 26, 2017 · 1 min · 164 words · Khoa

Configuration closure in Swift

Issue #2 When I was reading through Swinject, I found something interesting https://github.com/Swinject/Swinject/blob/master/Sources/Container.swift public convenience init(parent: Container? = nil, registeringClosure: (Container) -> Void) { self.init(parent: parent) registeringClosure(self) } The init has a closure that makes configuration easy, much like a Builder pattern. So I think we can learn from that and make a Configurable protocol protocol Configurable: class {} extension Configurable { func config(block: (Self) -> Void) -> Self { block(self) return self } } extension NSObject : Configurable {} With this, we can init some class with less hassle...

April 26, 2017 · 1 min · 102 words · Khoa

Hello world, again

Issue #1 I’ve used Wordpress, then moved to GitHub Pages with Jekyll, Octopress, Hexo, Hugo. You can view my page here http://fantageek.com/. It was good with all the custom themes and Disqus But then I was a bit lazy with all the commands generate, commit, deploy, it hinders me from writing, so I moved to Medium. The only thing I like about Medium is its discovery, your posts have high chanced of finding and viewing by people....

April 26, 2017 · 1 min · 204 words · Khoa