It's pretty simple. First make sure to add this before your class ViewController: UIViewController{}:
import Foundation
And then, you can find the number of common words like so:
var array = ["word1", "word2", "word3 and word 4"]
var count=0
var s=textField.text!
for item in array{
if s.components(separatedBy: " ").contains(item){
count+=1
}
}
if count > 0{ // do something here
print("There are \(count) in the text field from the array")
}
And if the array contains elements with spaces, do something like this:
var array = ["word1", "word2", "word3"]
var count=0
var s=textField.text!
for item in array{
if s.contains(item){
count+=1
}
}
if count > 0{ // do something here
print("There are \(count) in the text field from the array")
}
Explanation:
If a word is found, the variable count increases by 1. After that, you check if the count is higher than 0, and if so, you do what you want there.
The final code will look like this:
import Foundation
import UIKit
class myClass: UIViewController{
@IBOutlet weak var textField: UITextField!
var array = ["word1", "word2", "word3"]
var count=0
@IBAction func buttonPressed(_ sender: UIButton){
var s=textField.text!
for item in array{
if s.components(separatedBy: " ").contains(item){
count+=1
}
}
if count > 0{
print("There are \(count) in the text field from the array")
}
}
}
Hope it helps!