0

在更新到 xcode 7.3 后,我正在更新我所有的 swift 语法在此过程中,我遇到了一些错误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来自另一个控制器:

在此处输入图像描述

        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)")
            }
        })

    }

更新

在此处输入图像描述

4

1 回答 1

1

首先尽可能使用 Swift 原生类型,例如NSArray对象内容的类型是未指定的。

其次,尽可能少地使用类型注释,在这种情况下

var array = Array(self.participants)

如果没有注释,您将Array免费获得一个 Swift,并且编译器知道内容的类型是PFUser. 该函数sortInPlace()在没有返回值的情况下对数组本身进行排序,您必须强制将fullName值向下转换为String

     array.sortInPlace {
        user1, user2 in

        let date1 = user1["fullName"] as! String
        let date2 = user2["fullName"] as! String
        return date1 > date2
}

并使用正确的类型Set<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!)
于 2016-03-28T08:37:33.537 回答