0

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'

3
  • You want to add same word in same array ? means result should be [["hi", "hi","hi"], ["there","there", "there"]] ? instead of your current result ? Commented Jun 2, 2017 at 6:12
  • thats right! but somehow my code is not working as [["hi", "hi","hi"], ["there","there", "there"]]. its weird or am i missing something? Commented Jun 2, 2017 at 6:18
  • Have you tried my below solution, is it working now ? Commented Jun 2, 2017 at 6:54

1 Answer 1

3

The problem is that you need to break the loop in if and need to add new array only if there is no array match condition otherwise it will go further.

var found = false
for i in 0..<arrs.count {

    if arrs[i].contains(textField.text!) {
        arrs[i].append(textField.text!)
        found = true
        break
    } 
}
if !found {
    arrs.append([textField.text!])
}

But this is not Swifty way what you need is to use index(where:) and don't need to use for loop.

if let index = arrs.index(where: { $0.contains(textField.text!) }) {
    arrs[index].append(textField.text!)
}
else {
    arrs.append([textField.text!])
}
Sign up to request clarification or add additional context in comments.

14 Comments

my god! that worked! Thanks Nirav! (D for deadpool?), so i assume that index(where) is one good swifty way to look in the arrays! Thanks new learning! can you pls explain me when to use $0? i know that $0 will be used as a property within the array. but that is all! Thank again!
like $0 when to use it?
@Terry Welcome mate, $0 represent the each elements of array one by one in the closure because index(where:) iterate though array and if fine condition true it will no go further and stop it
@Terry Also there is problem in previous for loop answer I have corrected it check that too.
Ok, I faced some little issue with your second answer. Basically the index(where:) get an error if i try other than string type. for e.g. small class i've made. (the class is also to store some strings) self.message.index(where: {$0.contains(userID)}) and seems its not detecting the method. any idea? error says argument label 'where' is missing @Nirav
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.