Created
March 1, 2019 10:00
-
-
Save topherPedersen/8370eb64a2c1c08331aa56cce6eb2a57 to your computer and use it in GitHub Desktop.
Basic Realm Database for iOS Example
This file contains hidden or 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
| // | |
| // ViewController.swift | |
| // HelloRealm | |
| // | |
| // Created by Christopher Pedersen on 2/25/19. | |
| // Copyright © 2019 Christopher Pedersen. All rights reserved. | |
| // | |
| import UIKit | |
| import RealmSwift | |
| class ViewController: UIViewController { | |
| @IBOutlet weak var cookieCountLabel: UILabel! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Do any additional setup after loading the view, typically from a nib. | |
| let realm = try! Realm() | |
| let cookies = realm.objects(Cookie.self) | |
| let cookieCount: Int = cookies.count | |
| if cookieCount > 0 { | |
| cookieCountLabel.text = "Cookies: \(cookieCount)" | |
| } else { | |
| cookieCountLabel.text = "Cookies: 0" | |
| } | |
| } | |
| @IBAction func onBakeCookiesButtonClicked(_ sender: Any) { | |
| let realm = try! Realm() | |
| let newCookie = Cookie() | |
| newCookie.type = "Chocolate Chip" | |
| newCookie.size = "Large" | |
| let cookies = realm.objects(Cookie.self) | |
| try! realm.write { | |
| realm.add(newCookie) | |
| } | |
| let cookieCount: Int = cookies.count | |
| cookieCountLabel.text = "Cookies: \(cookieCount)" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment