【问题标题】:Update Every Day in swift syntax以快速的语法更新每一天
【发布时间】:2015-07-28 03:11:26
【问题描述】:

使用报价应用程序,作为初学者,我决定在我的应用程序中排除使用 CoreData 和 Sqlite。因此我决定尝试一个集合并更改文本标签。我将一个集合存储在一个数组中。我正在尝试实现每 24 小时更改一次文本,它在东部标准时间上午 8:00 更改(所以上午 8 点到上午 8 点)我希望大纲类似于

quoteindex = 0
if(time_elasped:24 hours something about 8:00 A.M EST) {
quote.text = quoteCollection.quoteArray[quoteIndex]
quoteindex++ (next quote in array)
}

我将如何在语法方面组织这样的事情?我会使用另一个循环吗?

【问题讨论】:

  • 为什么不能设置重复NSTimer 来实现这一点?
  • 你能在答案部分展示如何做这样的事情吗?

标签: ios swift


【解决方案1】:

一个简单的方法是使用 NSUserDefaults 来存储一个 NSDictionary,其中包含用户最后一次检索报价的时间和索引。

在 viewDidLoad 中:(或做成独立函数 - checkLastRetrieval())

let userDefaults = NSUserDefaults.standardUserDefaults()

if let lastRetrieval = userDefaults.dictionaryForKey("lastRetrieval") {
    if let lastDate = lastRetrieval["date"] as? NSDate {
        if let index = lastRetrieval["index"] as? Int {
           if abs(lastDate.timeIntervalSinceNow) > 86400 { // seconds in 24 hours
            // Time to change the label
            var nextIndex = index + 1

            // Check to see if next incremented index is out of bounds
            if self.myQuoteArray.count <= nextIndex {
                // Move index back to zero? Behavior up to you...
                nextIndex = 0
            }

            self.myLabel.text = self.myQuoteArray[nextIndex]

            let lastRetrieval : [NSObject : AnyObject] = [
               "date" : NSDate(),
               "index" : nextIndex
             ]

             userDefaults.setObject(lastRetrieval, forKey: "lastRetrieval")
             userDefaults.synchronize()
        }
        // Do nothing, not enough time has elapsed to change labels
      }
   }
} else {

  // No dictionary found, show first quote
  self.myLabel.text = self.myQuoteArray.first!

  // Make new dictionary and save to NSUserDefaults
  let lastRetrieval : [NSObject : AnyObject] = [
    "date" : NSDate(),
    "index" : 0
  ]

  userDefaults.setObject(lastRetrieval, forKey: "lastRetrieval")
  userDefaults.synchronize()
}

如果您想确保特定时间(如上午 8 点)或确保每个实际日期(周一、周二等)都有唯一的报价,您可以使用 NSDate 更具体。如果用户在 24 小时前看到了报价,则此示例只是更改标签。

查看NSUserDefaults 的文档。

编辑: 如果您想在第二天早上 8 点通知用户新报价,您可以向用户发送local notification

let notification = UILocalNotification()

notification.fireDate = NSDate(timeIntervalSinceNow: someTimeInterval)
notification.timeZone = NSCalender.currentCalendar().timeZone

notification.alertBody = "Some quote" // or "Check the app"
notiication.hasAction = true
notification.alertAction = "View"

application.scheduleLocalNotification(notification)

您必须将 timeInterval 计算为第二天早上 8 点剩下的任何时间。看看这个答案:https://stackoverflow.com/a/15262058/2881524(这是objective-c,但你应该能够弄清楚)

编辑

要在视图进入前台时执行代码,您需要在 AppDelegate 的 applicationWillEnterForeground 方法中发布通知。并在您的视图控制器中为该通知添加一个观察者。

AppDelegate

let notification = NSNotification(name: "CheckLastQuoteRetrieval", object: nil)
NSNotificationCenter.defaultCenter().postNotification(notification)

ViewController

 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("checkLastRetrieval"), name: "CheckLastQuoteRetrieval", object: nil)
    checkLastRetrieval()

}

【讨论】:

  • 你的索引和下一个索引让我很困惑......你能进一步解释一下吗?
  • index 是之前的索引(之前访问过的引用),nextIndex 只是加 1 的索引(要显示的索引)。
  • 感谢回复。最后我想问一下,我如何能够设置 8:00 AM 的功能而不是检索后的 24 小时
  • 嗯,感谢您的更新,但它似乎不适用于我的 ios 模拟器。我已经等了超过 24 小时,报价似乎没有更新。对此有什么想法吗?
  • 尝试使用更短的时间进行调试......您不必等待 24 小时才能看到它是否正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
相关资源
最近更新 更多