iOS Swift Filter Array using NSPredicate crashing on run time
Clash Royale CLAN TAG#URR8PPP
iOS Swift Filter Array using NSPredicate crashing on run time
I have a cell Model
struct BeerCellModel: Hashable
var beer: Beer
static func == (lhs: BeerCellModel, rhs: BeerCellModel) -> Bool
return lhs.beer.id == rhs.beer.id
var hashValue: Int
return self.beer.id
AND
public struct Beer
public var abv: String
public var ibu: String
public var id: Int
public var name: String
public var style: String
public var ounces: Int
Now i have a array name 'items = [BeerCellModel]' of above cell model and i'm filtering array with param style
style
let value = ["tuborg", "budwiser", "bira"]
let query = value.map "SELF.beer.style CONTAINS[cd] ($0)" .joined(separator: " || ")
let predicate = NSPredicate(format: query)
let results = self.items.filter predicate.evaluate(with: $0)
But i'm getting run time crash
2018-08-08 00:38:01.787170+0530 BeerCrafts[3388:401950] ***
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<_SwiftValue 0x60400064dce0> valueForUndefinedKey:]: this
class is not key value coding-compliant for the key beer.'
What is wrong in array or predicate?
Update: Adding JSON response of Beer
Beer
"abv": "0.08",
"ibu": "35",
"id": 11,
"name": "Monks Blood",
"style": "Belgian Dark Ale",
"ounces": 12
,
"abv": "0.07",
"ibu": "65",
"id": 10,
"name": "Brew Free! or Die IPA",
"style": "American IPA",
"ounces": 12
,
"abv": "0.04",
"ibu": "17",
"id": 9,
"name": "Hell or High Watermelon Wheat",
"style": "Fruit / Vegetable Beer",
"ounces": 12
BeerCellModel
@AhmadF: The expected result should give items in which style matches in value array
– Abhishek Thapliyal
Aug 7 at 19:34
So far so good. Could you add
BeerCellModel
(which seems to be items
in your code snippet) so I can test my answer before I post it :)– Ahmad F
Aug 7 at 19:36
BeerCellModel
items
@AhmadF: You can make dummy model and test it. I have added JSON as well
– Abhishek Thapliyal
Aug 7 at 19:40
You probably mean simply
NSPredicate(format: "beer.style IN %@", value)
– vadian
Aug 7 at 20:03
NSPredicate(format: "beer.style IN %@", value)
3 Answers
3
At this point, there is no need to deal with NSPredicate
, the filter
method is handy enough for your case! all you should do is:
NSPredicate
filter
let filtered = items.filter value.contains($0.beer.style)
The filtered
is array of BeerCellModel
which should contains the objects that their beer.style
is one of the value
array elements.
filtered
BeerCellModel
beer.style
value
You can try
let value = ["tuborg", "budwiser", "bira"]
let query = value.map "self CONTAINS[cd] '($0)'" .joined(separator: " || ")
let predicate = NSPredicate(format: query)
let results = self.items.filter predicate.evaluate(with: $0.beer.style)
Can you explain as well
– Abhishek Thapliyal
Aug 7 at 20:12
Looks like you're trying to map an array of String
s, not an array of BeerCellModel
structs.
String
BeerCellModel
Wrong i m adding predicate query in filter. Google it for references
– Abhishek Thapliyal
Aug 7 at 19:42
Hi, this does not seem to be an attempt to solve the problem. For clarifications you should use comments.
– orgtigger
Aug 7 at 20:09
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Could you add the
BeerCellModel
array and mention what is the expected result?– Ahmad F
Aug 7 at 19:30