NSUserDefaults不保存TextField文本(Swift)

时间:2022-08-24 16:24:28

I'm trying to create a game with Swift, and I want to add the ability to create a username, which will be saved in NSUserDefaults. This is my code:

我正在尝试使用Swift创建一个游戏,我想添加创建用户名的功能,该用户名将保存在NSUserDefaults中。这是我的代码:

println("Textfield Text: \(usernameTextfield.text)")

NSUserDefaults.standardUserDefaults().setObject(usernameTextfield.text, forKey:"Username")
NSUserDefaults.standardUserDefaults().synchronize()

println(NSUserDefaults.standardUserDefaults().objectForKey("Username") as? String)

The output is:

输出是:

Textfield Text: MyUsername

文本字段文本:MyUsername

nil

The only explanation I can see as to why it is printing nil is that either the saving or the loading of the username is failing. Is there any way this can be corrected or am I doing something wrong?

我可以看到的唯一解释为什么它是打印nil是保存或加载用户名失败。有什么方法可以纠正或我做错了什么?

Any help is appreciated!

任何帮助表示赞赏!

5 个解决方案

#1


8  

println("Textfield Text: \(usernameTextfield.text)")

var myValue:NSString = usernameTextfield.text

NSUserDefaults.standardUserDefaults().setObject(myValue, forKey:"Username")
NSUserDefaults.standardUserDefaults().synchronize()

var myOutput: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("Username")
println(myOutput)

#2


1  

In Swift 4.1

 UserDefaults.standard.set(textfield.text, forKey: "yourKey") // saves text field text
  UserDefaults.standard.synchronize()

// To Retrieve
textfield.text = UserDefaults.standard.value(forKey:"yourKey") as? String

#3


0  

I made a small modification to Roman's answer with Swift 2.0 and Xcode 6.4.

我用Swift 2.0和Xcode 6.4对Roman的回答做了一点修改。

saving:

保存:

    var myValue:NSString = usernameTF.text

    NSUserDefaults.standardUserDefaults().setObject(myValue, forKey:"Username")
    NSUserDefaults.standardUserDefaults().synchronize()

retrieving:

检索:

var myOutput = NSUserDefaults.standardUserDefaults().objectForKey("Username")
if (myOutput != nil)
{
    self.title = "Welcome "+((myOutput) as! String)
    }

#4


0  

In Swift 3.0

在Swift 3.0中

    let userDefult = UserDefaults.standard //returns shared defaults object.

    if let userName = usernameTextfield.text {
        //storing string in UserDefaults
        userDefult.set(userName, forKey: "userName") //Sets the value of the specified default key in the standard application domain.
    }

    print(userDefult.string(forKey: "userName")!)//Returns the string associated with the specified key.

#5


0  

For swift 3.0, You can create user default by,

对于swift 3.0,您可以创建用户默认值,

UserDefaults.standard.set("yourValue", forKey: "YourString")

To Print the value in console :

要在控制台中打印值:

print(UserDefaults.standard.string(forKey: "YourString")!)

#1


8  

println("Textfield Text: \(usernameTextfield.text)")

var myValue:NSString = usernameTextfield.text

NSUserDefaults.standardUserDefaults().setObject(myValue, forKey:"Username")
NSUserDefaults.standardUserDefaults().synchronize()

var myOutput: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("Username")
println(myOutput)

#2


1  

In Swift 4.1

 UserDefaults.standard.set(textfield.text, forKey: "yourKey") // saves text field text
  UserDefaults.standard.synchronize()

// To Retrieve
textfield.text = UserDefaults.standard.value(forKey:"yourKey") as? String

#3


0  

I made a small modification to Roman's answer with Swift 2.0 and Xcode 6.4.

我用Swift 2.0和Xcode 6.4对Roman的回答做了一点修改。

saving:

保存:

    var myValue:NSString = usernameTF.text

    NSUserDefaults.standardUserDefaults().setObject(myValue, forKey:"Username")
    NSUserDefaults.standardUserDefaults().synchronize()

retrieving:

检索:

var myOutput = NSUserDefaults.standardUserDefaults().objectForKey("Username")
if (myOutput != nil)
{
    self.title = "Welcome "+((myOutput) as! String)
    }

#4


0  

In Swift 3.0

在Swift 3.0中

    let userDefult = UserDefaults.standard //returns shared defaults object.

    if let userName = usernameTextfield.text {
        //storing string in UserDefaults
        userDefult.set(userName, forKey: "userName") //Sets the value of the specified default key in the standard application domain.
    }

    print(userDefult.string(forKey: "userName")!)//Returns the string associated with the specified key.

#5


0  

For swift 3.0, You can create user default by,

对于swift 3.0,您可以创建用户默认值,

UserDefaults.standard.set("yourValue", forKey: "YourString")

To Print the value in console :

要在控制台中打印值:

print(UserDefaults.standard.string(forKey: "YourString")!)