Find match in array for given string? - ios

Ok so lets say I have an custom object for vocabulary words, alternate way of being written, and their meaning.
class VocabEntry {
var kanji:String?
var kana:String?
var meaning:String?
}
Then I have an array comprised of these objects. Here's one for example.
let newVocabWord = VocabEntry()
newVocabWord.kanji = "下さい”
newVocabWord.kana = "ください”
newVocabWord.meaning = "please"
Now I have a string of text:
let testString = "すみません、十階のボタンを押して下さい"
How can I compare that string to my array of custom objects (that contain strings) and reference the matches?
I tried.
if vocabArray.contains( { $0.kanji == testString }) {
print("yes")
}
But that trying to match the entire string. If I change testString to "下さい" it works, but that's not what I'm looking for. What I want is for it to say "Here I found 下さい in xx object. Here's the index number."

You can use indexOf() with a predicate to find the index of a
matching entry, and containsString() to search for substrings.
Since the kanji property is optional, you have to check that via
optional binding:
if let index = vocabArray.indexOf({ entry in
if let kanji = entry.kanji {
// check if `testString` contains `kanji`:
return testString.containsString(kanji)
} else {
// `entry.kanji` is `nil`: no match
return false
}
}) {
print("Found at index:", index)
} else {
print("Not found")
}
This can be written more concise as
if let index = vocabArray.indexOf({
$0.kanji.flatMap { testString.containsString($0) } ?? false
}) {
print("Found at index:", index)
} else {
print("Not found")
}
To get the indices of all matching entries, the following would work:
let matchingIndices = vocabArray.enumerate().filter { (idx, entry) in
// filter matching entries
entry.kanji.flatMap { testString.containsString($0) } ?? false
}.map {
// reduce to index
(idx, entry) in idx
}
print("Found at indices:", matchingIndices)

Related

find duplicate names on list and report to user [duplicate]

Declaration:
let listArray = ["kashif"]
let word = "kashif"
then this
contains(listArray, word)
Returns true but if declaration is:
let word = "Kashif"
then it returns false because comparison is case sensitive.
How to make this comparison case insensitive?
Xcode 8 • Swift 3 or later
let list = ["kashif"]
let word = "Kashif"
if list.contains(where: {$0.caseInsensitiveCompare(word) == .orderedSame}) {
print(true) // true
}
alternatively:
if list.contains(where: {$0.compare(word, options: .caseInsensitive) == .orderedSame}) {
print(true) // true
}
if you would like to know the position(s) of the element in the array (it might find more than one element that matches the predicate):
let indices = list.indices.filter { list[$0].caseInsensitiveCompare(word) == .orderedSame }
print(indices) // [0]
You can also use localizedStandardContains method which is case and diacritic insensitive and would match a substring as well:
func localizedStandardContains<T>(_ string: T) -> Bool where T : StringProtocol
Discussion This is the most appropriate method for doing user-level string searches, similar to how searches are done generally in the system. The search is locale-aware, case and diacritic insensitive. The exact list of search options applied may change over time.
let list = ["kashif"]
let word = "Káshif"
if list.contains(where: {$0.localizedStandardContains(word) }) {
print(true) // true
}
you can use
word.lowercaseString
to convert the string to all lowercase characters
For checking if a string exists in a array (case insensitively), please use
listArray.localizedCaseInsensitiveContainsString(word)
where listArray is the name of array
and word is your searched text
This code works in Swift 2.2
Swift 4
Just make everything (queries and results) case insensitive.
for item in listArray {
if item.lowercased().contains(word.lowercased()) {
searchResults.append(item)
}
}
You can add an extension:
Swift 5
extension Array where Element == String {
func containsIgnoringCase(_ element: Element) -> Bool {
contains { $0.caseInsensitiveCompare(element) == .orderedSame }
}
}
and use it like this:
["tEst"].containsIgnoringCase("TeSt") // true
Try this:
let loword = word.lowercaseString
let found = contains(listArray) { $0.lowercaseString == loword }
For checking if a string exists in a array with more Options(caseInsensitive, anchored/search is limited to start)
using Foundation range(of:options:)
let list = ["kashif"]
let word = "Kashif"
if list.contains(where: {$0.range(of: word, options: [.caseInsensitive, .anchored]) != nil}) {
print(true) // true
}
if let index = list.index(where: {$0.range(of: word, options: [.caseInsensitive, .anchored]) != nil}) {
print("Found at index \(index)") // true
}
swift 5, swift 4.2 , use the code in the below.
let list = ["kAshif"]
let word = "Kashif"
if list.contains(where: { $0.caseInsensitiveCompare(word) == .orderedSame }) {
print("contains is true")
}
SWIFT 3.0:
Finding a case insensitive string in a string array is cool and all, but if you don't have an index it can not be cool for certain situations.
Here is my solution:
let stringArray = ["FOO", "bar"]()
if let index = stringArray.index(where: {$0.caseInsensitiveCompare("foo") == .orderedSame}) {
print("STRING \(stringArray[index]) FOUND AT INDEX \(index)")
//prints "STRING FOO FOUND AT INDEX 0"
}
This is better than the other answers b/c you have index of the object in the array, so you can grab the object and do whatever you please :)
Expanding on #Govind Kumawat's answer
The simple comparison for a searchString in a word is:
word.range(of: searchString, options: .caseInsensitive) != nil
As functions:
func containsCaseInsensitive(searchString: String, in string: String) -> Bool {
return string.range(of: searchString, options: .caseInsensitive) != nil
}
func containsCaseInsensitive(searchString: String, in array: [String]) -> Bool {
return array.contains {$0.range(of: searchString, options: .caseInsensitive) != nil}
}
func caseInsensitiveMatches(searchString: String, in array: [String]) -> [String] {
return array.compactMap { string in
return string.range(of: searchString, options: .caseInsensitive) != nil
? string
: nil
}
}
My example
func updateSearchResultsForSearchController(searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else { return }
let countries = Countries.getAllCountries()
filteredCountries = countries.filter() {
return $0.name.containsString(searchText) || $0.name.lowercaseString.containsString(searchText)
}
self.tableView.reloadData()
}
If anyone is looking to search values from within model class, say
struct Country {
var name: String
}
One case do case insensitive checks like below -
let filteredList = countries.filter({ $0.name.range(of: "searchText", options: .caseInsensitive) != nil })

