I'm using arrays within an array. something like var arrs = [[String()]]
then for in loop through-
for i in 0..<arrs.count {
if arrs[i].contains(textField.text!) {
self.arr[i].append(field.text!)
} else {
self.arr.append([textField.text!])
}
}
well, it only works first two inserts [["hi", "hi"]] then when i add another string "hi" this happens.
[["hi", "hi"], ["hi"]]
What i'm trying is, if "hi" is in the array append them within the "hi" section. if there is a new word, use a new section. so i tried "there" then this happens [["hi", "hi"], ["hi"], ["there"], ["there"], ["there"]]
i really dont know why the if statement isn't working properly. any master's help will be appreciated! Thank you!
So i tried it with @Nirav's 2nd answer and it worked perfect with Strings! then i tried it with my own small class(just a variable string) i got an error argument label missing 'where' the class looks like this the changed var arrs = [[String()]] to var arrs:[[CheckIt]] = []
As per request! updated all codes and the error im getting
class CheckIt {
var test:String!
init(test:String) {
self.test = test
}
}
var arrs:[[CheckIt]] = []
let testIt = CheckIt(test: "hi")
let testIt2 = CheckIt(test: "there")
let testIt3 = CheckIt(test: "bye")
let testIt4 = CheckIt(test: "terry")
arrs.append([testIt])
arrs.append([testIt2])
arrs.append([testIt3])
arrs.append([testIt4])
if let index = arrs.index(where: {$0.test.contains(testIt)}) {
print(index)
}
and the EXACT error message im getting - Error: Value of type '[CheckIt]' has no member 'test'
[["hi", "hi","hi"], ["there","there", "there"]]? instead of your current result ?