这是我的对象。
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'类型的操作数"。我找不到任何关于如何为int的做这件事的例子
import UIKit
首先,您不需要从NSObject
继承。此外,如果您不需要引用语义,请使用struct
s。
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聚结运算符。
let filteredTicketCellModels = ticketCellModels.filter { $0.quantity ?? 0 > 0 }
print(filteredTicketCellModels)
在上面的示例中,初始化过程中没有未知变量,因此非可选属性可能更适合。那么你就不必打开任何东西。
Leo Dabus补充道,建议所有属性都是常数。执行此操作的方法是将所有var
替换为let
。如果需要更改特性,可以创建一个新对象,从旧对象复制特性,并将新值添加到已更改的特性中。
如果想要快速安全的修复,可以使用nil合并运算符??
使item.quantity=0等于nil。
它看起来是这样的:
let filteredTicketCellModels = ticketCellModels.filter( { return ($0.quantity ?? 0 > 0)! } )
for item in filteredTicketCellModels {
print("qty: (item.quantity)")
}
按标题过滤不会崩溃的原因是,您的可选变量中没有包含nil。然而,如果item.title==为零,那么它末尾的bang(!)会导致变量访问崩溃。
如果title(或类中的任何其他变量)从不为零,则应将它们声明为var title: String
,末尾不包含?
(这意味着您的对象实际上也必须有一个初始值设定项!(无论如何,这是更好的做法:))
好吧,两个有效的解决方案是。
let filteredTicketCellModels = ticketCellModels.filter( { return ($0.quantity ?? 0 > 0) } )
第二个是
let filteredTicketCellModels = ticketCellModels.filter( { return $0.quantity != 0 } )
我还将数量的默认值设置为零。