我已经构建了一个名为Mad Lib的简单应用。在这里,您将11个单词输入文本字段。单词被附加到名为wordStack的列表中。您单击确定按钮并准备进行segway,将用户带到第二个屏幕。在这里,将11个单词作为字符串填充到文本中。
第二页上txt的示例:“ Tarzan也被称为(wordStack [1])”
因此,在我阅读时,似乎在访问属性的用户默认值时内容是不可变的,并且它还用于简单数据,例如单个字符串或整数。我很难让它与此数组一起工作。
因此,除了以下事实之外,这还算有效
var wordStack: [String] = []
每次重置工作堆栈。
任何人帮助我并做些什么将是一个好的解决方案的解释都是绝对的英雄!谢谢!
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let s = userDefault.valueForKey("wordstack"){
var wordStack: [String] = []
for i in s as! NSArray {
print(i)
wordStack.append(String(i))
print(wordStack)
}
}
print(wordStack)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var guess: UILabel!
@IBOutlet weak var textField: UITextField!
// Save game state.
var userDefault = NSUserDefaults.standardUserDefaults()
// Saves the user's input.
var wordStack: [String] = []
func test() {
let userInput = textField.text
textField.text = nil
if wordStack.count < 10 {
wordStack.append(userInput!)
guess.text = "\(11-wordStack.count) guesses left!"
userDefault.setValue(wordStack, forKey: "wordstack")
} else {
wordStack.append(userInput!)
}
}
// When pressed enter in the textfield append the input to workstack and refresh textfield for new input.
@IBAction func enterTextField(sender: AnyObject) {
test()
}
@IBAction func buttonClick(sender: AnyObject) {
if wordStack.count <= 10 {
let alertController = UIAlertController(title: "Wacht", message: "Je bent nog niet klaar. \(wordStack.count)/11 woorden ingevuld.",preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController,animated: true, completion: nil)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let secondViewController: ViewController2 = segue.destinationViewController as! ViewController2
if wordStack.count > 10 {
secondViewController.outputMessage = "One of the most silly characters is named Tarzan of the bees. Tarzan is raised by an/a \(wordStack[0]) and lives in the \(wordStack[1]) in the heart of the darkest \(wordStack[2]). He spent most of his time eating \(wordStack[3]) and swinging from tree to \(wordStack[4]). Whenever he gets angry he beats on his chest and says \(wordStack[5])! This is his way cry. Tarzan always dresses in smelly \(wordStack[6]) shorts made of the skin of an/a \(wordStack[7]) and his best friend is an/a \(wordStack[8]) Chimpanzee called Cheetah. He is supposed to be able to speak to elephants and \(wordStack[9]). In the movies, Tarzan plays \(wordStack[10])"
// reset guesses
//var count: Int = 0
}
}
}
最佳答案
问题是您声明了一个局部变量wordStack
,该局部变量与具有相同名称的实例属性不同。
即使从用户默认值读取值,用var
声明的变量也总是可变的。
简单的解决方案是在var
中的wordStack
之前删除viewDidLoad()
关键字
更好的解决方案:
在applicationDidFinishLaunching
的AppDelegate
中注册键/值对
let defaults = NSUserDefaults.standardUserDefaults()
let defaultValues = ["wordstack" : [String]()]
defaults.registerDefaults(defaultValues)
然后只需更换
if let s = userDefault.valueForKey("wordstack"){
var wordStack: [String] = []
for i in s as! NSArray {
print(i)
wordStack.append(String(i))
print(wordStack)
}
}
与
wordStack = userDefault.objectForKey("wordstack")
由于存在空数组的默认值,因此
wordStack
永远不能为nil
,并且类型安全。注意:除非确实需要KVC方法,否则请始终使用
objectForKey
而不是valueForKey
。关于arrays - 如何通过将数据保存为NsUserDefaults中的数组来记住游戏状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33709230/