2017-07-13 7 views
-1

私は引用符のリストを持っています。 24Hごとに新しい見積もりに変更するラベルが必要です。 どうすればいいですか?どのように私は1日を管理できますか?毎日新しいアプリケーションのコンテンツ

私はAPIを使用することができますが、私は自分の引用符を使用したいと思っています。自分でAPIをまだ作成することはできません。あなたが引用符の配列を持っていると仮定すると

答えて

2

let numberOfQuotes = 3 
let quotes = ["quote a", "quote b", "quote c"] 

override func viewDidLoad() { 
    _ = Timer.scheduledTimer(timeInterval: TimeInterval(60), 
               target: self, 
               selector: #selector(self.updateQuote), 
               userInfo: nil, 
               repeats: true) 

} 

func updateQuote() { 
    let lastUpdate = UserDefaults.standard.object(forKey: "lastUpdate") as? Date 
    if lastUpdate != nil { 
     let date1:Date = Date() // Same you did before with timeNow variable 
     let date2: Date = Date(timeIntervalSince1970: lastUpdate) 

     let calender:Calendar = Calendar.current 
     let components: DateComponents = calender.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date1, to: date2) 

     // you can use components month, hour, second.... to update your message, in your case, we will day 

     if components.day! >= 1 { 
      UserDefaults.standard.set(Date(), forKey: "lastUpdate") 
      yourLabel.text = quotes[randomInt(0,numberOfQuotes)] 
     } 

    } else { //firstTime running 
      UserDefaults.standard.set(Date(), forKey: "lastUpdate") 
      yourLabel.text = quotes[randomInt(0,numberOfQuotes)] 
    } 
} 

func randomInt(min: Int, max:Int) -> Int { 
    return min + Int(arc4random_uniform(UInt32((max - 1) - min + 1))) 
} 

このコードはそのまま、あなたの説明で、それは正確に何をしたいんです。

関連する問題