使用Swift

时间:2016-02-10 07:47:34

标签: swift realm

我是Realm / Swift的新手并尝试做简单的打印。我有这个简单的代码:

//
//  ViewController.swift
//  Click
//

import UIKit
import RealmSwift

class ViewController: UIViewController {

    var testclick : Results<Click>?

    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 {}

    }
}

打印结果(testclick)是正确的:

Results<Click> (
    [0] Click {
        id = 1;
        name = C. Boutrup;
        totalclicks = 5;
    }
)

但是我如何才能获得例如totalclicks的值?尝试使用totalclicks = testclick.totalclicks,但我不会工作。

写入Realm时,我希望totalclicks的总值为Results + 1的总点击次数,例如click.totalclicks = totalclicks + 1.正确的代码是什么?

提前致谢: - )

更新

这两个答案都带来了很好的工作,你可以在这里找到两个解决方案:

//
//  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 {}
        }
    }
}

第二个解决方案:

//
//  ViewController.swift
//  Click
//

import UIKit
import RealmSwift

class ViewController: UIViewController {

    var testclick : Results<Click>?

    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 {}

    }
}

2 个答案:

答案 0 :(得分:1)

如果&#39; id&#39; property是你对象的主键:

//
//  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 {}
        }
    }
}

答案 1 :(得分:0)

在您的代码中,testclick将是一个Results对象,它将是一个零或更多结果的数组。

要实际访问您的Click对象,您需要先将其从结果对象中删除。

// 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)

如果您需要进一步澄清,请告诉我们! :)