如何保存多个字符串?

How do I save multiple strings?

我在 ibaction 中有这段代码

NSUserDefaults().setObject(TAValue1.text, forKey: TAValue1Key)
NSUserDefaults().setObject(TAValue2.text, forKey: TAValue2Key)
NSUserDefaults().setObject(TAValue3.text, forKey: TAValue3Key)
NSUserDefaults().setObject(TAValue4.text, forKey: TAValue4Key)
NSUserDefaults().setObject(TAValue5.text, forKey: TAValue5Key)
NSUserDefaults().setObject(TAValue6.text, forKey: TAValue6Key)
NSUserDefaults().setObject(TAValue7.text, forKey: TAValue7Key)
NSUserDefaults().setObject(TAValue8.text, forKey: TAValue8Key)
NSUserDefaults().setObject(TAValue9.text, forKey: TAValue9Key)
NSUserDefaults().setObject(TAHowMany1.text, forKey: TAHowMany1Key)
NSUserDefaults().setObject(TAHowMany2.text, forKey: TAHowMany2Key)
NSUserDefaults().setObject(TAHowMany3.text, forKey: TAHowMany3Key)
NSUserDefaults().setObject(TAHowMany4.text, forKey: TAHowMany4Key)
NSUserDefaults().setObject(TAHowMany5.text, forKey: TAHowMany5Key)
NSUserDefaults().setObject(TAHowMany6.text, forKey: TAHowMany6Key)
NSUserDefaults().setObject(TAHowMany7.text, forKey: TAHowMany7Key)
NSUserDefaults().setObject(TAHowMany8.text, forKey: TAHowMany8Key)
NSUserDefaults().setObject(TAHowMany9.text, forKey: TAHowMany9Key)
NSUserDefaults().setObject(TATotal1.text, forKey: TATotal1Key)
NSUserDefaults().setObject(TATotal2.text, forKey: TATotal2Key)
NSUserDefaults().setObject(TATotal3.text, forKey: TATotal3Key)
NSUserDefaults().setObject(TATotal4.text, forKey: TATotal4Key)
NSUserDefaults().setObject(TATotal5.text, forKey: TATotal5Key)
NSUserDefaults().setObject(TATotal6.text, forKey: TATotal6Key)
NSUserDefaults().setObject(TATotal7.text, forKey: TATotal7Key)
NSUserDefaults().setObject(TATotal8.text, forKey: TATotal8Key)
NSUserDefaults().setObject(TATotal9.text, forKey: TATotal9Key)
NSUserDefaults().setObject(TATotal10.text, forKey: TATotal10Key)

我觉得 Xcode 正在获取最后一个文本 (TATotal10.text) 并仅保存它。然后将其加载到我希望重新加载数据的每个文本字段框中。 如何创建单独保存的字符串? 干杯

这是我的加载数据代码。

 override func viewDidLoad() {

    TAValue1.text = NSUserDefaults().stringForKey(TAValue1Key)
    TAValue2.text = NSUserDefaults().stringForKey(TAValue2Key)
    TAValue3.text = NSUserDefaults().stringForKey(TAValue3Key)
    TAValue4.text = NSUserDefaults().stringForKey(TAValue4Key)
    TAValue5.text = NSUserDefaults().stringForKey(TAValue5Key)
    TAValue6.text = NSUserDefaults().stringForKey(TAValue6Key)
    TAValue7.text = NSUserDefaults().stringForKey(TAValue7Key)
    TAValue8.text = NSUserDefaults().stringForKey(TAValue8Key)
    TAValue9.text = NSUserDefaults().stringForKey(TAValue9Key)
    TAHowMany1.text = NSUserDefaults().stringForKey(TAHowMany1Key)
    TAHowMany2.text = NSUserDefaults().stringForKey(TAHowMany2Key)
    TAHowMany3.text = NSUserDefaults().stringForKey(TAHowMany3Key)
    TAHowMany4.text = NSUserDefaults().stringForKey(TAHowMany4Key)
    TAHowMany5.text = NSUserDefaults().stringForKey(TAHowMany5Key)
    TAHowMany6.text = NSUserDefaults().stringForKey(TAHowMany6Key)
    TAHowMany7.text = NSUserDefaults().stringForKey(TAHowMany7Key)
    TAHowMany8.text = NSUserDefaults().stringForKey(TAHowMany8Key)
    TAHowMany9.text = NSUserDefaults().stringForKey(TAHowMany9Key)
    TATotal1.text = NSUserDefaults().stringForKey(TATotal1Key)
    TATotal2.text = NSUserDefaults().stringForKey(TATotal2Key)
    TATotal3.text = NSUserDefaults().stringForKey(TATotal3Key)
    TATotal4.text = NSUserDefaults().stringForKey(TATotal4Key)
    TATotal5.text = NSUserDefaults().stringForKey(TATotal5Key)
    TATotal6.text = NSUserDefaults().stringForKey(TATotal6Key)
    TATotal7.text = NSUserDefaults().stringForKey(TATotal7Key)
    TATotal8.text = NSUserDefaults().stringForKey(TATotal8Key)
    TATotal9.text = NSUserDefaults().stringForKey(TATotal9Key)
    TATotal10.text = NSUserDefaults().stringForKey(TATotal10Key)

}

希望对您有所帮助