searching keys in dictionary swift

I want to search for the key id in the dictionary. I have a dictionary like this:
var tableData:[String:Any] = ["id":["path":"","type":"","parameters":[]]]
Table data has 307 items, and all id's unique. I want to search in dictionary key id, like when I write "get" , it need the show all the search results with "get" in table view.
func updateSearchResults(for searchController: UISearchController) {
let searchString = searchController.searchBar.text
if let entry = tableData.keys.first(where: { $0.lowercased().contains(searchString) }) {
print(entry)
} else {
print("no match")
}
tableView.reloadData()
}
func didChangeSearchText(searchText: String) {
if let entry = tableData.keys.first(where: { $0.lowercased().contains(searchText) }) {
print(entry)
} else {
print("no match")
}
// Reload the tableview.
tableView.reloadData()
}
When I try to search a word it prints "no match", in the debug, unable to read data writes for the value of the entry. Thank you in advance!
In fact your keys have to be unique and as in your case id is a top level key you don't have to perform filtering in order to access its value. Simply use tableData[searchText] to grab its value.
If you don't know the id value and you want to loop through all the keys you can do so like
for key in tableData.keys {
print(key)
let value = tableData[key]
// or do whatever else you want with your key value
}
As per what you already have in place you need to do the following
var tableData:[String:Any] = ["hello world":["path":"fgh","type":"dfghgfh","parameters":[]], "something else":["path":"sdfsdfsdf","type":"dfghfghfg","parameters":[]]]
if let entry = tableData.keys.first(where: { $0.lowercased().contains("hello") }) {
print(entry)
// prints 'hello world'
} else {
print("no match")
}
Or you can simply get a new filtered array from your data source like
let result = tableData.filter { (key, value) in
key.lowercased().contains("else") // your search text replaces 'else'
}
/*
* result would be an array with the objects based on your id search
* so you'll not only have the keys but the entire object as well
*/
print("Found \(result.count) matches")
To access an element in a Dictionary with a key, use the following code.
if let entry = tableData[searchText] {
print(entry)
}
For further information, have a look at:
how can i get key's value from dictionary in Swift?
Try using first(where:) directly on tableData, like this:
func updateSearchResults(for searchController: UISearchController) {
guard let searchString = searchController.searchBar.text else { return }
if let (id, entry) = tableData.first(where: { (key, value) -> Bool in key.lowercased().contains(searchString) }) {
print(entry)
} else {
print("no match")
}
tableView.reloadData()
}
func didChangeSearchText(searchText: String) {
if let (id, entry) = tableData.first(where: { (key, value) -> Bool in key.lowercased().contains(searchText) }) {
print(entry)
} else {
print("no match")
}
// Reload the tableview.
tableView.reloadData()
}

how can I remove a struct element from an array of structs based on the id of the element in Swift?

