我试图在设备上本地存储“是”或“否”的值,日复一日。 我不知道如何实现这一点,甚至不知道从哪里开始,所以我没有特别尝试任何东西。 下面是我用来提示用户输入的代码。
override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) // if user didnt yet provided value and time is after 6 pm then only alert will occur let dateNow = NSDate() let calendar = NSCalendar.currentCalendar() let hour = calendar.component(NSCalendarUnit.Hour, fromDate: dateNow) if Score == 0 && hour >= 18 && hour <= 21{ if !isAlertShown { isAlertShown = true let Alert1 = UIAlertController(title: "Meal check", message: "Had Your lunch Today", preferredStyle: .Alert) let YesAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) { UIAlertAction in self.increaseScore() self.ScoreView.text = "\(self.Score)" } let NoAction = UIAlertAction(title: "NO", style: UIAlertActionStyle.Cancel) { UIAlertAction in self.discreaseScore() self.ScoreView.text = "\(self.Score)" } Alert1.addAction(YesAction) Alert1.addAction(NoAction) self.presentViewController(Alert1, animated: true, completion: nil) } // setting if user provided value than cancel the notification and alert for today if Score == 1 { } }I am trying to store a value of Yes or No locally on the device, day to day. I am not sure how to accomplish that or even where to get started, so I did not try anything in particular yet. Below is the code I use to prompt for the user's input.
override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) // if user didnt yet provided value and time is after 6 pm then only alert will occur let dateNow = NSDate() let calendar = NSCalendar.currentCalendar() let hour = calendar.component(NSCalendarUnit.Hour, fromDate: dateNow) if Score == 0 && hour >= 18 && hour <= 21{ if !isAlertShown { isAlertShown = true let Alert1 = UIAlertController(title: "Meal check", message: "Had Your lunch Today", preferredStyle: .Alert) let YesAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) { UIAlertAction in self.increaseScore() self.ScoreView.text = "\(self.Score)" } let NoAction = UIAlertAction(title: "NO", style: UIAlertActionStyle.Cancel) { UIAlertAction in self.discreaseScore() self.ScoreView.text = "\(self.Score)" } Alert1.addAction(YesAction) Alert1.addAction(NoAction) self.presentViewController(Alert1, animated: true, completion: nil) } // setting if user provided value than cancel the notification and alert for today if Score == 1 { } }最满意答案
我找到了一些你可能想看的教程。 每个教程都采用不同的方法来本地和远程存储数据。
核心数据 。 在我看来,这是你最安全的选择。 虽然可能听说设置它是可靠的,你可以在本地存储数据。 这将为您提供一个数据库,您可以在其中编写关键数据和是/否以及写入数据。 这是 Raywenderlich的精彩教程。 核心数据 解析 。 你的第二个最好的赌注是解析。 在这里,您将所有数据存储在远程数据库中,但是几个月前推出的这个惊人的新功能名为Local Datastore,允许您在本地存储数据。 在这里,您只需将所有内容上传到服务器并在对象上调用.pinInBackground() 。 这是一个很好的教程,可以让你设置解析, 这是一个很好的指南,可以使用本地数据存储。 解析和本地数据存储 。 CloudKit。 最后,您可以使用Apple自己的Parse版本,称为CloudKit(基于iCloud)。 要为开发人员设置和运行iCloud,请点击此链接 。 这与Core Data完美集成,然后为Parse创建完美的替代方案。如果你问我,我个人会选择B,Parse。 但你也可以使用1和3来创建完全相同的替代品,可能更安全,因为它完全取决于Apple自己的技术。
希望有所帮助,朱利安。
I found a few tutorials you may want to take a look at. Each tutorial is taking a different approach to storing data, both locally and remotely.
Core Data. This in my opinion is your safest bet. While it may be heard to set up it'll be reliable and you can store data locally. This will give you a database where you can write a key data and yes/no and the write data to those. Here's a great tutorial from Raywenderlich. Core Data Parse. Your second best bet would be parse. Here you store all your data in a remote database, however this amazing new feature introduced a few months back called Local Datastore allows you to also store data locally. Here you simply upload everything to the server and call .pinInBackground() on your object. Here is a great tutorial to get you set up with parse, and here is a great guide to get going with local Datastore. Parse and Local Datastore. CloudKit. Lastly, you can use Apple's own version of Parse called CloudKit(based on iCloud). To get set up and running with iCloud for developers follow this link. This integrates well with Core Data, then creating the perfect alternative for Parse.If you'd ask me, personally I'd go with option B, Parse. But you can also use 1 and 3 to create the exact same alternative, probably more secure as it's all on Apple's own tech.
Hope that helps, Julian.
更多推荐
发布评论