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.
}
}
Because you declared
secret_word_loc
as a type ofArray<String>
, andArray<secret_word>
is actually a type ofArray<Character>
. You could either declarevar secret_word_loc = [Character]()
or dosecret_word_loc = map("secret_word") {String($0)}
which will convert each character insecret_word
toString
and put them in anArray
.