Can't use in/contains operator with collection'
You're getting this error because you're trying to use a CONTAINS
predicate on SearchResult
objects, which aren't collections with a defined notion of CONTAINS
. This error occurs at runtime because NSPredicate
parses and processes its string at runtime. It's MUCH more preferable to use native swift facilities for this:
let searchTerm = searchController.searchBar.text!
let array = results.filter { result in
return result.studentID.contains(searchTerm) ||
result.something.contains(searchTerm) // and so on...
}
Search Bar Swift 3 - Can't use in/contains operator with collection
NSPredicate is a Cocoa feature that lives in the Objective-C world. It's never going to work on an array of UserData because UserData is a Swift struct — and Objective-C cannot see a Swift struct at all (and even if it could, it certainly can't see any type namespaced inside a class, as your UserData is).
You would have an easy time of this if you simply used the built-in Swift filter
method to filter the dataSource
array. For example (if this is what you're trying to do):
let array = self.dataSource.filter{$0.fullname.contains(searchString)}
how to solve NSPredicate error 'Can't use in/contains operator with collection 8 (not a collection)
The probably means that the mood
attribute is stored as Number, not as String. "CONTAINS" works only with strings. The following predicate should work:
NSNumber *mood = @(7);
NSString *filter = @"%K == %@";
NSPredicate *getMoodPred = [NSPredicate predicateWithFormat:filter, @"mood", mood];
using NSNumber
on the rhs and ==
as comparator.
iPhone Predicate error Can't use in/contains operator with collection in search
It looks like FIRST is being interpreted as an aggregate operation. Try changing it to firstName. This will make it clearer for coredata and also your future self (some time in the future you may add an address with firstLine for street address which could be confusing)
NSPredicate crash on CONTAINS?
I was able to reproduce your problem. This happens if the displayValue
of one of the objects
is not a NSString
, but some different type.
From your error message, I assume that you assigned an NSNumber
, for example
obj.displayValue = @100;
somewhere in your code. The "CONTAINS" predicate works only with strings, so you must assign
only string values to the property.
Therefore, you should define the type of the property asNSString *
instead of id
, and check the assignments to that property.
If you really need to keep the id
type and store different kinds of objects in that property,
then you cannot use the "CONTAINS" operator in the predicate. A direct comparison
with @"displayValue == %@"
would work, however.
UPDATE: As a workaround, you could use the description
method, which converts any object
to a string, in particular it converts a NSNumber
to its string representation. So the following could work:
[NSPredicate predicateWithFormat:@"displayValue.description CONTAINS[cd] %@", searchString];
The drawback is that the exact description
format is not documented.
Another solution could be to use a block-based predicate, where you can check the type
of each object and perform the appropriate comparison.
Related Topics
Closure:Use Unresolved Identifier 'Self'
How to Get Part of a String in Swift
Swift 3: Converting Data to String Returns a Nil Value
Ambiguous Use of Operator '-' in Swift with 'Abs()'
How to Set the iOS13 Uisegmentedcontrol Backgroundcolor to White
Swift Firestore Check If Documents Exists
Using @Discardableresult for Closures in Swift
Left Aligned Horizontal Stackview and Top Aligned Vertical Stackview
Swift - Getting Only Alphanumeric Characters from String
Swift Language Statically or Dynamically Dispatched
Generic Method Override Not Working in Swift
Xcode 8 Swift 3 Pitch-Altering Sounds
How to Pass Image Value to The Imageslideshow Using Swift
How to Remove Node from Parent If Touched More Than Once
Iwatch: Wkinterfacelabel How to Stop Text from Being Cut Off with "..." at The End of a Label
Provide Simple Method to Get Current Speed (Implementing Speedometer)