I want to search in array of objects in swift
but I didn't know how :(
I tried
filteredArrayUsingPredicate
but still don't work ,It's giving me an error msg
-- Update --
the error message is
swift:42:9: 'Array<search_options>' does not have a member named 'filteredArrayUsingPredicate'
-- Update --
class search_options {
let id:String
let option:String
init(){}
init(id:String ,option:String){
self.id = id
self.option = option
}
}
I only want to search in option variable
And when I tried to used
func searchBarSearchButtonClicked( searchBar: UISearchBar!)
{
let filteredArray = filter(search_options_array) { $0 == "test" }
println(searchBar.text)
}
I got this message
swift:40:58: 'search_options' is not a subtype of 'String'
Find index of specific object:
if let index = find(myArray, objectIAmLookingFor) {
// found! do something
}
Filter array:
let filteredArray = filter(myArray) { $0 == objectIAmLookingFor }
Finally after long search I did't ! ,
I was looking to find a way to do a dynamic search like if array of String contains
"hello","lo","yes"
and I want to get all the strings that contains for example "lo"
I want to get "hello" and "lo"
so the best way I found is regular expression search
so I do a For Loop throw all options in Array and compare every single object variable to the pattern ,and save it in new array on objects
for var i = 0; i < search_options_array.count; i++ {
let myRegex = "searched_text"
if let match = search_options_array[i].option.rangeOfString(myRegex, options: .RegularExpressionSearch){
filtered_options_array.append(search_options(id:search_options_array[i].id,option:search_options_array[i].option) )
}
}
The best part here you can use all benefits of regular expression and have a copy of yours old array so if you need it.
Thanks every one for helping.
Because filter accepts as a predicate a function which maps each element of the given Array to a Bool value (to determine which value should be filtered out), in your case it may be this way;
let a = [
search_options(id: "a", option: "X"),
search_options(id: "b", option: "Y"),
search_options(id: "c", option: "X")
]
let b = filter(a) { (e: search_options) in e.option == "X" }
// ==> [search_options(id: "a", option: "X"), search_options(id: "c", option: "X")]
The correct answer is
func searchBarSearchButtonClicked( searchBar: UISearchBar!)
{
let filteredArray = filter(search_options_array) { $0.option == "test" }
println(searchBar.text)
}
or
func searchBarSearchButtonClicked( searchBar: UISearchBar!)
{
let filteredArray = filter(search_options_array) { $0.id == "test" }
println(searchBar.text)
}
You must retrieve property of searched object by which you perform searching
Related
I have created an array struct to house my values used in a list. Now I want to be able to search this list and every time the user makes a blank space it should be viewed by the program as two different searchwords that should both be met.
I have successfully created a function to get the searchwords but I don't really get how to now filter my stuctArray by all the searchWords.
let searchWords = findAllSearchResutsRecursive(searchWord) //example ["A", "B", ,"C"]
let filteredArray = listArray.filter {
for word in searchWords {
$0.firstname!.capitalized.contains(word.capitalized) ||
$0.lastname!.capitalized.contains(word.capitalized) ||
$0.id!.capitalized.contains(word.capitalized) ||
$0.city!.capitalized.contains(word.capitalized)
}
}
To clarify, if the searchWords is ["A", "N"] and one of the participants (people in the list) has the firstname "Anna" but nothing else match the search I still want to show it.
Alternatively is if it would be better to convert the SearchWords to a set and in that way somehow filter them all at the same time.
This is errors I get:
You could rewrite it like this:
let searchWords = findAllSearchResutsRecursive(searchWord) //example ["A", "B", ,"C"]
let filteredArray = listArray.filter {
var matchFound = false
for word in searchWords {
if (
$0.firstname!.capitalized.contains(word.capitalized) ||
$0.lastname!.capitalized.contains(word.capitalized) ||
$0.id!.capitalized.contains(word.capitalized) ||
$0.city!.capitalized.contains(word.capitalized)
) {
matchFound = true
}
}
return matchFound
}
The outer filter function needs to return a boolean value. To not iterate over all remaining words if a match is found you could use:
let filteredArray = listArray.filter {
var result = false
for word in searchWords {
// check if any property contains the word
if $0.firstname!.capitalized.contains(word.capitalized) ||
$0.lastname!.capitalized.contains(word.capitalized) ||
$0.id!.capitalized.contains(word.capitalized) ||
$0.city!.capitalized.contains(word.capitalized){
// set the result and break the loop
result = true
break
}
}
// return the result
return result
}
I'm taking an array of ([(String?, String)]) and want to drop the last 3 or 5 elements of the non-optional String, depending on the String.
If I know I'm dropping 5 I use
let strippedName = (data.map{ ($0.1).dropLast(5) } ).map{ String($0) }
however, as I said this is a conditional thing depending on the string within the array.
When I try
let strippedName = (data.map{
if ($0.1 == "a") {
return $0.1.first!
} else {
return $0.1.last!
}
} ).map{ String($0) }
(which is obviously not quite the finished article for my needs).
The example will crash on some inputs, that is irrelevant. I need to have an if statement in the map above (the detail of the if statement is not relevant). An alternative is any way to have a conditional map function in Swift (as is the question title).
As above, my requirement is "I'm taking an array of ([(String?, String)]) and want to drop the last 3 or 5 elements of the non-optional String, depending on the String."
I get an error on the last String() cast - Ambiguous use of init.
How can I take my array and use an if statement within my first map?
Another way to solve it is to use reduce
let strippedName = data.reduce(into: []) { if $1.1 == "a" {$0.append($1.1)}}
Update
Trying to solve this as OP wants by using an if in the mapping. For my example here I use compactMap rather than map, this way anything that doesn't satisfy the if condition will be excluded from the array.
Since the return type of the closure is define to be String I don't need to do a second mapping
let strippedName = data.compactMap { t -> String? in
if t.1 == "a" {
return t.1
}
return nil
}
Swift could not infer the complex closure return type, hence it is showing you the error "Ambiguous use of init()"
Specifying the return type explicitly solves the issue.
let data: [(String?, String)] = [("a","bBCDEA"), ("b","a"), (nil,"CBCDEB"), ("d","aBCDEF"), (nil,"a"), ("f","FBCDEC")]
var strippedName = (data.map { tuple -> String.Element in
if (tuple.1 == "a") {
return tuple.1.first!
} else {
return tuple.1.last!
}
}).map{ String($0) }
print(strippedName) // ["A", "a", "B", "F", "a", "C"]
strippedName = (data.map{ ($0.1).dropLast(5) } ).map{ String($0) }
print(strippedName) // ["b", "", "C", "a", "", "F"]
I think the problem is with anonimous closure and type inference, however, to get what you're trying in your second chunk of code, you can use filter instead of conditionals. With this you won't get error:
let strippedName = data.filter { $0.1 == "a" }.map { $0.1 }
and you can add dropLast to the end.
I have an array of Super Hero objects. I want to group the superheroes based on the name property into separated arrays and then count how many objects are in each individual separated array
Object:
class SuperHero{
var name: String?
var power: Bool?
}
Array of superheroes (there can be an infinite num of superheroes)
var superHeroes = [SuperHero]()
let superHero1 = SuperHero()
superHero1.name = "SuperMan"
superHero1.power = true
superHeroes.append(superHero1)
let superHero2 = SuperHero()
superHero2.name = "BatMan"
superHero2.power = true
superHeroes.append(superHero2)
let superHero3 = SuperHero()
superHero3.name = "BatMan"
superHero3.power = true
superHeroes.append(superHero3)
let superHero4 = SuperHero()
superHero4.name = "SuperMan"
superHero4.power = true
superHeroes.append(superHero4)
//etc...
Use name property to sort:
let sortedHeros = superHeroes.sort{$0.name < $1.name}
for hero in sortedHeros{
print(hero.name)
/*
prints
BatMan
BatMan
SuperMan
SuperMan
*/
}
How do I put the sorted array into separate arrays then print the count of each separated array?
//this is what I want
separatedArraysOfSuperHeroes = [[superHero2, superHero3], [superHero1, superHero4]]
//subscriprting isn't ideal because i'll never know the exact number of separated arrays
print(separatedArraysOfSuperHeroes[0].count)
print(separatedArraysOfSuperHeroes[1].count)
As per the comments the reason why I want sub arrays is because I want to use them to populate different tableview sections. For i.e. inside my tableview I would now have a 2 sections. The first section would have a header that says "Batman" with 2 Batman objects inside of it and the second section would have a header that says Superman with 2 Superman objects inside of it. The count property would show the number of super hero objects inside each section.
func getSeparatedArrayBasedOnName(superHeroes: [SuperHero]) -> [[SuperHero]] {
guard let superNames = NSOrderedSet.init(array: superHeroes.map { $0.name ?? "" }).array as? [String] else {
print("Something went wrong with conversion")
return [[SuperHero]]()
}
var filteredArray = [[SuperHero]]()
for superName in superNames {
let innerArray = superHeroes.filter({ return $0.name == superName })
filteredArray.append(innerArray)
}
for array in filteredArray {
for hero in array {
print(hero.name ?? "")
}
}
return filteredArray
}
I have this class
class InboxInterests {
var title = ""
var eventID = 0
var count = ""
var added = 0
init(title : String, eventID : NSInteger, count: String, added : NSInteger) {
self.title = title
self.eventID = eventID
self.count = count
self.added = added
}
}
And i use it like this
var array: [InboxInterests] = [InboxInterests]()
Add item
let post = InboxInterests(title: "test",eventID : 1, count: "test", added: 0)
self.array.append(post)
I want to find the index by eventID key and change the value of added key in the same index
How is that possible?
For me, the above answer did not work. So, what I did was first find the index of the object that I want to replace then using the index replace it with the new value
if let row = self.upcoming.index(where: {$0.eventID == id}) {
array[row] = newValue
}
In Swift 5.0:
if let row = self.upcoming.firstIndex(where: {$0.eventID == id}) {
array[row] = newValue
}
Since you are using a class, use filter and first to find the value:
array.filter({$0.eventID == id}).first?.added = value
In this you:
filter the array down to elements that match the event ID
pick the first result, if any
then set the value
This works since classes are pass by reference. When you edit the return value from array.filter({$0.eventID == id}).first?, you edit the underlying value. You'll need to see the answers below if you are using a struct
EDIT: In Swift 3 you can save yourself a couple of characters
array.first({$0.eventID == id})?.added = value
EDIT: Swift 4.2:
array.first(where: { $0.eventID == id })?.added = value
array.filter {$0.eventID == id}.first?.added = value
The filter operator is not the best in this case, it works for some of you because classes are passed by reference.
Explanation: (You can copy the following code in a playground if you want to verify it).
class Book {
let id: Int
var title = "default"
init (id: Int) {
self.id = id
}
}
var arrayBook = [Book]()
arrayBook.append(Book(id: 0))
arrayBook.append(Book(id:1))
arrayBook.forEach { book in
print(book.title)
}
arrayBook.filter{ $0.id == 1 }.first?.title = "modified"
arrayBook.forEach { book in
print(book.title)
}
Arrays are copied by value not reference, so when you are using filter you are creating a new array (different than the initial), but when you modify the new one, the initial one gets modified too because both are pointing to the same class (classed are passed by reference), so after the filter your array will have changed and the new one gets deallocated. So in this case it will print "default", "default" and then "default, "modified".
What happens if you change class for struct, the value will be passed by value not reference so you will have 2 arrays in memory with different values, so if you go through arrayBooks again it will print before the filter "default","default", and then "default", "default" again. Because when you are using the filter you are creating and modifying a new array that will get deallocated if you do not store it).
The solution is using map, creating a new array with all the values but with the modified items or fields that we want and then replace our array with the new one. This will print "default", "default" before the map, and then "default", "modified"
This will work with structs, classes and everything that you want :).
struct Book {
let id: Int
var title = "default"
init (id: Int) {
self.id = id
}
}
var arrayBook = [Book]()
arrayBook.append(Book(id: 0))
arrayBook.append(Book(id:1))
arrayBook.forEach { book in
print(book.title)
}
arrayBook = arrayBook.map{
var mutableBook = $0
if $0.id == 1 {
mutableBook.title = "modified"
}
return mutableBook
}
arrayBook.forEach { book in
print(book.title)
}
array = array.map { $0.eventID == id ? newValue : $0 }
If you conform your class to Equatable then this would work:
extension Array where Element: Equatable {
#discardableResult
public mutating func replace(_ element: Element, with new: Element) -> Bool {
if let f = self.firstIndex(where: { $0 == element}) {
self[f] = new
return true
}
return false
}
}
Use like this:
array.replace(prev, with: new)
is there a possibility to get an object from an array with an specific property? Or do i need to loop trough all objects in my array and check if an property is the specific i was looking for?
edit: Thanks for given me into the correct direction, but i have a problem to convert this.
// edit again: A ok, and if there is only one specific result? Is this also a possible method do to that?
let imageUUID = sender.imageUUID
let questionImageObjects = self.formImages[currentSelectedQuestion.qIndex] as [Images]!
// this is working
//var imageObject:Images!
/*
for (index, image) in enumerate(questionImageObjects) {
if(image.imageUUID == imageUUID) {
imageObject = image
}
}
*/
// this is not working - NSArray is not a subtype of Images- so what if there is only 1 possible result?
var imageObject = questionImageObjects.filter( { return $0.imageUUID == imageUUID } )
// this is not working - NSArray is not a subtype of Images- so what if there is only 1 possible result?
You have no way to prove at compile-time that there is only one possible result on an array. What you're actually asking for is the first matching result. The easiest (though not the fastest) is to just take the first element of the result of filter:
let imageObject = questionImageObjects.filter{ $0.imageUUID == imageUUID }.first
imageObject will now be an optional of course, since it's possible that nothing matches.
If searching the whole array is time consuming, of course you can easily create a firstMatching function that will return the (optional) first element matching the closure, but for short arrays this is fine and simple.
As charles notes, in Swift 3 this is built in:
questionImageObjects.first(where: { $0.imageUUID == imageUUID })
Edit 2016-05-05: Swift 3 will include first(where:).
In Swift 2, you can use indexOf to find the index of the first array element that matches a predicate.
let index = questionImageObjects.indexOf({$0.imageUUID == imageUUID})
This is bit faster compared to filter since it will stop after the first match. (Alternatively, you could use a lazy sequence.)
However, it's a bit annoying that you can only get the index and not the object itself. I use the following extension for convenience:
extension CollectionType {
func find(#noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element? {
return try indexOf(predicate).map({self[$0]})
}
}
Then the following works:
questionImageObjects.find({$0.imageUUID == imageUUID})
Yes, you can use the filter method which takes a closure where you can set your logical expression.
Example:
struct User {
var firstName: String?
var lastName: String?
}
let users = [User(firstName: "John", lastName: "Doe"), User(firstName: "Bill", lastName: "Clinton"), User(firstName: "John", lastName: "Travolta")];
let johns = users.filter( { return $0.firstName == "John" } )
Note that filter returns an array containing all items satisfying the logical expression.
More info in the Library Reference
Here is a working example in Swift 5
class Point{
var x:Int
var y:Int
init(x:Int, y:Int){
self.x = x
self.y = y
}
}
var p1 = Point(x:1, y:2)
var p2 = Point(x:2, y:3)
var p3 = Point(x:1, y:4)
var points = [p1, p2, p3]
// Find the first object with given property
// In this case, firstMatchingPoint becomes p1
let firstMatchingPoint = points.first{$0.x == 1}
// Find all objects with given property
// In this case, allMatchingPoints becomes [p1, p3]
let allMatchingPoints = points.filter{$0.x == 1}
Reference:
Trailing Closure
Here is other way to fetch particular object by using object property to search an object in array.
if arrayTicketsListing.contains({ $0.status_id == "2" }) {
let ticketStatusObj: TicketsStatusList = arrayTicketsListing[arrayTicketsListing.indexOf({ $0.status_id == "2" })!]
print(ticketStatusObj.status_name)
}
Whereas, my arrayTicketsListing is [TicketsStatusList] contains objects of TicketsStatusList class.
// TicketsStatusList class
class TicketsStatusList {
internal var status_id: String
internal var status_name: String
init(){
status_id = ""
status_name = ""
}
}