gpt4 book ai didi

ios - 解析: retrieving the last object

转载 作者:行者123 更新时间:2023-11-30 14:03:50 25 4
gpt4 key购买 nike

我试图通过 desc 字段“createdAt”检索选择订单的最后一个对象:在 SQL 中是这样的:

SELECT *   
FROM Message
ORDER BY createdAt DESC
LIMIT 1

我尝试了这个,但不起作用。结果是第一个对象:

let query = PFQuery(className: "Message")

query.orderByDescending("createdAt")
query.limit = 1

var lastMessage = "..."

if let objects = query.findObjects() as [AnyObject]?
{
for object in objects
{
//Execute one time only and don't return the row that i want
lastMessage = object["Text"] as! String
}
}

通过这个懒惰的代码,我得到了我想要的,但我必须循环所有对象:

let query = PFQuery(className: "Message")

var lastMessage = "..."

if let objects = query.findObjects() as [AnyObject]?
{
for object in objects
{
//Execute many times and return the row that i want int the last
lastMessage = object["Text"] as! Strin
}
}

最佳答案

var lastMessage = "..."        

let query = PFQuery(className: "Message", predicate: predicate)

query.orderByDescending("createdAt")

if let object = query.getFirstObject() as PFObject?
{
lastMessage = object["Text"] as! String
}

关于ios - 解析: retrieving the last object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32638948/

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