Swift Array(string) not working

1.8k views Asked by At

I'm trying to make a hangman game. I'm going to paste my code in because I feel my problem is somewhere in here that I'm just not seeing. Look down and find the part where I wrote THIS IS WHERE THE ERROR OCCURS. Here is the error message I get: Missing argument label 'arrayLiteral' in call. When I add the arrayLiteral the code does not work. I'm very new to Swift so some help would be very appreciated! Thank you! (sorry that there's a lot of code that's probably unrelated to the issue)

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var secret_word_label: UILabel!
    @IBOutlet weak var guess_field: UITextField!
    @IBOutlet weak var submit_button: UIButton!

    //only for bug testing
    @IBOutlet weak var arb_print: UILabel!
    @IBOutlet weak var print_secret: UILabel!

    var user_guesses = [String]()

    var secret_word = "asdf"

    var wrong_guesses = 0

    var guess_letter = ""

    var correct_guesses = 0

    let limit_wrongs = 3

    var secret_word_loc = [String]()


    func return_len(text: String) -> Int {

        var final = [String]()

        var loc = Array(text)

        for letter in loc {

            if !contains(final, String(letter)) {

                final.append(String(letter))

                }

        }

        return final.count

    }


    func gen_secret_word() -> String {

        var random_num = arc4random_uniform(4)

        var words = ["hello","world","iphone","apple"]

        return words[Int(random_num)]

    }


    func start_game(action: UIAlertAction! = nil) {

        secret_word = gen_secret_word()

        print_secret.text = secret_word

        secret_word_label.text = dashes_except(secret_word,except: "1")


       THIS IS WHERE THE ERROR OCCURS <


        secret_word_loc = Array(secret_word)


        >
        user_guesses = [String]()

        correct_guesses = 0

        wrong_guesses = 0



    }


    func dashes_except(word: String,except: String) -> String {

        var loc = [String]()

        for (index, character) in enumerate(word) {

            loc.append(String(character))

        }

        var loc_except = [String]()

        for (index, character) in enumerate(except) {

            loc_except.append(String(character))

        }

        for x in 0..<loc.count {

            if !contains(loc_except, loc[x]) {

                loc[x] = "-"

            }

        }

        return "".join(loc)

    }


    func textFieldShouldReturn(textField: UITextField!) -> Bool {

        textField.resignFirstResponder()

        return true

    }


    @IBAction func submit_engaged(sender: UIButton) {

        guess_letter = guess_field.text

        if contains(user_guesses, guess_letter) {

            var nuttin = "well nuttin"

        }

        else if contains(secret_word_loc, guess_letter) {

            correct_guesses += 1

            user_guesses.append(guess_letter)

        } else {

            wrong_guesses += 1

            user_guesses.append(guess_letter)

        }



        arb_print.text = "".join(user_guesses)

        secret_word_label.text = dashes_except(secret_word,except: "".join(user_guesses))

        if correct_guesses == return_len(secret_word) {

            //give alert that they won and then ask for continue to play again and put how many guesses it took them


            let ac = UIAlertController(title: "You won", message: "Congrats. It took you \(wrong_guesses + correct_guesses) tries", preferredStyle: .Alert)

            ac.addAction(UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: start_game))

            presentViewController(ac, animated: true, completion: nil)


        }

        else if wrong_guesses == limit_wrongs {

            //give alert that they lost and then ask for continue to play again and give the word

            let ac = UIAlertController(title: "You lost", message: "The word was \(secret_word)", preferredStyle: .Alert)

            ac.addAction(UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: start_game))

            presentViewController(ac, animated: true, completion: nil)

        }









    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        guess_field.delegate = self

        start_game()

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
3

There are 3 answers

2
Eric Qian On BEST ANSWER

Because you declared secret_word_loc as a type of Array<String>, and Array<secret_word> is actually a type of Array<Character>. You could either declare var secret_word_loc = [Character]() or do secret_word_loc = map("secret_word") {String($0)} which will convert each character in secret_word to String and put them in an Array.

0
Jenner Felton On

Try secret_word_loc = [secret_word].

0
nhgrif On

There are (at least) two possible solutions here, as Array has several initializers.

First, we can use the literal array syntax, which is simply a pair of square brackets wrapped around our comma-separated array elements:

secret_word_loc = [secret_word]

Or, we can use the more verbose initializer, which actually explains the source of the error message:

secret_word_loc = Array(arrayLiteral: secret_word)

This initializer for Array accepts a comma-separated list of array elements to instantiate your array with.

Both solutions work perfectly fine.

The second explains the error message:

Missing argument label 'arrayLiteral'

which is simply telling you "Hey, everything is right, but you must use the argument label, which in this case is arrayLiteral.

The first solution, however, is more Swift-esque and I'd absolutely prefer seeing that in any code I'm maintaining.