swift - 是否可以在 Parse 中创建特定对象的数组?
问题描述
我制作了一个 QR 扫描仪应用程序,我手动将一些 QR 码放入解析中以使其识别,任何我未放入解析的扫描的 QR 码都不会被识别。
唯一能区分他们的是他们的(信息),即“餐厅”、“美甲沙龙”等。
我正在寻找一种能够记录所选二维码被扫描次数的整数,然后放置在应用程序中的标签上的方法。
我可以(.count)用户保存和扫描的所有 qrCode,但似乎无法弄清楚如何将所有“美甲沙龙”放入他们自己的数组中进行解析或运行与我匹配的 For 循环需要。
// The code below will retrieve everything in the "info" column and print it to console
// This prints "Nails Salon" x 5, "Restaurant" x3 and "Coffee Shop" x 7 in the order that they were scanned (Unorganised)
// What block of code could I make to display what PFuser.current currently has in their parse?
// E.g. PFUser has scanned "Nail Salon" 5 Times, "Restaurant" 3 time etc etc
let infoCheck = PFQuery(className: "UserQRCodes")
infoCheck.whereKey("info", contains: "")
infoCheck.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let objects = objects {
print(objects)
}
}
// To retrieve everything the USER has scanned and display it as String on the APP
let query = PFQuery(className: "UserQRCodes")
query.whereKey("userName", equalTo: PFUser.current()!)
query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
if let error = error {
//log details of the failure
print(error.localizedDescription)
} else if let objects = objects {
let stampees: Int = objects.count
let totalStampees = String(stampees)
self.stampeesCollectedLabel.text = totalStampees
print(objects.count)
}
}
// Do any additional setup after loading the view.
}
解决方案
您想要过滤扫描数组中的元素。对于每种代码类型,调用类似
// '$0' is your PFObject. Replace 'name' with whatever `PFObject` property
// represents the object's type
let nailSalons = objects.filter { $0.name == "Nail Salon" }
然后,您可以使用此过滤后的数组来获取您的计数。
请注意,filter { $0... }
语法是
objects.filter { (object) throws -> Bool) in
return object.name == "Nail Salon"
}
如果您的条件比简单的单行表达式更复杂,您将需要使用完整版本。请注意,在简短版本中,return
隐含的是。
推荐阅读
- app-store - 发布免费应用程序,但按月订阅
- google-analytics - 谁有集成 GTM 电子商务分析 fot Prestashop 的经验
- laravel - 在所有模型中实施全局范围
- javascript - 在 React 钩子中,使用 setValue() 的 handleChange 中没有 e.target
- google-bigquery - 下载 .sql 文件 BigQuery
- c# - 使用列表对象返回列表对象
- forms - Typo3 Powermail 表单 - 将页面 UID 发送给接收者
- r - 在 grid.arrange 中使用 tex 表达式
- android - mysql驱动程序类未找到异常
- perl - 如何为 MSYS Perl 安装基本模块?