如果 UserDefaults 为空,如何检查一次
How to check a single time if UserDefaults is empty
我的应用计算日期和 NSDate() 之间的天数。当我发布它时,用户只能保存一个日期、一个标题和一张背景图片。
现在我有一个 UICollectionView,可以选择保存多个日期,它会通过将日期、标题和图像字符串附加到它们各自的数组来创建一个单元格。
应用程序已经完全改变,所以我正在努力研究如何检查用户是否保存了日期,如果保存了,将该信息添加到数组中以创建一个单元格。
但是,我希望只检查一次 - 第一次从更新或全新安装打开应用程序时。
这是我的想法,顺便说一句,它不起作用。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
if userDefault.objectForKey("day") == nil {
} else {
// Add the first date created from previous version
let day = userDefault.objectForKey("day") as? String
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMMM yyyy hh:mm a"
let date = dateFormatter.dateFromString(day!)!
let date1 = dateFormatter.stringFromDate(date)
addDateToArray(date1)
// Add the since text
let text = userDefault.objectForKey("sinceText") as? String
addSinceLabelToArray(text!)
//Add the image background
let image = userDefault.objectForKey("khoury") as! String
addThemeToImagesArray(image)
}
上面的代码发生了什么returns nil。我期待它创建第一个单元格,以便用户可以看到他们保存的日期。
在您的视图控制器中创建一个数组。
var dates = [NSDate]()
然后在viewDidLoad
:
if let retrieved = userDefault.objectForKey("day") {
dates = retrieved as! [NSDate]
}
然后重新加载您的 collection 视图
您可以在 NSUserDefaults
中使用另一个布尔值来检测第一个 运行:
let defaults = NSUserDefaults.standardUserDefaults()
if (!defaults.boolForKey("migrated")) {
defaults.setBool(true, forKey: "migrated")
// ... anything else
}
我的应用计算日期和 NSDate() 之间的天数。当我发布它时,用户只能保存一个日期、一个标题和一张背景图片。
现在我有一个 UICollectionView,可以选择保存多个日期,它会通过将日期、标题和图像字符串附加到它们各自的数组来创建一个单元格。
应用程序已经完全改变,所以我正在努力研究如何检查用户是否保存了日期,如果保存了,将该信息添加到数组中以创建一个单元格。
但是,我希望只检查一次 - 第一次从更新或全新安装打开应用程序时。
这是我的想法,顺便说一句,它不起作用。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
if userDefault.objectForKey("day") == nil {
} else {
// Add the first date created from previous version
let day = userDefault.objectForKey("day") as? String
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMMM yyyy hh:mm a"
let date = dateFormatter.dateFromString(day!)!
let date1 = dateFormatter.stringFromDate(date)
addDateToArray(date1)
// Add the since text
let text = userDefault.objectForKey("sinceText") as? String
addSinceLabelToArray(text!)
//Add the image background
let image = userDefault.objectForKey("khoury") as! String
addThemeToImagesArray(image)
}
上面的代码发生了什么returns nil。我期待它创建第一个单元格,以便用户可以看到他们保存的日期。
在您的视图控制器中创建一个数组。
var dates = [NSDate]()
然后在viewDidLoad
:
if let retrieved = userDefault.objectForKey("day") {
dates = retrieved as! [NSDate]
}
然后重新加载您的 collection 视图
您可以在 NSUserDefaults
中使用另一个布尔值来检测第一个 运行:
let defaults = NSUserDefaults.standardUserDefaults()
if (!defaults.boolForKey("migrated")) {
defaults.setBool(true, forKey: "migrated")
// ... anything else
}