问题描述
在更新到xcode 7.3之后,我正在更新我所有的Swift语法在此过程中,我遇到了有关ambiguous use of subscript swift
的一些错误,并且我认为该错误也导致了信号故障.
I am in the process of updating all my swift syntax after updating to xcode 7.3In the process, I got some errors about ambiguous use of subscript swift
and I believe that this error is also causing the Signal Fault.
有问题的代码:
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
的声明来自另一个控制器:
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)")
}
})
}
更新
推荐答案
首先尽可能多地使用Swift本机类型,例如,未指定NSArray
对象内容的类型.
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)
没有注释,您可以免费获得Swift Array
,编译器知道内容的类型为PFUser
.函数sortInPlace()
对数组本身进行排序,而没有返回值,您必须将fullName
值强制向下转换为String
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
}
,并在完成处理程序中使用正确的类型Set<PFUser>
,而不是Set<NSObject>
,可能使用的是users: [PFUser]?
,而不是users: NSArray?
and use the proper type Set<PFUser>
rather then Set<NSObject>
and probably users: [PFUser]?
in the completion handler rather then users: NSArray?
queryForAllUsersWithCompletion
方法的开头应该看起来像
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!)
这篇关于由于信号故障导致命令失败-Swift 2.2 Xcode 7.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!