title: Print specific value in Realm array using Swift
body: <p>I am new to Realm / Swift and trying to do simple prints. I have this simple code:</p>
  
  <pre><code>//
  //  ViewController.swift
  //  Click
  //
  
  import UIKit
  import RealmSwift
  
  class ViewController: UIViewController {
  
      var testclick : Results&lt;Click&gt;?
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
      }
  
  
      @IBAction func clickButton(sender: AnyObject) {
  
          do {
  
              let testclick = try Realm().objects(Click).filter("id == 1")
  
              // let totalclicks = testclick.totalclicks
  
              print(testclick)
  
          } catch {}
  
          let click = Click()
          click.id = 1
          click.totalclicks = 5
          click.name = "C. Boutrup"
  
          do {
              let realm = try Realm()
  
              try realm.write {
                  realm.add(click, update: true)
              }
  
          } catch {}
  
      }
  }
  </code></pre>
  
  <p>Result of print(testclick) is correct:</p>
  
  <pre><code>Results&lt;Click&gt; (
      [0] Click {
          id = 1;
          name = C. Boutrup;
          totalclicks = 5;
      }
  )
  </code></pre>
  
  <p>but how do I only get the value of for example totalclicks? Tried with let totalclicks = testclick.totalclicks but I won't work.</p>
  
  <p>When writing to Realm I want the total value of totalclicks to be total clicks from the Results + 1, something like click.totalclicks = totalclicks + 1. What is the correct code for that?</p>
  
  <p>Thanks in advance :-)</p>
  
  <p>Update:</p>
  
  <p>Both answers was resulting in great work, and you can se both solutions here:</p>
  
  <pre><code>//
  //  ViewController.swift
  //  Click
  //
  
  import UIKit
  import RealmSwift
  
  class ViewController: UIViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
      }
  
      @IBAction func clickButton(sender: AnyObject) {
  
          //initialize your realm
          let realm = try! Realm()
  
          //check if there is a Click with id == 1
          if let clickWithId1 = realm.objects(Click).filter("id == 1").first {
              do {
                  //if there is a Click with id == 1, increment its totalclicks by 1 and update it
  
                  try realm.write {
                      clickWithId1.totalclicks += 1
                      realm.add(clickWithId1, update: true)
                  }
  
              } catch {}
          } else {  // if there is no Click with id == 1, create it and add it to realm
              let click = Click()
              click.id = 1
              click.totalclicks = 5
              click.name = "C. Boutrup"
  
              do {
                  try realm.write {
                      realm.add(click, update: true)
                  }
  
              } catch {}
          }
      }
  }
  </code></pre>
  
  <p>Second solution:</p>
  
  <pre><code>//
  //  ViewController.swift
  //  Click
  //
  
  import UIKit
  import RealmSwift
  
  class ViewController: UIViewController {
  
      var testclick : Results&lt;Click&gt;?
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
      }
  
  
      @IBAction func clickButton(sender: AnyObject) {
  
          do {
  
              // let testclick = try Realm().objects(Click).filter("id == 1")
              // let name = testclick.name
              // print(testclick)
  
              // Get a list of objects back out of Realm
              let results = try Realm().objects(Click).filter("id == 1")
  
              // Access the first object from the list (results[0] would also work)
              let testclick = results.first
  
              // Print its total clicks value
              print(testclick!.totalclicks)
  
              // trying
              let myclicks = testclick!.totalclicks
  
              let click = Click()
              click.id = 1
              click.totalclicks = myclicks + 1
  
              print(click.totalclicks)
  
              let realm = try Realm()
  
              try realm.write {
                  realm.add(click, update: true)
              }
  
          } catch {}
  
      }
  }
  </code></pre>