问题
I am in the process of updating all my swift syntax after updating to xcode 7.3
In the process, I got some errors about ambiguous use of subscript swift
and I believe that this error is also causing the Signal Fault.
The code in question:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var arry:NSArray = Array(self.participants)
arry = arry.sort {
item1, item2 in
// ambiguous use of subscript swift error for both these lines
let date1 = item1["fullName"] as String
let date2 = item2["fullName"] as String
return date1 > date2
}
Edit
Declaration of participants
comes from another controller here:
func gotoMembers(){
let set:NSSet = self.conversation.participants
let arr = set.allObjects //Swift Array
UserManager.sharedManager.queryForAllUsersWithCompletion(arr as! [String], completion:{ (users: NSArray?, error: NSError?) in
if error == nil {
//participants declared here and passed into the participant controller
let participants = NSSet(array: users as! [PFUser]) as Set<NSObject>
let controller = ParticipantTableViewController(participants: participants, sortType: ATLParticipantPickerSortType.FirstName)
controller.delegate = self
self.navigationController?.pushViewController(controller, animated:true);
} else {
appDelegate.log.error("Error querying for All Users: \(error)")
}
})
}
Update
回答1:
First of all use Swift native types a much as possible, for example the type of the contents of an NSArray
object is unspecified.
Second of all use type annotations as few as possible, in the case
var array = Array(self.participants)
without an annotation you get a Swift Array
for free and the compiler knows the type of the contents which is PFUser
. The function sortInPlace()
sorts the array itself without a return value and you have to forced downcast the fullName
values to String
array.sortInPlace {
user1, user2 in
let date1 = user1["fullName"] as! String
let date2 = user2["fullName"] as! String
return date1 > date2
}
and use the proper type Set<PFUser>
rather then Set<NSObject>
and probably users: [PFUser]?
in the completion handler rather then users: NSArray?
Edit: The beginning of the queryForAllUsersWithCompletion
method is supposed to look like
UserManager.sharedManager.queryForAllUsersWithCompletion(arr as! [String], completion:{ (users: [PFUser]?, error: NSError?) in
if error == nil {
//participants declared here and passed into the participant controller
let participants = Set<PFUser>(array: users!)
来源:https://stackoverflow.com/questions/36258298/command-failed-due-to-signal-fault-swift-2-2-xcode-7-3