George Blissett
George Blissett

Reputation: 43

Argument labels do not match any availble overloads

I'm trying to create an anagram tester, and I'm pretty sure the code I have should work, but I'm getting an error 'Argument labels '(_:)' do not match any available overloads' I've looked at the other posts regarding the same error, but I'm still not sure what this means or how to fix it.

var anagram1 : String!
var anagram2 : String!
var failure : Bool = false
var counter : Int = 0
print("Please enter first word: ")
anagram1 = readLine()
print("Please enter Second word: ")
anagram2 = readLine()

if anagram1.count == anagram2.count {
for i in anagram1.characters{
if (!failure){
    failure = true
    for y in anagram2.characters {
        counter += 1
        if i == y {

            failure = false
            anagram2.remove(at: String.Index(counter)) // error here

        }
    }
}
else {
    print("these words are not anagrams") 
    break;
    }
}
if (!failure) {
    print("these words ARE anagrams")

}
}
else{
    print ("these words aren't even the same length you fucking nonce")
}

Upvotes: 0

Views: 1945

Answers (2)

leanne
leanne

Reputation: 8719

To answer your first question: the error message Argument labels '(_:)' do not match any available overloads means that you've given a function parameter names or types that don't match anything Swift knows about.

The compiler is also trying to tell you what parameters to look at. '(_:)' says that you're calling a function with an unlabeled parameter. (That means a value without any parameter name. A common example of a function that would look like this is print("something"). In Swift documentation, this would look like print(_:).

Finally, overloads are ways to call a function with different information. Again using the print function as an example, you can call it multiple ways. A couple of the most common overloads would be:

// print something, followed by a newline character
print("something")

// print something, but stay on the same line
// (end with an empty string instead of the default newline character)
print("something", terminator: "") 

Documented, these might look like print(_:) and print(_:, terminator:).
Note: these are broken down for explanation. The actual Swift documentation shows func print(_: Any..., separator: String, terminator: String) which covers a number of different overloads!

Looking at the line where the error occurs, you see a function call and an initializer (which is essentially a function). Documented, the way you've entered the parameters, the functions would look like: remove(at:) and String.Index(_:).

String.Index(_:) matches the parameters of the error message, so that's where your error is. There is no overload of the String.Index initializer that takes an unnamed parameter.

To fix this error, you need to find the correct way to create a String.Index parameter for the remove(at:) function. One way might be to try something like this:

for y in anagram2.characters.enumerated() {
    // `y` now represents a `tuple`: (offset: Int, element: Character)
    // so, you don't need `counter` anymore; use `offset` instead
    if i == y.element { //`i` is a Character, so it can compare to `element`
        ...
        let yIndex: String.Index = anagram2.index(anagram2.startIndex, offsetBy: y.offset)
        anagram2.remove(at: yIndex)
        ...
    }
}

However, there are other issues with your code that will cause further errors.

For one, you're looping through a string (anagram2) and trying to change it at the same time - not a good thing to do.

Good luck to you in solving the anagram problem!

Upvotes: 1

George Blissett
George Blissett

Reputation: 43

Thanks for the help Leo but I found a way of doing it :)

if anagram1.count == anagram2.count {
for i in anagram1.characters{
if (!failure){
    counter = -1
    failure = true
    for y in anagram2.characters {
        counter += 1
        if i == y {

            failure = false
            if counter < anagram2.count {
            anagram2.remove(at: (anagram2.index(anagram2.startIndex, offsetBy: counter)))

                break;
            }



        }
    }
}

Upvotes: 0

Related Questions