这是我的对象。
class TicketsCellModel: NSObject {
var title: String?
var text: String?
var price: String?
var tintColor: UIColor?
var quantity: Int?
}
这里是一些随机数据
var ticketCellModels: [TicketsCellModel] = {
var cellOne = TicketsCellModel()
cellOne.title = "Standard Entry"
cellOne.text = "This is aa standard entry ticket, it's not sutiable for special events please see the plus ticket for that."
cellOne.price = "£8.35"
cellOne.tintColor = UIColor.white
cellOne.quantity = 0
var cellThree = TicketsCellModel()
cellThree.title = "Standard with re-entry"
cellThree.text = "This is a standard entry ticket but you can come and go as you please during the night."
cellThree.price = "£8.99"
cellThree.tintColor = UIColor.white
cellThree.quantity = 2
var cell6 = TicketsCellModel()
cell6.title = "Plus Entry"
cell6.text = "This is the plus entry ticket for special events."
cell6.price = "£9.99"
cell6.tintColor = UIColor.rgb(red: 192, green: 192, blue: 192)
cell6.quantity = 0
var cell9 = TicketsCellModel()
cell9.title = "VIP Entry"
cell9.text = "Here is some more text that is to act as a description for this thing you will purchase."
cell9.price = "£12.99"
cell9.tintColor = UIColor.rgb(red: 255, green: 215, blue: 0)
cell9.quantity = 4
return [cellOne, cellThree, cell6, cell9]
}()
我现在正在尝试生成一个新的 TicketsCellModel 数组,但只有那些数量 > 0 的数组。我能够执行以下操作以过滤那些以标题“S”开头的数组
let filteredTicketCellModels = ticketCellModels.filter( { return ($0.title?.starts(with: "S") )! } )
for item in filteredTicketCellModels {
print("qty: \(item.title)")
}
但如果我将其调整为;
let filteredTicketCellModels = ticketCellModels.filter( { return ($0.quantity? > 0)! } )
for item in filteredTicketCellModels {
print("qty: \(item.quantity)")
}
我收到“二元运算符‘>’不能应用于‘Int’类型的操作数?”和‘国际’”。我找不到任何关于如何为 int 执行此操作的示例
最佳答案
import UIKit
首先你不需要继承NSObject
。此外,如果您不需要引用语义,请改用 struct
。
struct TicketsCellModel {
var title: String?
var text: String?
var price: String?
var tintColor: UIColor?
var quantity: Int?
}
创建[TicketsCellModel]
并不是真的需要使用闭包。只需直接分配元素即可。由于我们使用的是 struct
,因此我们不需要创建单独的 init
。
var ticketCellModels = [
TicketsCellModel(
title: "Standard Entry",
text: "This is aa standard entry ticket, it's not sutiable for special events please see the plus ticket for that.",
price: "£8.35",
tintColor: UIColor.white,
quantity: 0
),
TicketsCellModel(
title: "Standard with re-entry",
text: "This is a standard entry ticket but you can come and go as you please during the night.",
price: "£8.99",
tintColor: UIColor.white,
quantity: 2
),
TicketsCellModel(
title: "Plus Entry",
text: "This is the plus entry ticket for special events.",
price: "£9.99",
tintColor: UIColor.white,
quantity: 0
),
TicketsCellModel(
title: "VIP Entry",
text: "Here is some more text that is to act as a description for this thing you will purchase.",
price: "£12.99",
tintColor: UIColor.white,
quantity: 4
)
]
现在,如果你需要访问一个可选的,你必须先打开它。最安全的方法是使用 if let
构造或使用 nil-coalescing operator .
let filteredTicketCellModels = ticketCellModels.filter { $0.quantity ?? 0 > 0 }
print(filteredTicketCellModels)
在上面的示例中,初始化期间没有未知变量,因此非可选属性可能更适合。这样您就不必打开任何东西。
Leo Dabus补充说,建议所有属性都是常量。这样做的方法是用 let
替换所有 var
。如果您需要更改属性,您可以创建一个新对象,从旧对象复制属性并将新值添加到已更改的属性。
关于arrays - 按对象 INT 快速过滤对象数组!属性(property),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46966080/