由对象INT快速过滤对象数组!属性

时间:2017-10-27 01:17:57

标签: arrays swift filter properties int

这是我的目标。

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的

做这个的例子

3 个答案:

答案 0 :(得分:1)

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补充说,建议所有属性都是常量。执行此操作的方法是将所有var替换为let。如果需要更改属性,可以创建一个新对象,复制旧属性并将新值添加到已更改的属性中。

答案 1 :(得分:0)

如果您想快速安全地修复,可以使用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是==为nil,那么它结尾处的爆炸(!)会导致变量访问时崩溃。

如果标题(或类中的任何其他变量)永远不是nil,则应将它们声明为var title: String,最后不要?(这意味着您必须实际拥有你的对象的初始化器也是!(不管怎样,这是更好的练习:)))

答案 2 :(得分:0)

好的,这两个有效的解决方案是。

let filteredTicketCellModels = ticketCellModels.filter( { return ($0.quantity ?? 0 > 0) } )

第二个是: -

let filteredTicketCellModels = ticketCellModels.filter( { return $0.quantity != 0 } )

我还将数量的默认值设置为零。