ios 无法为 [AnyObject] 的值添加下标?具有 Int 类型的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29874414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot subscript a value of [AnyObject]? with an index of type Int
提问by user1406716
This is in a class extending PFQueryTableViewControllerand I am getting the following error. The rows will be PFUser
only.
Why am I not able to cast it? Is there a way around this?
这是在扩展PFQueryTableViewController的类中,我收到以下错误。行将是PFUser
唯一的。
为什么我无法投射它?有没有解决的办法?
The error is:
错误是:
Cannot subscript a value of [AnyObject]? with an index of type Int
...for this line:
...对于这一行:
var user2 = self.objects[indexPath.row] as! PFUser
回答by Marcus Rossel
The problem isn't the cast, but the fact that self.objects
seems to be an optional array: [AnyObject]?
.
Therefore, if you want to access one of its values via a subscript, you have to unwrap the array first:
问题不在于中投,但事实self.objects
似乎是一个可选的阵列:[AnyObject]?
。
因此,如果要通过下标访问其值之一,则必须先解开数组:
var user2: PFUser
if let userObject = self.objects?[indexPath.row] {
user2 = userObject as! PFUser
} else {
// Handle the case of `self.objects` being `nil`.
}
The expression self.objects?[indexPath.row]
uses optional chainingto first unwrap self.objects
, and then call its subscript.
该表达式self.objects?[indexPath.row]
使用可选链首先解包self.objects
,然后调用其下标。
As of Swift 2, you could also use the guard statement:
从 Swift 2 开始,您还可以使用guard 语句:
var user2: PFUser
guard let userObject = self.objects?[indexPath.row] else {
// Handle the case of `self.objects` being `nil` and exit the current scope.
}
user2 = userObject as! PFUser
回答by rudenudedude
I ran into the same issue and resolved it like this:
我遇到了同样的问题并像这样解决了它:
let scope : String = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex] as! String
For your case, you might do:
对于您的情况,您可能会这样做:
var user2 : PFUser = self.objects![indexPath.row] as! PFUser
回答by Amit89
My workaround would be..
我的解决方法是..
- If you are certain that the tableview will contain only users try to typecast the objects Array of AnyObject to Array of PFUser. then use it.
- 如果您确定 tableview 将仅包含用户,请尝试将对象数组的 AnyObject 类型转换为 PFUser 的数组。然后使用它。
回答by ojassethi
Just add an ! (exclamation mark) after objects, like so:
只需添加一个!(感叹号)在对象之后,像这样:
var user2 = self.objects![indexPath.row] as! PFUser
That fixed it for me :)
那为我修好了:)
回答by dinesharjani
I had a similar issue with the following line:
我对以下行有类似的问题:
array![row]
I could not understand where the issue came from; if I replaced row
with a number like 1
the code compiled and ran without any issues.
我不明白问题从何而来;如果我row
用一个数字代替1
编译和运行没有任何问题的代码。
Then I had the happy thought of changing it to this:
然后我高兴地想到把它改成这样:
array![Int(row)]
And it worked. For the life of me, I don't understand why giving an array an index of -1
is theoretically legal, but there you go. It makes sense to me for subscripts to be unsigned, but maybe it's just me; I'll have to ask Chrisabout it.
它奏效了。对于我的一生,我不明白为什么给数组一个索引在-1
理论上是合法的,但是你去了。下标未签名对我来说是有意义的,但也许只是我;我得问问克里斯。