0

The result that comes up when I search Google; find() doesn’t exist anymore.

So is there a replacement find() in Swift 2.0? Not that I’m sure if that would have worked.

I’m looking for some basic “get value from array” thing, not just its index number. Like to search for a specific string in the array, to see if it’s there or not.

  • I don't believe it's a duplicate, as the other question is only asking for "seeing if a value is in an array - a boolean return" and mine asks "how to see it and get it from the array - a value return". However if you hadn't read the body of the question, and only read the title, I can understand how you could see it as being so - I have since duly edited the title for clarification. – What'sASwift Mar 28 '16 at 15:21
  • I'm rereading your question after the previous edits, and I'm not sure what you're looking to do. If you already know the string, what value do you want from the array? An associated value? – Shades Mar 28 '16 at 15:36
  • Post some code that shows what you're trying to do (even if it's sloppy) and the value you want to get – Shades Mar 28 '16 at 15:45
  • Hi @Shades, I think this is more of a learning question, where What'sASwift gives the answer to your question at the end of their answer (below), in this case the ability to get the index and use/edit the value in the array. – Craig Francis Mar 28 '16 at 16:01
  • 2
    I'll reopen the question if you clarify the difference between `if fruitArray.contains(fruit) { print(fruit) }` (which is the method from the "duplicate") and `if let fruitIndex = fruitArray.indexOf(fruit) { print(fruitArray[fruitIndex]) }` (which is your proposed method). – Martin R Mar 28 '16 at 16:01
  • Hi @MartinR, I believe the OP was also using the index to access/edit the element in the array... but just to confirm, the "Answer your own question – share your knowledge, Q&A-style" tickbox was ticked, as the OP wasn't asking a question, but providing the answer in a way that they wanted to find it (noting that this is a beginner question, so having a short/technical answer is not appropriate). – Craig Francis Mar 28 '16 at 22:33

2 Answers2

5

The method you are looking for is contains. While indexOf will tell you if the value is in the array, the way to specifically do what you are asking is the contains method.

Example:

let array = ["Lions", "Tigers", "Bears"]
array.contains("Lions") // true
array.contains("Oh My") // false
Slayter
  • 1,172
  • 1
  • 13
  • 26
  • Actually, I wanted to: "see if a value was in an array" _and_ “get value from array”. I've tested it, and I don't think `.contains()` can do that, as it only returns a Bool value - great for part one of the question, and simplifies a lot, but it doesn't in any way return something I can use to ~directly get the value out of the array. Thanks for an alternative to part one of the question though! – What'sASwift Mar 28 '16 at 14:02
  • 2
    I suppose I misunderstood the question then. However if you are using `contains` properly, then you already have the value and getting it from the array becomes redundant. – Slayter Mar 28 '16 at 14:16
  • Hi @Slayter, I just wanted to confirm that you are correct, if you already have the value, it is redundant to get a second copy of it from the array... I just wanted to add, the OP is using the index to work with / edit the array, so while we might talk about the "value" (the string of characters), the OP (new to programming) meant it in a slightly different way. – Craig Francis Mar 28 '16 at 15:57
  • @CraigFrancis It's unfortunate you choose to attack the site and me personally instead of realizing the faults of your friend's question when by your own admission he was not clear from the start. Perhaps you should help him reword it. – Shades Mar 28 '16 at 19:13
  • @CraigFrancis not to be pedantic but in his question he states `I’m looking for some basic “get value from array” thing, not just its index number`. Which implies he wants the value from the array. – Slayter Mar 28 '16 at 19:28
  • Hi @Slayter, just to hopefully explain, the OP wasn't really looking for a short technical answer, they were just writing up their research in a way that might help other beginners who aren't familiar with programming terminology (kind of like a small tutorial/explanation/story). This is why the question was posted with their answer, using the "Answer your own question – share your knowledge, Q&A-style" tickbox (unfortunately Stack Overflow does not allow you to accept your own answer for 48 hours). – Craig Francis Mar 28 '16 at 22:40
1

Well, Me from yesterday, I’m glad you asked and weren’t stumped for several hours.

One way is to use .contains().

This can tell you if the value exists in the array by returning a boolean value (true or false).

Another way to see if a value is in an array, but also get the specific value out of an array like you want, is to use .indexOf().

This will return nil if the value searched for is not in the array, or will return the index number of the value in question. If you just want to check it’s there or not, then contains() is the way to go, but if you definitely want the Value, then it’s just one small step to get the value once you’ve got the index number.


So, let’s say we have an array like so:

var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]

We could test to see if the value “Apple” appears in the string. .contains() uses dot notation, where you put the value you’re looking for in parenthesis after it.

fruitArray.contains("Apple")

And .indexOf() works the same, like so:

fruitArray.indexOf("Apple")

So if we just want to do something simple like get a true/false response for "if the value is in the array", we can use .contains():

var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]

fruitArray.contains("Apple")    //returns true

Of course this doesn’t allow us to actually get the value itself though - if we try to print it for example, we’ll only get true or false.

var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]

print (fruitArray.contains("Apple"))

//prints true

This is where .indexOf() comes in handy instead. With indexOf() you return the index number of the array, rather than a boolean true/false. So you can do what you like with the array’s index number you receive. Like showing its value. We can do that in the same way we would in any other array - using array[index number]:

var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]

if let fruitIndex = fruitArray.indexOf("Banana") {
    print(fruitArray[fruitIndex])
}

//prints ‘Banana’

Or we can do something like this, where we can use .contains() to search two different arrays for the same value:

var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]

var berryArray: [String] = ["Blueberry", "Strawberry", "Banana"]

var sneakyFruit = "Banana"

if fruitArray.contains(sneakyFruit) && berryArray.contains(sneakyFruit) {
    print(sneakyFruit)
}
//prints ‘Banana’

You can use .contains() in a function, like this easy one for checking if the entered value is in the array:

func fruitExists (name: String) -> Bool {
    return fruitArray.contains(name)
}

fruitExists("Kiwi")        //‘true’
fruitExists("Rhubarb")     //‘false’

Or use a function with like this one using both - .contains() to check the value exists and .indexOf() to then edit it:

func shopListEdit (name: String) {
    if fruitArray.contains(name) {
       let fruitIndex = fruitArray.indexOf(name)
       fruitArray[fruitIndex!] = ("\(name) and Custard")
    }
}

shopListEdit("Banana")
fruitArray                   //["Apple", "Banana and Custard", "Kiwi"]
shopListEdit("Rhubarb")      
fruitArray                   //["Apple", "Banana and Custard", "Kiwi"]

There you go, Me from yesterday, persevere and remember that no question’s too stupid to deserve an answer.