我目前正在使用Swift2创建一个存储用户输入数据的应用。现在我只是使用NSUserDefaults
,即使我将来会使用外部数据库。
通过我当前的实现,我将2个词典存储到NSUserDefaults
。 " allNouns"," myNouns"," nounTimes"。当我加载我的应用程序时,数据只会每隔一次加载一次。我有我的代码来获取数据和日志打印输出。
func saveObject(object: AnyObject, objectKey: String) {
let objectData = NSKeyedArchiver.archivedDataWithRootObject(object)
NSUserDefaults.standardUserDefaults().setObject(objectData, forKey: objectKey)
}
func loadObject(objectKey: String) -> AnyObject? {
var object : AnyObject? = nil
if( NSUserDefaults.standardUserDefaults().objectForKey(objectKey) != nil ) {
let objectData = NSUserDefaults.standardUserDefaults().objectForKey(objectKey) as? NSData
if let objectData = objectData {
object = NSKeyedUnarchiver.unarchiveObjectWithData(objectData)!
}
}
return object
}
override func viewDidLoad() {
super.viewDidLoad()
if( NSUserDefaults.standardUserDefaults().objectForKey("allNouns") != nil ) {
allNouns = loadObject("allNouns") as! [Int : Noun]
allNounIdList = Array(allNouns.keys)
}
if( NSUserDefaults.standardUserDefaults().objectForKey("myNouns") != nil ) {
myNouns = loadObject("myNouns") as! [Int : [NSDate]]
myNounIdList = Array(myNouns.keys)
}
}
以下是我保存数据的所有地方:
func stashNoun(nounId: Int) {
let myNounTimes = myNouns[nounId]
if( myNounTimes == nil || myNounTimes!.isEmpty ) {
myNouns[nounId] = [ NSDate() ]
myNounIdList.append(nounId)
}
else {
myNouns[nounId]!.append(NSDate())
}
saveObject(myNouns, objectKey: "my")
}
@IBAction func addButtonClicked(sender: AnyObject) {
let newNoun = Noun(name: nameTextField, type: typeTextField, year: yearTextField)
allNouns[newNoun.id] = newNoun
allNounIdList.append(newNoun.id)
saveObject(allNouns, objectKey: "allNouns")
}
这是我有数据的日志:
["AppleKeyboards": (
"en_US@hw=US;sw=QWERTY",
"emoji@sw=Emoji",
"en_US@hw=US;sw=QWERTY"
), "allNouns": <62706c69 73743030 d4010203 04050665 66582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0af 10130708 1718191a 1b292a31 3f404e4f 50515260 6155246e 756c6cd3 090a0b0c 1116574e 532e6b65 79735a4e 532e6f62 6a656374 73562463 6c617373 a40d0e0f 10800280 03800480 05a41213 14158006 8009800b 80108012 13688188 08c2d54a e71391c9 681dc633 746913ce dfdb57f5 78a12113 cddfb115 bd62466c d71c1d0b 1e1f2021 22222425 26222256 72656769 6f6e5563 6f6c6f72 52696454 79656172 54747970 65546e61 6d658007 80078008 13688188 08c2d54a e7100380 07800751 63d22b2c 2d2e5a24 636c6173 736e616d 65582463 6c617373 65735e43 6f726b53 74617368 2e57696e 65a22f30 5e436f72 6b537461 73682e57 696e6558 4e534f62 6a656374 d732330b 34353637 3838243b 3c383856 72656769 6f6e5563 6f6c6f72 52696454 79656172 54747970 65546e61 6d65800a 800a8008 1391c968 1dc63374 69100280 0a800a51 62d74142 0b434445 46474824 4a4b4c4d 56726567 696f6e55 636f6c6f 72526964 54796561 72547479 7065546e 616d6580 0e800f80 0813cedf db57f578 a1211107 c5800d80 0c5f101a 696c6927 73207375 70657220 64656c69 63696f75 73207769 6e655c70 696e6f74 20677269 67696f5a 41757374 72616c69 616e5577 68697465 d753540b 55565758 5959245c 5d595956 72656769 6f6e5563 6f6c6f72 52696454 79656172 54747970 65546e61 6d658011 80118008 13cddfb1 15bd6246 6c100180 11801151 61d22b2c 62635c4e 53446963 74696f6e 617279a2 64305c4e 53446963 74696f6e 6172795f 100f4e53 4b657965 64417263 68697665 72d16768 54726f6f 74800100 08001100 1a002300 2d003200 37004d00 53005a00 62006d00 74007900 7b007d00 7f008100 86008800 8a008c00 8e009000 9900a200 ab00b400 c300ca00 d000d300 d800dd00 e200e400 e600e800 f100f300 f500f700 f900fe01 09011201 21012401 33013c01 4b015201 58015b01 60016501 6a016c01 6e017001 79017b01 7d017f01 81019001 97019d01 a001a501 aa01af01 b101b301 b501be01 c101c301 c501e201 ef01fa02 00020f02 16021c02 1f022402 29022e02 30023202 34023d02 3f024102 43024502 4a025702 5a026702 79027c02 81000000 00000002 01000000 00000000 69000000 00000000 00000000 00000002 83>, "AppleKeyboardsExpanded": 1, "AddingEmojiKeybordHandled": 1, "AppleLanguages": (
"en-US"
), "ApplePasscodeKeyboards": (
"en_US"
), "nounTimes": <62706c69 73743030 d4010203 0405063f 40582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0af 100f0708 15161718 1d21272a 2d313438 3b55246e 756c6cd3 090a0b0c 1014574e 532e6b65 79735a4e 532e6f62 6a656374 73562463 6c617373 a30d0e0f 80028003 8004a311 12138005 800a800c 800e1368 818808c2 d54ae713 cddfb115 bd62466c 13cedfdb 57f578a1 21d20a0b 191ca21a 1b800680 088009d2 1e0b1f20 574e532e 74696d65 2341bbaa 1eb23666 778007d2 22232425 5a24636c 6173736e 616d6558 24636c61 73736573 564e5344 617465a2 2426584e 534f626a 656374d2 1e0b2820 2341bbab 0aa3ea33 6a8007d2 22232b2c 574e5341 72726179 a22b26d2 0a0b2e1c a12f800b 8009d21e 0b322023 41bbaa1c 3b5fcfc4 8007d20a 0b351ca1 36800d80 09d21e0b 39202341 bbab0a88 d0ea0780 07d22223 3c3d5c4e 53446963 74696f6e 617279a2 3e265c4e 53446963 74696f6e 6172795f 100f4e53 4b657965 64417263 68697665 72d14142 54726f6f 74800100 08001100 1a002300 2d003200 37004900 4f005600 5e006900 70007400 76007800 7a007e00 80008200 84008600 8f009800 a100a600 a900ab00 ad00af00 b400bc00 c500c700 cc00d700 e000e700 ea00f300 f8010101 03010801 10011301 18011a01 1c011e01 23012c01 2e013301 35013701 39013e01 47014901 4e015b01 5e016b01 7d018001 85000000 00000002 01000000 00000000 43000000 00000000 00000000 00000001 87>, "AppleLocale": en_US, "NSInterfaceStyle": macintosh, "MSVLoggingMasterSwitchEnabledKey": 0, "NSLanguages": (
"en-US",
en
), "AppleITunesStoreItemKinds": (
audiobook,
"tv-episode",
booklet,
software,
"software-update",
"itunes-u",
ringtone,
"tv-season",
movie,
mix,
newsstand,
song,
wemix,
tone,
artist,
"podcast-episode",
podcast,
document,
eBook,
album,
"music-video"
), "AppleLanguagesDidMigrate": 9.0, "myNouns": <62706c69 73743030 d4010203 04050653 54582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0af 10100708 15161718 26272e3c 3d4b4c4d 4e4f5524 6e756c6c d3090a0b 0c101457 4e532e6b 6579735a 4e532e6f 626a6563 74735624 636c6173 73a30d0e 0f800280 038004a3 11121380 05800880 0a800f13 68818808 c2d54ae7 13cddfb1 15bd6246 6c13cedf db57f578 a121d719 1a0b1b1c 1d1e1f1f 2122231f 1f567265 67696f6e 55636f6c 6f725269 64547965 61725474 79706554 6e616d65 80068006 80071368 818808c2 d54ae710 03800680 065163d2 28292a2b 5a24636c 6173736e 616d6558 24636c61 73736573 5e436f72 6b537461 73682e57 696e65a2 2c2d5e43 6f726b53 74617368 2e57696e 65584e53 4f626a65 6374d72f 300b3132 33343535 21383935 35567265 67696f6e 55636f6c 6f725269 64547965 61725474 79706554 6e616d65 80098009 800713cd dfb115bd 62466c10 01800980 095161d7 3e3f0b40 41424344 45214748 494a5672 6567696f 6e55636f 6c6f7252 69645479 65617254 74797065 546e616d 65800d80 0e800713 cedfdb57 f578a121 1107c580 0c800b5f 101a696c 69277320 73757065 72206465 6c696369 6f757320 77696e65 5c70696e 6f742067 72696769 6f5a4175 73747261 6c69616e 55776869 7465d228 2950515c 4e534469 6374696f 6e617279 a2522d5c 4e534469 6374696f 6e617279 5f100f4e 534b6579 65644172 63686976 6572d155 5654726f 6f748001 00080011 001a0023 002d0032 0037004a 00500057 005f006a 00710075 00770079 007b007f 00810083 00850087 00900099 00a200b1 00b800be 00c100c6 00cb00d0 00d200d4 00d600df 00e100e3 00e500e7 00ec00f7 0100010f 01120121 012a0139 01400146 0149014e 01530158 015a015c 015e0167 0169016b 016d016f 017e0185 018b018e 01930198 019d019f 01a101a3 01ac01af 01b101b3 01d001dd 01e801ee 01f30200 02030210 02220225 022a0000 00000000 02010000 00000000 00570000 00000000 00000000 00000000 022c>]
以下是没有保存我的数据的时间:
["AppleLocale": en_US, "NSInterfaceStyle": macintosh, "MSVLoggingMasterSwitchEnabledKey": 0, "NSLanguages": (
"en-US",
en
), "AppleKeyboards": (
"en_US@hw=US;sw=QWERTY",
"emoji@sw=Emoji",
"en_US@hw=US;sw=QWERTY"
), "AppleKeyboardsExpanded": 1, "AppleITunesStoreItemKinds": (
audiobook,
"tv-episode",
booklet,
software,
"software-update",
"itunes-u",
ringtone,
"tv-season",
movie,
mix,
newsstand,
song,
wemix,
tone,
artist,
"podcast-episode",
podcast,
document,
eBook,
album,
"music-video"
), "AddingEmojiKeybordHandled": 1, "AppleLanguagesDidMigrate": 9.0, "AppleLanguages": (
"en-US"
), "ApplePasscodeKeyboards": (
"en_US"
)]
答案 0 :(得分:0)
当您完成向NSUserDefaults
发送数据后,您必须从其实例中调用synchronize
以确保您的数据已保留。否则,您的数据可能无法及时保留(应用程序可能会在自动调用之前终止)。