gpt4 book ai didi

ios - nsuserdefault 不保存数组 - Swift

转载 作者:可可西里 更新时间:2023-11-01 01:43:38 24 4
gpt4 key购买 nike

我可以得到一个字符串保存到 NSUserDefaults 但我不确定为什么它不会使用下面的代码保存一个数组。我确定它很小,但可以使用指针。

//
// ViewController.swift
// DemoUserDefaults
//
// Created by Chris Cantley on 10/7/14.
// Copyright (c) 2014 Chris Cantley. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

var storeNames:[String] = []

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.


storeNames.append("Billy")
storeNames.append("Chris")

// Shows the strings in the array.
println(storeNames)


// puts mutable into immutable object
let holdNames = storeNames

// "should" store the object into UserDefaults... but does not.
NSUserDefaults.standardUserDefaults().setObject(holdNames, forKey: "storeNames")
NSUserDefaults.standardUserDefaults().synchronize()


// Displays all data in UserDefaults... array is missing.
println(NSUserDefaults.standardUserDefaults().dictionaryRepresentation())


}

}

结果...

[Billy, Chris]
[NSLanguages: (
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"
), AppleKeyboardsExpanded: 1, NSInterfaceStyle: macintosh, AppleKeyboards: (
"en_US@hw=US;sw=QWERTY",
"emoji@sw=Emoji",
"en_US@hw=US;sw=QWERTY"
), AppleLanguages: (
en
), names: Rob]

注意:“names:Rob”来自之前的单个字符串保存。

最佳答案

数字...我发布了一个问题,我花了数小时寻找自己的答案,几分钟后我找到了解决方案。

无论如何,NSUserDefaults 似乎不喜欢“String”,但是将其更改为“NSString”是可行的。

所以变化是...

var storeNames:[String] = []

var storeNames:[NSString] = []

关于ios - nsuserdefault 不保存数组 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26238128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com