Swift: How to Create a Predicate with an Int Value

Swift: How do I create a predicate with an Int value?

When your data is safe or sanitized, you might try String Interpolation Swift Standard Library Reference. That would look something like this:

let thisSection = 1
let thisPredicate = NSPredicate(format: "sectionNumber == \(thisSection)")

NSPredicate compare with Integer

NSNumber is an object type. Unlike NSString, the actual value of NSNumber is not substitued when used with %@ format. You have to get the actual value using the predefined methods, like intValue which returns the integer value. And use the format substituer as %d as we are going to substitute an integer value.

The predicate should be,

predicateWithFormat:@"userID == %d", [stdUserNumber intValue]];

Compare integer with string in NSPredicate

The problem is that, although you specify integerValue, when using the predicate as part of a fetch, CoreData treats your string as a string. In string terms, 10 weeks is "less than" 6 weeks, so it's excluded from the results.

You should store the number (as an Int) instead of storing the string. Adding the text " weeks" is then something you do for display purposes. If you store the number, rather than the string, then CoreData can do a numerical comparison and your predicate will work as you expect.

If you really don't want to do that, there is one workaround, and one hack. The workaround is to filter the objects in memory rather than as part of a fetch. The integerValue will then be correctly interpreted and the comparison will be numerical.

The hack is to force CoreData to treat your string as though it is a number, by using a numeric function, like abs (assuming you only have a positive number of weeks, taking the absolute value leaves the value unchanged):

predicates.append(NSPredicate(format: "abs:(scheduleStr) >= 6"))

Invalid value when query on Int in realm

You shouldn't be using String interpolation when creating NSPredicates, since even though it is supported, it is really easy to mess up the predicate format. Simply use %@ for substituting variable values into the predicate.

let allRooms = Users.realm.objects(Rooms.self).filter("placeId == %@ AND floor == %@",placeId, floor)

Some further improvements to your code: don't use nil check, then force unwrapping, use optional binding when working with Optionals.

if let placeName = placeName , let floor = floor {

Also don't add an initial value to Arrays when creating them, instead of var roomNames = [""] and var roomTypes = [""], do

var roomNames = [String]()
var roomTypes = [String]()

Integer Comparison through Predicate

Try cast integer to string and compare it

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"electronic_id.stringValue CONTAINS[c] %@",searchText];

Swift - Dynamically generate NSPredicate?

value as NSObject does the trick:

var keyValues: [String: AnyObject] = ["name" : "John Doe", "age" : 39]
var predicates = [NSPredicate]()

for (key, value) in keyValues {
var predicate = NSPredicate(format: "%K = %@", key, value as NSObject)
predicates.append(predicate)
}

var compundPredicate = NSCompoundPredicate.andPredicateWithSubpredicates(predicates)
println(compundPredicate)

Output:


name == "John Doe" AND age == 39

Update for Swift 3/4:

let keyValues = ["name" : "John Doe" as NSObject, "age" : 39 as NSObject]
var predicates = [NSPredicate]()

for (key, value) in keyValues {
let predicate = NSPredicate(format: "%K = %@", key, value)
predicates.append(predicate)
}

let compundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)

Or shorter:

let keyValues = ["name" : "John Doe" as NSObject, "age" : 39 as NSObject]
let predicates = keyValues.map { NSPredicate(format: "%K = %@", $0.key, $0.value) }
let compundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)


Related Topics



Leave a reply



Submit