Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 按对象INT快速筛选对象数组!财产_Arrays_Swift_Filter_Properties_Int - Fatal编程技术网

Arrays 按对象INT快速筛选对象数组!财产

Arrays 按对象INT快速筛选对象数组!财产,arrays,swift,filter,properties,int,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 = TicketsCellMo

这是我的目标

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]
}()
我现在正在尝试生成一个新的TicketCellModel数组,但只使用那些数量大于0的数组。我可以按标题“S”开头的人进行以下筛选

但如果我把它调整到

let filteredTicketCellModels = ticketCellModels.filter( { return ($0.quantity? > 0)! } )
for item in filteredTicketCellModels {
    print("qty: \(item.quantity)")
}

我得到“二进制运算符'>'不能应用于'Int'和'Int'类型的操作数。我找不到任何例子来说明我是如何对int进行此操作的

如果您想要快速而安全的修复,可以使用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,则其末尾的砰砰(!)会导致变量访问崩溃


如果标题(或类中的任何其他变量)从不为零,则应将其声明为仅
var title:String
,末尾不带
(这意味着您也必须为您的对象实际拥有一个初始值设定项!(这是更好的做法:)

如果您想要快速安全的修复,当item.quantity=0等于nil时,可以使用nil合并运算符
??

看起来是这样的:

let filteredTicketCellModels = ticketCellModels.filter( { return ($0.quantity ?? 0 > 0)! } )
    for item in filteredTicketCellModels {
        print("qty: \(item.quantity)")
    }
按标题筛选不会崩溃的原因是,没有一个可选变量包含nil。但是,如果item.title为==到nil,则其末尾的砰砰(!)会导致变量访问崩溃

如果标题(或类中的任何其他变量)从不为零,则应将其声明为仅
var title:String
,末尾不带
(这意味着您也必须为对象指定一个初始值设定项!)

首先,您不需要从
NSObject
继承。此外,如果不需要引用语义,请使用
struct
s

struct TicketsCellModel {
  var title: String?
  var text: String?
  var price: String?
  var tintColor: UIColor?
  var quantity: Int?
}
实际上不需要使用闭包来创建
[ticketcellmodel]
。直接分配元素即可。由于我们使用的是
struct
s,因此不需要创建单独的
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
构造或使用

在上面的示例中,初始化期间没有未知变量,因此非可选属性可能更适合。那你就不用打开任何东西了

添加建议所有属性都为常量。方法是将所有
var
s替换为
let
s。如果需要更改特性,可以创建一个新对象,从旧对象复制特性,并将新值添加到已更改的特性中

首先,您不需要从
NSObject
继承。此外,如果不需要引用语义,请使用
struct
s

struct TicketsCellModel {
  var title: String?
  var text: String?
  var price: String?
  var tintColor: UIColor?
  var quantity: Int?
}
实际上不需要使用闭包来创建
[ticketcellmodel]
。直接分配元素即可。由于我们使用的是
struct
s,因此不需要创建单独的
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
构造或使用

在上面的示例中,初始化期间没有未知变量,因此非可选属性可能更适合。那你就不用打开任何东西了


添加建议所有属性都为常量。方法是将所有
var
s替换为
let
s。如果需要更改属性,您可以创建一个新对象,从旧对象复制属性,并将新值添加到已更改的属性中。

好的,因此有两种解决方案可用

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

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

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

好的,因此两种解决方案都有效

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

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

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

为什么这些字段是可选的?你什么时候会有一张没有标题、价格或数量的票?为什么这些字段是可选的?你什么时候会有一张没有标题、价格或数量的票?哦!我懂了。我也设法让它适合我正在做的事情。让filteredTicketCellModels=ticketCellModels.filter({return$0.quantity!=0})这种方法会起作用,但不会过滤掉任何数量的nil(它看起来像是你在强制展开时遇到的),我不确定你想要还是不想要。哦!我懂了。我也设法让它适合我正在做的事情。让filteredTicketCellModels=ticketCellModels.filter({return$0.quantity!=0})这种方法会起作用,但不会过滤掉任何数量的nil(它看起来像是在强制展开时发生崩溃),我不确定您想要还是不想要。