在每天由用户提供的iOS应用中存储值?

时间:2015-11-22 13:09:34

标签: ios xcode swift

我正在尝试在设备上本地存储“是”或“否”的值,日复一日。我不知道如何实现这一点,甚至不知道从哪里开始,所以我没有特别尝试任何东西。下面是我用来提示用户输入的代码。

  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   {



    }


}

1 个答案:

答案 0 :(得分:0)

我找到了一些你可能想看的教程。每个教程都采用不同的方法来本地和远程存储数据。

  1. 核心数据。在我看来,这是你最安全的选择。虽然可能听说设置它是可靠的,你可以在本地存储数据。这将为您提供一个数据库,您可以在其中编写关键数据和是/否以及写入数据。 Here's来自Raywenderlich的精彩教程。 Core Data
  2. 解析即可。你的第二个最好的赌注是解析。在这里,您将所有数据存储在远程数据库中,但是几个月前推出的这个惊人的新功能称为本地数据存储,您还可以在本地存储数据。在这里,您只需将所有内容上传到服务器并在对象上调用.pinInBackground()即可。 Here是一个很好的教程,可以让你设置解析,here是一个很好的指南,可以使用本地数据存储。 ParseLocal Datastore
  3. CloudKit。最后,您可以使用Apple自己的Parse版本,称为CloudKit(基于iCloud)。设置和运行 iCloud for developers link。这很好地融合了 使用Core Data,然后为Parse创建完美的替代方案。
  4. 如果你问我,我个人会选择B,Parse。但你也可以使用1和3来创建完全相同的替代品,可能更安全,因为它完全取决于Apple自己的技术。

    希望有帮助,朱利安。