-5

I'm trying to figure out how to check if a string passed to a function can be found in an array that is comprised of tuples as elements. Below is my code. I'm not sure how best to fix this.

typealias myTuple = (input: String, magnitude: Int)    

var userInput = "Happy"                               

var wordOneArray: [myTuple] = [] 

var magnitudeCount = 1

var userInputTuple: myTuple = (userInput, magnitudeCount)


magnitudeCount += magnitudeCound

func addUserInput(tempArray: myTuple) {    

    if wordOneArray.contains(userInput) {

        userInputTuple = (userInput, magnitudeCount += magnitudeCount)

    }

    wordOneArray.append(userInputTuple)

    print(wordOneArray)


}

addUserInput(tempArray: userInputTuple)

userInput = "Sad"

userInputTuple = (userInput, magnitudeCount)

addUserInput(tempArray: userInputTuple)
  • What's your expected output? What errors do you get, and on what lines? – Hamish Mar 16 '17 at 22:17
  • I get an error on the if statement line "missing argument label 'where:' in call. I also get an error in the next line that says '+=' produces '()', not the expected contextual result type 'Int'. I'm not sure why on the second. The first error fix doesn't make sense to me. – Andrew Wertheim Mar 16 '17 at 22:28
  • Please [edit] your question with this information. The reason for the first error is that you cannot directly ask if an array of tuples contains a string – you'll have to use a custom predicate (hence why the compiler is suggesting `contains(where:)`). The reason for the second error is that `+=` returns `Void`. You need to move it out of the tuple assignment, and just assign `magnitudeCount` instead – see for example http://stackoverflow.com/q/36185088/2976878 – Hamish Mar 16 '17 at 22:31

1 Answers1

1

Firstly, your code doesn't compile :/

In any case, you can use wordOneArray.contains(where: { $0.0 == userInput }) to find out if the tuple contains the string.

Kane Cheshire
  • 1,654
  • 17
  • 20
  • thank you. will this search for that particular string in all tuples of the array? or just the first? – Andrew Wertheim Mar 17 '17 at 02:18
  • It will loop through the entire array until it finds a tuple with a matching string and returns `true` if it does, if it doesn't it will return `false` instead. – Kane Cheshire Mar 17 '17 at 08:38
  • ok awesome. that worked well. The only last piece is that I am trying to then increment the "magnitudeCount" of the given tuple element it finds. I've modified my code but it doesn't give the right output.. (also, Stackoverflow won't allow me to paste my code here, it says it's too long for a comment). Do you have any ideas of how I could increment the magnitudeCount if a match is found? – Andrew Wertheim Mar 17 '17 at 15:23
  • Sure, just increment on a line before passing it into the tuple: ```magnitudeCount += magnitudeCount``` ```userInputTuple = (userInput, magnitudeCount)``` – Kane Cheshire Mar 17 '17 at 19:00
  • ah ok I see what you're saying, however the function first has to check if the userInput is in the array and if it is, then magnitudeCount *of that specific String* is incremented. So I might have some issues since magnitudeCount isn't tied to a specific string yet. – Andrew Wertheim Mar 17 '17 at 19:27
  • Well, no, because you can't get to this line `userInputTuple = (userInput, magnitudeCount)` without ` if wordOneArray.contains(where: { $0.0 == userInput }) {}` being true. So long as you add `magnitudeCount += magnitudeCount` inside that if statement, but before passing it into the tuple (i.e. on the line before, like I said) then it will only update the magnitude when that string is found. – Kane Cheshire Mar 17 '17 at 21:08