filteredArrayUsingPredicate does not exist in swift Array
Swift arrays have a .filter
method that takes a closure -- this will do it:
let filteredArray = arrayOfUsers.filter() { $0.userID == "1" }
Closures can be simplified in a variety of ways. The full declaration of the closure would look more like this:
var filteredArray = arrayOfUsers.filter( { (user: UserDetails) -> Bool in
return user.userID == "1"
})
The difference between the two is that the first is using trailing closure syntax, shorthand argument names, type inference, and implicit return. You can read more about closures in Apple's Swift documentation.
Swift - Predicate to filter an array by a property of member array
Using string interpolation in predicate format strings is never a good
idea. The correct form of your predicate would be
let searchPredicate = NSPredicate(format: "ANY arr.uid IN %@", ids)
However that checks if any of the uids is in the given list.
To check if all uids are in the given list, the following predicate
should work:
let searchPredicate = NSPredicate(format: "SUBQUERY(arr.uid, $x, $x IN %@).@count = %d", ids, ids.count)
The same can be achieved without predicates in "pure" Swift 3 as
filtered3 = available3.filter { $0.arr.filter { ids.contains($0.uid) }.count == ids.count }
or
filtered3 = available3.filter { Set(ids).isSubset(of: $0.arr.map { $0.uid }) }
How to filter an array using NSPredicate in swift 3
The native Swift equivalent to the ObjC code is
let filteredArray = arrayDirectory.filter { ($0["displayName2"] as! String).range(of: searchText!, options: [.diacriticInsensitive, .caseInsensitive]) != nil }
assuming arrayDirectory
is a native Swift Array
. It considers also the case insensitive
and diacritic insensitive
parameters.
NSPredicate to filter an Array of Dictionaries (Swift)
I agree with the comment by @luk2302, in that using the Swift Array
filter
method is nicer in general than filtering with an NSPredicate
. If you're worried about performance across a large data set you should have your content in CoreData
and use an NSPredicate
as part of an NSFetchRequest
with proper indexing setup in your data model.
Having said that, filtering by NSPredicate
is not explicitly supporting with the Swift Array
type, so you would have to cast to NSArray
to use it. It would look something like this:
let filteredArray = (arrayofDictionary as NSArray).filteredArrayUsingPredicate(speciesPredicate)
Problem when an array element i'm trying to filter for does not exist
You can make a pre check before accessing the index for messageIDElement
, readStatusElement
and filteredMessageID
that it not having count 0.
if filteredMessageID.count != 0 {
if readStatusElement.count != 0 && messageIDElement != 0 {
// Your code
}
}
NSPredicate is not working on some object
Really its strange... i unable to find the solution for this. But i found the alternate way to filter the array without using predicate.
tableDataArray = dataSourceArray.filter {$0.firstName.containsString(searchText) || $0.lastName.containsString(searchText) || $0.action.containsString(searchText) }
Related Topics
Swiftui How to Animate Each Character in Textfield
MAC App to Switch Between /Etc/Hosts Files, How to Allow Access
Why There Is a Overflow with Swift Language When Assign a 8 Bits Binary Value to a Var of Int8 Type
Swift Calculate Time for Timers Running in Background
Moving Keyboard When Editing Multiple Textfields with Constraints Swift
Skaction.Movebyx Not Setting Physicsbody.Velocity
Sending Firebase Push Notifications to Logged-In Users Only
Hovering a Modelentity in Front of Arcamera
Swift Delegate Beetween Two Vc Without Segue
Swift Firestore Search for Users
Skscene Becomes Unresponsive While Being Idle
Use Quick Look Inside a Swift Cocoa Application to Preview Audio Files
Disable Https Get Certificate Check in Swift 5
How to Create a Portal Effect in Arkit Just Using the Scenekit Editor