Ryan Belleview
Ryan Belleview

Reputation: 1

Swift NSPredicate String Variable doesn't Return Data

When using NSPredicate, why does a hard coded string work but a string variable does not work?

For example:

This code returns results:

let predicate = NSPredicate(format: "%K == %@", "level_id", "A2768F75-2293-4286-9ERA-D9888A823BF2")

This code does not return results:

let predicate = NSPredicate(format: "%K = %@", "level_id", String(levelId))

When debugging, printing out levelId returns the correct string from the first example: "A2768F75-2293-4286-9ERA-D9888A823BF2"

Thanks for the ideas and assistance!

Upvotes: 0

Views: 353

Answers (3)

Nazmul Hasan
Nazmul Hasan

Reputation: 10610

=, ==

The left-hand expression is equal to the right-hand expression.

but the == to make it case-insensitive

if so , most of describe it, no difference = and == in predicates

Upvotes: 1

A K M Saleh Sultan
A K M Saleh Sultan

Reputation: 2403

Try this

let predicate = NSPredicate(format: "%K = %@", "level_id", levelId as! String)

Upvotes: -1

chrismac4u
chrismac4u

Reputation: 29

What type is levelId? Swift's string interpolation looks at items that implement the Printable protocol. So your type may have a String property description or debugDescription, in which case casting to a String won't give you what you want.

I'd try a test where compare the two values. For example, does "A2768F75-2293-4286-9ERA-D9888A823BF2" == String(levelId)? What about levelId.description?

Upvotes: 2

Related Questions