简体   繁体   English

如何通过NSNumber属性将自定义数据对象排序为空数组-Swift2 iOS

[英]How to sort custom data objects into an empty array by NSNumber property -Swift2 iOS

I have a custom data object with a NSNumber property. 我有一个带有NSNumber属性的自定义数据对象。 How would I sort the messages from one array into another array using the NSNumber property? 如何使用NSNumber属性将消息从一个数组排序到另一个数组?

I tried this but I get an error 我尝试了这个但是出现错误

Binary operator '<' cannot be applied to two 'NSNumber?' operands

emptyArray = messagesArray.sort{$0.time < $1.time}

Ascending or descending doesn't matter 上升或下降无关紧要

class Message{
var toMessage: String?
var time: NSNumber?
}

var messagesArray[Message]()

let message1 = Message()
message1.toMessage = "Bonjour"
message1.time = 123

let message2 = Message()
message1.toMessage = "Hola"
message1.time = 456

let message3 = Message()
message1.toMessage = "Hello"
message1.time = 789

messagesArray.append(message1)
messagesArray.append(message2)
messagesArray.append(message3)

//I need to get the elements from messagesArray into emptyArray sorting by NSNumber time property
var emptyArray = [Message]()

我需要使用intValue

emptyArray = messagesArray.sort{$0.time.intValue < $1.time.intValue}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift如何使用属性按给定顺序对自定义对象数组进行排序 - Swift how to sort an array of custom objects in a given order with property Swift iOS-如何基于相似的属性将单个对象的数组分类为单独的数组 - Swift iOS -How to sort array of individual objects into separated arrays based on similar property iOS如何对包含NSarray的NSNray的自定义对象的NSMutableArray进行排序? - iOS how to sort NSMutableArray of custom object contained NSarray of NSNumber? 如何使用swift在单个数组中对具有date属性的多个类对象进行排序? - How to sort multiple class objects with date property in single array with swift? 如何在swift2中将分钟添加到自定义时间 - How to add minutes to custom time in swift2 如何在此属性上对自定义对象数组进行排序? - How do I sort an array of custom objects on this property? 按日期范围 Swift 对自定义对象数组进行排序 - Sort array of Custom Objects by date range Swift Swift2 iOS9-如何在我的代码中获取自定义图像而不是默认注释红色图钉 - Swift2 iOS9 - how to get custom image instead of default annotation red pin in my code 如何使用NSNumber对象的属性创建类别 - How to create a category with a property for NSNumber objects 无法在iOS9的swift2中获取航向数据(用于罗盘应用) - Cannot get heading data (for compass app) in swift2 on ios9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM