由于信号故障,命令失败 – Swift 2.2 Xcode 7.3
我正在更新所有我的swift更新到xcode 7.3的过程中,在过程中,我得到了一些错误ambiguous use of subscript swift
错误,我相信这个错误也导致信号故障。
有问题的代码:
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 }
编辑
participants
声明来自另一位pipe制员:
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)") } }) }
更新
首先尽可能使用Swift本地types,例如未指定NSArray
对象内容的types。
其次,尽可能less使用types注释,在这种情况下
var array = Array(self.participants)
如果没有注释,你可以免费得到一个Swift Array
,并且编译器知道PFUser
的内容types。 函数sortInPlace()
对数组本身进行sorting而不返回值,并且必须强制将fullName
值向下转换为String
array.sortInPlace { user1, user2 in let date1 = user1["fullName"] as! String let date2 = user2["fullName"] as! String return date1 > date2 }
并使用正确的typesSet<PFUser>
而不是Set<NSObject>
,可能是users: [PFUser]?
在完成处理程序,而不是users: NSArray?
编辑: queryForAllUsersWithCompletion
方法的开始应该看起来像
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!)