I have a structure in my Swift app:
open class Cluster : NSObject {
open var username: String? = ""
open var id: String? = ""
open var deleted: Bool? = false
}
and now I'm iterating over this array and I'm adding new elements to it, but only in case those elements are not there yet:
if(!self.array.contains(where: {$0.id==temp.id}))
{
self.array.append(temp);
}
I want to tweak this code so that it not only adds new elements if they're not there, but also removes the ones that - in the meantime - had their flag deleted changed to true.
I started writing this code:
if(!self.array.contains(where: {$0.id==temp.id}))
{
self.array.append(temp);
} else {
if(temp.deleted == true){
self.array.remove //how can I remove here this specific element?
}
}
To remove a particular element from an array, you are supposed to get index of that element first and then delete as shown below:
if let index:Int = self.array.index(where: {$0.id == temp.id && $0.deleted == true}) {
self.array.remove(at: index)
}
First, I suggest you fix your class:
An optional Bool makes no sense - the object is either deleted or not
An optional id doesn't make much sense either; All objects need an id
If you implement the hash and equality parts of NSObject then you get access to array's index(of:) method and you can use sets.
Cluster.swift
open class Cluster : NSObject {
open var username: String? = ""
open let id: String
open var isDeleted: Bool = false
init(id: String) {
self.id = id
}
open override var hashValue: Int {
get {
return self.id.hashValue
}
}
open override func isEqual(_ object: Any?) -> Bool {
guard let rhs = object as? Cluster else {
return false
}
let lhs = self
return lhs.id == rhs.id
}
}
Now, given an array of Cluster objects, you can remove the deleted ones using:
let cleanArray = dirtyArrayOfCluster.filter {
!$0.isDeleted
}
And you can remove duplicates by passing the array through a set:
let deDupedArray = Array(Set(cleanArray))
if temp.deleted == true, let index = array.index(where: { $0.id == temp.id }) {
array.remove(at: index)
}
What about this?
if array.contains(where: { $0.id == temp.id } ) {
array.append(temp)
}
array = array.filter { $0.deleted == true }
The first part add temp only if it is not into the array.
The last line removes all the elements marked as deleted.

If filter returns nil, how do I not return empty result?

