问题描述
我正在玩realm.io。我写了几个对象,现在我想查询它们。我的数据类:
I'm toying with realm.io. I've written a couple of objects, and now I want to query for them. My data class:
class Sample : RLMObject {
dynamic var sampleKey : String = ""
}
和我的查询代码
@IBAction func readLocalRecord(sender: UIButton) {
let s : NSString = NSString.stringWithString("sampleKey == SampleValue")
let p : NSPredicate = NSPredicate(format: "sampleKey = %@", argumentArray: NSArray(object: NSString.stringWithString("SampleValue")))
// the following throws exception, that I cannot catch in Swift:
// 'Unsupported predicate value type', reason: 'Object type any not supported'
let r = Sample.objectsWithPredicate(p)
}
网站和RLMObject的标题表示我应该可以说Sample.objectsWhere(sampleKey ='SampleValue')(或类似的) ),但objectsWhere给出一个编译错误抱怨函数不存在,并且没有autocomp拿它来。所以我尝试使用objectsForPredicate,但是这表示类型为'any'(通过标题挖掘,我发现这等于ObmC在Realm术语中的'id'类型)。我在这做错了什么?我试着显得非常明确,确保使用NSString而不是String和NSArray而不是Array,但仍然有些东西被解释为'id'而不是特定类型。
The webside, and the header of RLMObject, indicate that I should be able to say Sample.objectsWhere("sampleKey = 'SampleValue'") (or similar), but objectsWhere gives a compile error complaining the function isn't there, and there's no autocomplete for it. So I tried with objectsForPredicate instead, but this says that the type 'any' (digging through the headers, I find that this equals ObjC's 'id' type in Realm lingo). What am I doing wrong here? I try to be veery explicit, being sure to use NSString instead of String and NSArray instead of Array, but still something is interpreted as 'id' instead of a spesific type.
有什么建议吗?
干杯
-Nik
推荐答案
您的代码适用于Xcode 6 beta 5.顺便提一下,您不需要显式使用 NSArray
和 NSString
此处 - Swift将为您桥接到objective-c类型。以下适用于我并打印出我期望看到的对象:
Your code works fine for me with Xcode 6 beta 5. Incidentally, you don't need to explicitly use NSArray
and NSString
here - Swift will bridge to objective-c types for you. The following works for me and prints out the object I'd expect to see:
import Realm
class Sample : RLMObject {
dynamic var sampleKey : String = ""
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func readLocalRecord() {
// create some sample records
RLMRealm.defaultRealm().beginWriteTransaction()
var s = Sample()
s.sampleKey = "Testing"
RLMRealm.defaultRealm().addObject(s)
var s2 = Sample()
s2.sampleKey = "SampleValue"
RLMRealm.defaultRealm().addObject(s2)
RLMRealm.defaultRealm().commitWriteTransaction()
let p : NSPredicate = NSPredicate(format: "sampleKey = %@", argumentArray: [ "SampleValue" ])
let r = Sample.objectsWithPredicate(p)
println(r)
}
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
readLocalRecord()
return true
}
}
输出为:
RLMArray <0x7fe8241218c0> (
[0] Sample {
sampleKey = SampleValue;
}
}
请注意,Realm的 objectsWithPredicate
方法会返回一个Realm数组,而不是普通数组。
Note that Realm's objectsWithPredicate
method returns you a Realm array, not a normal Array.
这篇关于在Realm中查询(使用Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!