用swift语法更新每一天 [英] Update Every Day in swift syntax

查看:109
本文介绍了用swift语法更新每一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用引用应用程序,作为初学者,我决定在我的应用程序中排除使用CoreData和Sqlite。因此我决定尝试一个集合并更改文本标签。我有一个存储在数组中的集合。我试图实现每24小时更改一次的文本,它在美国东部时间早上8点(早上8点到早上8点)发生变化。我希望大纲有点像

Working on a quote app, and as a beginner I decided to rule out using CoreData and Sqlite in my app. Therefore I decided to try a collection and change the text label.I have a collection stored in an array. I'm trying to achieve the text changing every 24 hours and it changes at 8:00 A.M E.S.T (so 8 A.M to 8 A.M)I would like the outline to be something like

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

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

How would I organize something like this in terms of syntax? Would I use another loop?

推荐答案

一种简单的方法是使用NSUserDefaults存储包含上次的NSDictionary用户上次检索报价时的索引。

An easy way to do this would be to use NSUserDefaults to store an NSDictionary containing the last time and index when the user last retrieved a quote.

在viewDidLoad中:(或制作独立函数 - checkLastRetrieval())

in viewDidLoad: (or make into standalone function - 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小时前看到报价,此示例只会更改标签。

You can be more specific using NSDate if you want to ensure a specific time (like 8AM) or make sure that there is a unique quote for every actual day (Monday, Tuesday, etc). This example simply changes the label if the user has seen a quote more than 24 hours ago.

查看 NSUserDefaults

编辑
如果您想在新报价的第二天上午8点通知用户,您可以发送本地通知给用户。

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 (这是客观的 - 但你应该能够弄清楚)

You would have to calculate the timeInterval to be whatever time is left to 8AM the next day. Check this answer out: https://stackoverflow.com/a/15262058/2881524 (It's objective-c but you should be able to figure it out)

编辑

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

To execute code when your view enters the foreground you need to post a notification in your AppDelegate's applicationWillEnterForeground method. And add an observer for that notification in your view controller.

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()

}

这篇关于用swift语法更新每一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