gpt4 book ai didi

swift - 使用 Swift 在 Realm 数组中打印特定值

转载 作者:行者123 更新时间:2023-11-28 06:49:29 26 4
gpt4 key购买 nike

我是 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 的值?尝试使用 let totalclicks = testclick.totalclicks 但我不会工作。

当写入 Realm 时,我希望 totalclicks 的总值是结果的总点击次数 + 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 {}

}
}

最佳答案

如果“id”属性是您对象的主键,也许可以试试这个:

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

关于swift - 使用 Swift 在 Realm 数组中打印特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35309505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com