ios - Swift Array(string) not working -
i'm trying make hangman game. i'm going paste code in because feel problem somewhere in here i'm not seeing. down , find part wrote error occurs. here error message get: missing argument label 'arrayliteral' in call. when add arrayliteral code not work. i'm new swift appreciated! thank you! (sorry there's lot of code that's unrelated 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 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) 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") 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]() (index, character) in enumerate(word) { loc.append(string(character)) } var loc_except = [string]() (index, character) in enumerate(except) { loc_except.append(string(character)) } 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 won , ask continue play again , put how many guesses took them let ac = uialertcontroller(title: "you won", message: "congrats. took \(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 lost , ask continue play again , give word let ac = uialertcontroller(title: "you lost", message: "the word \(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() // additional setup after loading view, typically nib. guess_field.delegate = self start_game() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
because declared secret_word_loc
type of array<string>
, , array<secret_word>
type of array<character>
. either declare var secret_word_loc = [character]()
or secret_word_loc = map("secret_word") {string($0)}
convert each character in secret_word
string
, put them in array
.
Comments
Post a Comment