Use contains instead:
let arr = ["Hello","Bye","Halo"]
let filtered = arr.filter { $0.contains("lo") }
print(filtered)
Output
["Hello", "Halo"]
Thanks to @user3441734 for pointing out that functionality is of course only available when you import Foundation
Videos
Use contains instead:
let arr = ["Hello","Bye","Halo"]
let filtered = arr.filter { $0.contains("lo") }
print(filtered)
Output
["Hello", "Halo"]
Thanks to @user3441734 for pointing out that functionality is of course only available when you import Foundation
In Swift 3.0
let terms = ["Hello","Bye","Halo"]
var filterdTerms = [String]()
func filterContentForSearchText(searchText: String) {
filterdTerms = terms.filter { term in
return term.lowercased().contains(searchText.lowercased())
}
}
filterContentForSearchText(searchText: "Lo")
print(filterdTerms)
Output
["Hello", "Halo"]
I have an array of Match objects [Match] with a string property of matchID. I also have an array of strings.
I would like to remove the Match object where matchID equals any string in the array.
I currently have:
var matches = [Match]
var matchIDsToDelete = [String]
for match in matches {
for id in matchIDsToDelete {
if match.matchID == id {
// remove that item from array
}
}
}My concern is removing the object from the array as I'm enumerating. I believe there will be consequences. Any suggestions? I would also love a functional solution as I'm currently reading more about functional programming.
Thanks!
[Updated for Swift 2.0]
As NSString is toll-free bridged to Swift String, just avoid the coercions with:
3> ["abc", "bcd", "xyz"].filter() { nil != $0.rangeOfString("bc") }
$R1: [String] = 2 values {
[0] = "abc"
[1] = "bcd"
}
But, if you think allValues aren't strings:
(keywords.allValues as? [String]).filter() { nil != $0.rangeOfString("bc") }
which returns an optional array.
Your filter is over [AnyObject], but your closure takes NSString. These need to match. Also, your result needs to be a Bool, not a Bool?. You can address these simply like this:
self.filteredKeywords = filter(keywords.allValues, {
let keyword = $0 as? NSString
return keyword?.containsString(searchText) ?? false
})
This accepts AnyObject and then tries to coerce it down to NSString. It then nil-coalleces (??) the result to make sure it always is a Bool.
I'd recommend, though, treating keywords as a [String:String] rather than an NSDictionary. That would get rid of all the complications of AnyObject. Then you can just do this:
self.filteredKeywords = keywords.values.filter { $0.rangeOfString(searchText) != nil }
Whenever possible, convert Foundation collections into Swift collections as soon as you can and store those. If you have incoming Foundation objects, you can generally convert them easily with techniques like:
let dict = nsdict as? [String:String] ?? [:]
Or you can do the following to convert them such that they'll crash in debug (but silently "work" in release):
func failWith<T>(msg: String, value: T) -> T {
assertionFailure(msg)
return value
}
let dict = nsdict as? [String:String] ?? failWith("Couldn't convert \(d)", [:])
Use this code:
let yearArray: [String] = ... // your array of type [String]
let filteredArray = yearArray.filter {
!$0.isEmpty
}
Look at the picture for output:

You can do it by using filter (on an Array type) :
let filteredArray = yearArray.filter{$0 != ""}
It's that simple.
If your array is an NSMutableArray, just cast it to [String] :
if let yearArray = yearArray as? [String] {
let filteredArray = yearArray.filter{$0 != ""}
// add your code here
}
One approach is to update your filter to see if any value in pets is in the petArr array:
users = users.filter { $0.pets.contains(where: { petArr.contains($0) }) }
The first $0 is from the filter and it represents each User.
The second $0 is from the first contains and it represents each pet within the pets array of the current User.
If elements inside the internal array are Equatable you can just write:
array1 = array1.filter{ $0.arrayInsideOfArray1 == array2 }
If they are not, you can make them, by adopting Equatable protocol and implementing:
func ==(lhs: YourType, rhs: YourType) -> Bool
How about
let filteredMenuItems = allMenuItems.filter { anArrayOfIds.contains($0.id)}
That gives me the items with 1 and 3.
I've taken the liberty of updating your code a little:
struct MenuItems {
let id: String
let name: String
}
let m1 = MenuItems(id: "1", name: "Name 1")
let m2 = MenuItems(id: "2", name: "Name 2")
let m3 = MenuItems(id: "3", name: "Name 3")
let allMenuItems = [m1, m2, m3]
let anArrayOfIds: [String] = ["1", "3"]
let filteredMenuItems = allMenuItems.filter { anArrayOfIds.contains($0.id)}
Hope that helps
You could do it like so:
let filteredMenuItems = allMenuItems.filter { anArrayOfIds.contains($0.id) }
I would suggest you :
- remove the pointless initializer,
- make
idan integer, - if the properties won't be changed after initialization then use
letinstead ofvar, - make anArrayOfIds a set instead of an array, since the
containsmethod would be O(1) instead of O(n).
Implementing all of these recommendations gives you the following code:
struct MenuItems {
let id: Int
let name: String
}
let m1 = MenuItems(id: 1, name: "Name 1")
let m2 = MenuItems(id: 2, name: "Name 2")
let m3 = MenuItems(id: 3, name: "Name 3")
let allMenuItems: [MenuItems] = [m1, m2, m3]
var aSetOfIds: Set<Int> = [1, 3] //I used a var if aSetOfIds could be changed
var filteredMenuItems = allMenuItems.filter { aSetOfIds.contains($0.id) }