I'm doing the following filtering that returns a recipe list, filtered by given category name value.
filteredRecipe = filteredRecipe.filter({
if let category = $0.valueForKey("category") as? NSManagedObject {
if let name = category.valueForKey("name") as? String {
return name.rangeOfString(cap) != nil
} else {
return false
}
} else {
return false
}
})
This filter works in association with searchBar textfield so I will possibly have random value in the textfield which will make filteredRecipe to hold unreliable data.
I need to make sure when the filter can't find any recipe from filteredRecipe with given category name value, I leave filteredRecipe untouched.
Currently, when there is no match, it makes filteredRecipe empty array []. I'm not sure what part causes this.
You need to assign the filtering result to a temporary variable and check that it isn't empty.
let filtered = filteredRecipe.filter({
if let category = $0.valueForKey("category") as? NSManagedObject {
if let name = category.valueForKey("name") as? String {
return name.rangeOfString(cap) != nil
}
return false
})
if !filtered.isEmpty {
filteredRecipe = filtered
}
Another approach is to extend Collection with a selfOrNilIfEmpty property, and use the nil coalescing operator:
extension Collection {
var selfOrNilIfEmpty: Self? {
return isEmpty ? nil : self
}
}
and later, on your code:
let filteredRecipe = filteredRecipe.filter { ... }.selfOrNilIfEmpty ?? filteredRecipe

Find an object in array?

Does Swift have something like _.findWhere in Underscore.js?
I have an array of structs of type T and would like to check if array contains a struct object whose name property is equal to Foo.
Tried to use find() and filter() but they only work with primitive types, e.g. String or Int. Throws an error about not conforming to Equitable protocol or something like that.
SWIFT 5
Check if the element exists
if array.contains(where: {$0.name == "foo"}) {
// it exists, do something
} else {
//item could not be found
}
Get the element
if let foo = array.first(where: {$0.name == "foo"}) {
// do something with foo
} else {
// item could not be found
}
Get the element and its offset
if let foo = array.enumerated().first(where: {$0.element.name == "foo"}) {
// do something with foo.offset and foo.element
} else {
// item could not be found
}
Get the offset
if let fooOffset = array.firstIndex(where: {$0.name == "foo"}) {
// do something with fooOffset
} else {
// item could not be found
}
You can use the index method available on Array with a predicate (see Apple's documentation here).
func index(where predicate: (Element) throws -> Bool) rethrows -> Int?
For your specific example this would be:
Swift 5.0
if let i = array.firstIndex(where: { $0.name == "Foo" }) {
return array[i]
}
Swift 3.0
if let i = array.index(where: { $0.name == Foo }) {
return array[i]
}
Swift 2.0
if let i = array.indexOf({ $0.name == Foo }) {
return array[i]
}
FWIW, if you don't want to use custom function or extension, you can:
let array = [ .... ]
if let found = find(array.map({ $0.name }), "Foo") {
let obj = array[found]
}
This generates name array first, then find from it.
If you have huge array, you might want to do:
if let found = find(lazy(array).map({ $0.name }), "Foo") {
let obj = array[found]
}
or maybe:
if let found = find(lazy(array).map({ $0.name == "Foo" }), true) {
let obj = array[found]
}
Swift 3
If you need the object use:
array.first{$0.name == "Foo"}
(If you have more than one object named "Foo" then first will return the first object from an unspecified ordering)
You can filter the array and then just pick the first element, as shown
in Find Object with Property in Array.
Or you define a custom extension
extension Array {
// Returns the first element satisfying the predicate, or `nil`
// if there is no matching element.
func findFirstMatching<L : BooleanType>(predicate: T -> L) -> T? {
for item in self {
if predicate(item) {
return item // found
}
}
return nil // not found
}
}
Usage example:
struct T {
var name : String
}
let array = [T(name: "bar"), T(name: "baz"), T(name: "foo")]
if let item = array.findFirstMatching( { $0.name == "foo" } ) {
// item is the first matching array element
} else {
// not found
}
In Swift 3 you can use the existing first(where:) method
(as mentioned in a comment):
if let item = array.first(where: { $0.name == "foo" }) {
// item is the first matching array element
} else {
// not found
}
Swift 3.0
if let index = array.index(where: { $0.name == "Foo" }) {
return array[index]
}
Swift 2.1
Filtering in object properties is now supported in swift 2.1. You can filter your array based on any value of the struct or class here is an example
for myObj in myObjList where myObj.name == "foo" {
//object with name is foo
}
OR
for myObj in myObjList where myObj.Id > 10 {
//objects with Id is greater than 10
}
Swift 4,
Another way to achieve this
using filter function,
if let object = elements.filter({ $0.title == "title" }).first {
print("found")
} else {
print("not found")
}
Swift 3
you can use index(where:) in Swift 3
func index(where predicate: #noescape Element throws -> Bool) rethrows -> Int?
example
if let i = theArray.index(where: {$0.name == "Foo"}) {
return theArray[i]
}
Swift 2 or later
You can combine indexOf and map to write a "find element" function in a single line.
let array = [T(name: "foo"), T(name: "Foo"), T(name: "FOO")]
let foundValue = array.indexOf { $0.name == "Foo" }.map { array[$0] }
print(foundValue) // Prints "T(name: "Foo")"
Using filter + first looks cleaner, but filter evaluates all the elements in the array. indexOf + map looks complicated, but the evaluation stops when the first match in the array is found. Both the approaches have pros and cons.
Another way to get access to array.index(of: Any) is by declaring your object
import Foundation
class Model: NSObject { }
Swift 3
if yourArray.contains(item) {
//item found, do what you want
}
else{
//item not found
yourArray.append(item)
}
Use contains:
var yourItem:YourType!
if contains(yourArray, item){
yourItem = item
}
Or you could try what Martin pointed you at, in the comments and give filter another try: Find Object with Property in Array.
Swift 3:
You can use Swifts built in functionality to find custom objects in an Array.
First you must make sure your custom object conforms to the: Equatable protocol.
class Person : Equatable { //<--- Add Equatable protocol
let name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
//Add Equatable functionality:
static func == (lhs: Person, rhs: Person) -> Bool {
return (lhs.name == rhs.name)
}
}
With Equatable functionality added to your object , Swift will now show you additional properties you can use on an array:
//create new array and populate with objects:
let p1 = Person(name: "Paul", age: 20)
let p2 = Person(name: "Mike", age: 22)
let p3 = Person(name: "Jane", age: 33)
var people = [Person]([p1,p2,p3])
//find index by object:
let index = people.index(of: p2)! //finds Index of Mike
//remove item by index:
people.remove(at: index) //removes Mike from array
For Swift 3,
let index = array.index(where: {$0.name == "foo"})
Use Dollar which is Lo-Dash or Underscore.js for Swift:
import Dollar
let found = $.find(array) { $0.name == "Foo" }
For example, if we had an array of numbers:
let numbers = [2, 4, 6, 8, 9, 10]
We could find the first odd number like this:
let firstOdd = numbers.index { $0 % 2 == 1 }
That will send back 4 as an optional integer, because the first odd number (9) is at index four.

Resources