gpt4 book ai didi

swift - 调用最近的 PFObject

转载 作者:行者123 更新时间:2023-11-30 13:57:24 28 4
gpt4 key购买 nike

我有一个计时器,可以将当前日期倒计时到我在 Parse 中设置的日期(类名:“Date”)。现在,它使用我设置的 ObjectId 检索该数据。我想将其设置为使用“updatedAt”列中的日期来调用类中最近更新的对象。我怎样才能做到这一点?

    super.viewDidLoad()

// Retrieve the date in Parse

var query = PFQuery(className:"Date")
query.getObjectInBackgroundWithId("CSJFqvJDqs") {
(dateFromParse: PFObject?, error: NSError?) -> Void in
if error == nil && dateFromParse != nil {
// Process everything here

// Step 1: Get the current date.
let currentDate = NSDate()

// Step 2: Convert the dateFromParse PFObject into a NSDate object
let electionDateComponents = NSDateComponents()
electionDateComponents.day = dateFromParse!["Day"] as! Int
electionDateComponents.month = dateFromParse!["Month"] as! Int
electionDateComponents.year = dateFromParse!["Year"] as! Int
let electionDate = NSCalendar.currentCalendar().dateFromComponents(electionDateComponents)!

// Step 3: Find the difference between dateFromParse and currentDate
let timeToElection = electionDate.timeIntervalSinceDate(currentDate)
let daysToElection = (((timeToElection/60)/60)/24)
print(daysToElection)

// Step 4: Display the number of days of difference on countDown.text
let daysText = String(format: "%.0f days", floor(daysToElection))
self.countDown.text = daysText
} else {
print(error)
}
}
}

最佳答案

您应该浏览 SDK 文档和指南:

https://parse.com/docs/ios/api/

https://parse.com/docs/ios/guide#queries

查询具有排序方法。你想做一些类似于

的事情
[query sortDescending:@"updatedAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *dateObject, NSError *error)
{
//do your stuff
}];

抱歉,刚刚意识到我给了你 obj-c 语法,而你正在使用 swift。我对swift不太熟悉,但是方法应该是一样的。只需称它们为 Swift 风格即可。

关于swift - 调用最近的 PFObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33442265/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com