nc14
nc14

Reputation: 559

Retrieve realm object equal to UUID

Hopefully a quick one that I'm stuck on.

I'm passing a UUID String through a segue to the next VC. This is definitely working because I am printing it on viewDidLoad.

What I want to do is retrieve the object with that UUID (workoutID). I'm getting a crash of :

"Terminating app due to uncaught exception 'Invalid value', reason: 'Expected object of type string for property 'workoutID' on object of type 'WorkoutSessionObject', but received: 0'"

When i try run the code below which I've adapted from other search related questions (I'm finding querying realm quite difficult to get my head round) :

class WorkoutSummaryController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // Holder for my UUID passed via Segue which is working 
    var workoutID = UUID().uuidString

    override func viewDidLoad() {
        print(workoutID)

        // Set up the query to retrieve the object with the UUID that has been passed
        let predicate = NSPredicate(format: "workoutID = %A", workoutID)

        // Run the query and print the result which crashes
        let test = try! Realm().objects(WorkoutSessionObject.self).filter(predicate)
        print (test)
        super.viewDidLoad()
    }
}

Any help appreciated :-)

Upvotes: 1

Views: 975

Answers (1)

David Pasztor
David Pasztor

Reputation: 54706

Your NSPredicate format is flawed. You should be using %@ for substituting variable's and %K for keypaths.

let predicate = NSPredicate(format: "workoutID = %@", workoutID)

Upvotes: 3

Related Questions