4

I'm trying to sort my contacts array by severity

 var contacts: [Contact]? 
 if let customer = CoreDataAccess.sharedInstance.getSavedCustomer() {
 //Cast from NSSet to [Contact] 
 self.contacts = customer.contacts.allObjects as? [Contact]
 self.contacts!.sort({0ドル.severity < 1ドル.severity}) //error
 }

The compiler errors out at the marked line with the following message:

Cannot invoke 'sort' with an argument list of type '((_, _) -> _)'

I'm not sure what I'm doing wrong because this exact same thing worked in another file. If it helps to explain, the above code crashes when running on the WatchKit interface, but not when used in iOS.

EDIT: severity is an NSNumber

asked Aug 2, 2015 at 7:52
2
  • Try reproducing the problem in a dummy project. If it's possible, file a bug report at Apple Bug Reporter. Commented Aug 2, 2015 at 8:18
  • 1
    How is severity declared? What type does it have? Commented Aug 2, 2015 at 8:21

3 Answers 3

2

Try to cast the first argument explicit to NSNumber and use the compare: method (I tested it in a Playground)

var contacts: [Contact]?
if let customer = CoreDataAccess.sharedInstance.getSavedCustomer() {
 //Cast from NSSet to [Contact]
 self.contacts = customer.contacts.allObjects as? [Contact]
 self.contacts!.sort { (0ドル.severity as NSNumber).compare(1ドル.severity) == .orderedAscending }
}

It's even more efficient to declare the relationship as native Swift set Set<Contact> and the attribute as Int, this avoids to call allObjects and the type casts. And declare contacts as non-optional empty array.

var contacts = [Contact]()
if let customer = CoreDataAccess.sharedInstance.getSavedCustomer() {
 self.contacts = customer.contacts.sorted { 0ドル.severity < 1ドル.severity }
}
answered Aug 2, 2015 at 18:04
1
  • Thank you! Casting NSNumber to Int worked perfectly! Commented Aug 2, 2015 at 22:53
1

I think the problem is the property of Contact "severity" !

The type of "severity" is your custom type, and it's not implement compare operator like "<"

Since severity is NSNumber We can do this

func <(lhs: NSNumber, rhs: NSNumber) -> Bool {
 return lhs.compare(rhs) == NSComparisonResult.OrderedAscending
}
func >(lhs: NSNumber, rhs: NSNumber) -> Bool {
 return lhs.compare(rhs) == NSComparisonResult.OrderedDescending
}
func ==(lhs: NSNumber, rhs: NSNumber) -> Bool {
 return lhs.compare(rhs) == NSComparisonResult.OrderedSame
}

then the code will works fine.

answered Aug 2, 2015 at 10:30
0
0

Try this:

var contacts: [Contact]? 
if let customer = CoreDataAccess.sharedInstance.getSavedCustomer() {
 //Cast from NSSet to [Contact] 
 self.contacts = customer.contacts.allObjects as? [Contact]
 self.contacts!.sort {
 0ドル.severity < 1ドル.severity
 }
}
answered Aug 2, 2015 at 8:00
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.