要将数据存储在 NSUSerDefaults 中,试试这个

// Saving data in nsuserdefaults
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:TAValue1.text forKey:TAValue1Key];
[defaults setObject:TAValue2.text forKey:TAValue2Key];
[defaults setObject:TAValue3.text forKey:TAValue3Key];
[defaults setObject:TAValue4.text forKey:TAValue4Key];
[defaults setObject:TAValue5.text forKey:TAValue5Key];


// Fetching data from nsuserdefaults corresponding to key
NSString* taValue1 = [defaults objectForKey:TAValue1Key];
NSString* taValue2 = [defaults objectForKey:TAValue2Key];
NSString* taValue3 = [defaults objectForKey:TAValue3Key];
NSString* taValue4 = [defaults objectForKey:TAValue4Key];
NSString* taValue5 = [defaults objectForKey:TAValue5Key];

试试这个。希望它能帮到你..谢谢

或者您可以创建标签或文本字段数组并执行如下操作:

for var i = 1; i <= TAValues.count; i++
{
    NSUserDefaults().setObject(TAValues[i-1], forKey: "TAValue\(i)Key")
}
for var i = 0; i < TAHowManys.count; i++
{
    NSUserDefaults().setObject(TAValues[i], forKey: "TAValue\(i+1)Key")
}

我怀疑您的键常量都是相同的文字字符串。 (即,您 copy/pasted TATotalxxxKey 并重新编号但没有更改每个的 @"literalstringxxx" 定义)

多亏了 Joshua Nozzi 的回复,我才能够修改我的代码并解决了这个问题。

var TAValue1Key = String(01)
var TAValue2Key = String(02)
var TAValue3Key = String(03)
var TAValue4Key = String(04)
var TAValue5Key = String(05)
var TAValue6Key = String(06)
var TAValue7Key = String(07)
var TAValue8Key = String(08)
var TAValue9Key = String(09)
var TAHowMany1Key = String(10)
var TAHowMany2Key = String(11)
var TAHowMany3Key = String(12)
var TAHowMany4Key = String(13)
var TAHowMany5Key = String(14)
var TAHowMany6Key = String(15)
var TAHowMany7Key = String(16)
var TAHowMany8Key = String(17)
var TAHowMany9Key = String(18)
var TATotal1Key = String(19)
var TATotal2Key = String(20)
var TATotal3Key = String(21)
var TATotal4Key = String(22)
var TATotal5Key = String(23)
var TATotal6Key = String(24)
var TATotal7Key = String(25)
var TATotal8Key = String(26)
var TATotal9Key = String(27)
var TATotal10Key = String(28)

以前我在每个 string() 中都没有数字。

我也把我的保存码改成了

NSUserDefaults().setObject(TAValue1.text, forKey: String(01))
NSUserDefaults().setObject(TAValue2.text, forKey: String(02))
NSUserDefaults().setObject(TAValue3.text, forKey: String(03))
NSUserDefaults().setObject(TAValue4.text, forKey: String(04))
NSUserDefaults().setObject(TAValue5.text, forKey: String(05))
NSUserDefaults().setObject(TAValue6.text, forKey: String(06))
NSUserDefaults().setObject(TAValue7.text, forKey: String(07))
NSUserDefaults().setObject(TAValue8.text, forKey: String(08))
NSUserDefaults().setObject(TAValue9.text, forKey: String(09))
NSUserDefaults().setObject(TAHowMany1.text, forKey: String(10))
NSUserDefaults().setObject(TAHowMany2.text, forKey: String(11))
NSUserDefaults().setObject(TAHowMany3.text, forKey: String(12))
NSUserDefaults().setObject(TAHowMany4.text, forKey: String(13))
NSUserDefaults().setObject(TAHowMany5.text, forKey: String(14))
NSUserDefaults().setObject(TAHowMany6.text, forKey: String(15))
NSUserDefaults().setObject(TAHowMany7.text, forKey: String(16))
NSUserDefaults().setObject(TAHowMany8.text, forKey: String(17))
NSUserDefaults().setObject(TAHowMany9.text, forKey: String(18))
NSUserDefaults().setObject(TATotal1.text, forKey: String(19))
NSUserDefaults().setObject(TATotal2.text, forKey: String(20))
NSUserDefaults().setObject(TATotal3.text, forKey: String(21))
NSUserDefaults().setObject(TATotal4.text, forKey: String(22))
NSUserDefaults().setObject(TATotal5.text, forKey: String(23))
NSUserDefaults().setObject(TATotal6.text, forKey: String(24))
NSUserDefaults().setObject(TATotal7.text, forKey: String(25))
NSUserDefaults().setObject(TATotal8.text, forKey: String(26))
NSUserDefaults().setObject(TATotal9.text, forKey: String(27))
NSUserDefaults().setObject(TATotal10.text, forKey: String(28))

因为我的加载代码是由字符串名称定义的,并且字符串名称现在分配了一个字符串编号,所以无需更改我的加载代码。 对于基于其他视图控制器 .swift 文件的所有加载代码,我只是简单地为字符串命名,并使用第一个 .swift 中的字符串编号分配它。例如,我在另一个视图控制器中使用字符串 TATotal10Key。

我给它起的名字

var TaxableTotalText = String(28)

我可以随便叫它,只要它有正确的关联号码。

再次感谢您的帮助。