Request in Realm (using Swift)

I play with realm.io. I wrote a couple of objects, and now I want to request them. My data class:

class Sample : RLMObject { dynamic var sampleKey : String = "" } 

and my request code

 @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) } 

The web interface and the RLMObject header indicate that I should be able to say Sample.objectsWhere ("sampleKey = 'SampleValue') (or similar), but objectsWhere gives a compilation error because the function does not exist and there is no autocomplete for it. So I tried with objectForPredicate instead, but it says that the type is "any" (tearing out the headers, I think it's equal to the ObjC 'id' type in Realm lingo). What am I doing wrong here? I try to be explicit explicit, don't forget use NSString instead of String and NSArray instead of Array, but still something is interpreted as an "id" instead of a spesific type.

Any suggestions?

Greetings

-Nik

+6
source share
2 answers

Your code works fine for me with Xcode 6 beta 5. By the way, you don't need to explicitly use NSArray and NSString here - Swift will use objective-c types for you. The following works for me and prints out the object that I 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 } } 

Output:

 RLMArray <0x7fe8241218c0> ( [0] Sample { sampleKey = SampleValue; } } 

Note that the Realm objectsWithPredicate method returns you a Realm array, not a regular array.

+3
source

Will NSArray always return the equivalent of [AnyObject]? Even when setting the type of a string, when creating it, other objects will not necessarily know the type, if several can be assumed.

I would try to create an array of type [String] and pass it instead, or use NSPredicate, which takes arguments directly (predicateWithFormat: arguments :), and not in the array - maybe this will work?

Finally, I am not familiar with the kingdom, but the template that I found if something is not available in swift is that it is available, but there is only another way to do it. for example, is there an array of objects in RLMObject? If so, you can either do objects.filter ({$ 0.key == value}) or a full block (which allows you to debug using breakpoints, etc.), which I personally prefer to use NSPredicates.

0
source

Source: https://habr.com/ru/post/973528/


All Articles