Swift 数组(字符串)不工作
Swift Array(string) not working
我正在尝试制作刽子手游戏。我要把我的代码粘贴进去,因为我觉得我的问题就在这里,我只是没有看到。往下看,找到我写 THIS IS WHERE THE ERROR OCCURS 的部分。这是我收到的错误消息:调用中缺少参数标签 'arrayLiteral'。当我添加 arrayLiteral 时,代码不起作用。我是 Swift 的新手,非常感谢您的帮助!谢谢! (很抱歉,有很多代码可能与问题无关)
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.
}
}
尝试secret_word_loc = [secret_word]
。
因为你把secret_word_loc
声明为Array<String>
的类型,而Array<secret_word>
实际上是Array<Character>
的类型。您可以声明 var secret_word_loc = [Character]()
或执行 secret_word_loc = map("secret_word") {String([=15=])}
,这会将 secret_word
中的每个字符转换为 String
并将它们放入 Array
.
这里(至少)有两种可能的解决方案,因为 Array
有多个初始值设定项。
首先,我们可以使用文字数组语法,这只是一对用逗号分隔的数组元素括起来的方括号:
secret_word_loc = [secret_word]
或者,我们可以使用更详细的初始化程序,它实际上解释了错误消息的来源:
secret_word_loc = Array(arrayLiteral: secret_word)
此 Array
的初始值设定项接受以逗号分隔的数组元素列表来实例化您的数组。
两种解决方案都工作得很好。
第二个解释错误信息:
Missing argument label 'arrayLiteral'
这只是告诉你“嘿,一切都正确,但你必须使用参数标签,在这种情况下是arrayLiteral
。
然而,第一个解决方案更 Swift 风格,我绝对希望在我维护的任何代码中看到它。
我正在尝试制作刽子手游戏。我要把我的代码粘贴进去,因为我觉得我的问题就在这里,我只是没有看到。往下看,找到我写 THIS IS WHERE THE ERROR OCCURS 的部分。这是我收到的错误消息:调用中缺少参数标签 'arrayLiteral'。当我添加 arrayLiteral 时,代码不起作用。我是 Swift 的新手,非常感谢您的帮助!谢谢! (很抱歉,有很多代码可能与问题无关)
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.
}
}
尝试secret_word_loc = [secret_word]
。
因为你把secret_word_loc
声明为Array<String>
的类型,而Array<secret_word>
实际上是Array<Character>
的类型。您可以声明 var secret_word_loc = [Character]()
或执行 secret_word_loc = map("secret_word") {String([=15=])}
,这会将 secret_word
中的每个字符转换为 String
并将它们放入 Array
.
这里(至少)有两种可能的解决方案,因为 Array
有多个初始值设定项。
首先,我们可以使用文字数组语法,这只是一对用逗号分隔的数组元素括起来的方括号:
secret_word_loc = [secret_word]
或者,我们可以使用更详细的初始化程序,它实际上解释了错误消息的来源:
secret_word_loc = Array(arrayLiteral: secret_word)
此 Array
的初始值设定项接受以逗号分隔的数组元素列表来实例化您的数组。
两种解决方案都工作得很好。
第二个解释错误信息:
Missing argument label 'arrayLiteral'
这只是告诉你“嘿,一切都正确,但你必须使用参数标签,在这种情况下是arrayLiteral
。
然而,第一个解决方案更 Swift 风格,我绝对希望在我维护的任何代码中看到它。