ios 类型没有下标成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36138139/
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
Type Has No Subscript Members?
提问by Mr_CryptoPrime
I get the error "Type 'Ship' has no subscript members when I try to do:
当我尝试执行以下操作时,出现错误“类型‘船舶’没有下标成员:
var coor = ship[index]
I tried to do
我试着做
var coor = ship?[index] as? Coordinate
But I get this error: "Cannot use optional chaining on non-optional value of type 'Ship'"
但我收到此错误:“不能对类型为‘Ship’的非可选值使用可选链”
Here's my Ship
class:
这是我的Ship
课:
import Foundation
class Ship: NSObject, NSCoding {
var shipCoors: [Coordinate]?
var count: Int {
var count = 0
for _ in shipCoors! {
count++
}
return count
}
init(shipCoors: [Coordinate]) {
self.shipCoors = shipCoors
}
required init(coder decoder: NSCoder) {
self.shipCoors = decoder.decodeObjectForKey("shipCoors") as? [Coordinate]
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(shipCoors, forKey: "shipCoors")
}
}
The Coordinate
class is also of type NSObject, NSCoding
, etc...
The objects seem to be in the array when I load them (from NSUserDefaults
)? How do I get them out?!
该Coordinate
班也是类型的NSObject, NSCoding
,等等的对象似乎是在阵列中,当我加载它们(从NSUserDefaults
)?我怎么把它们弄出来?!
回答by JAL
Add a subscript to your Ship
object to return an optional Coordinate
:
向您的Ship
对象添加下标以返回一个可选的Coordinate
:
subscript(index: Int) -> Coordinate? {
guard let coordinate = shipCoors?[index] else {
return nil
}
return coordinate
}
shipCoors
is declared as [Coordinate]?
(an optional array), so there's a risk a Ship
won't have an array in shipCoors
. In this case I return nil
, but you can return whatever you want instead.
shipCoors
被声明为[Coordinate]?
(一个可选数组),因此存在一个风险 a 中Ship
没有数组shipCoors
。在这种情况下,我 return nil
,但您可以返回任何您想要的东西。
回答by Naishta
Had similar issue in Swift 3
在 Swift 3 中有类似的问题
Type '() -> [myObject]' has no subscript members
In my case, It was a simple case of not adding the brackets to the function-call "()". school boy error.
就我而言,这是一个简单的情况,即不将括号添加到函数调用“()”中。小学生错误。
i.e. the below code was the culprit
即下面的代码是罪魁祸首
dataModel.myFunction
Solved with dataModel.myFunction()
解决了 dataModel.myFunction()
回答by Daniel Hall
You have to explicitly add subscripting support to the class in order to use the subscript syntax, e.g. ship[index]
.
您必须显式地向类添加下标支持才能使用下标语法,例如ship[index]
.
Here are the docs that cover subscripting and how to add subscripting to your class:
以下是涵盖下标以及如何向类中添加下标的文档:
回答by Honey
For my code:
对于我的代码:
func takeN(_ numbers: Int...) -> [Int]{
var intArray : [Int] = []
for n in numbers{
intArray.append[n]
}
return intArray
}
I was getting the following error:
我收到以下错误:
error: type '(Int) -> ()' has no subscript members intArray.append[n]
错误:类型 '(Int) -> ()' 没有下标成员 intArray.append[n]
It means the append
function doesn't use subscripts or []
!
这意味着该append
函数不使用下标或[]
!
changing the line of
改变线
intArray.append[n]
to:
到:
intArray.append(n)
would resolve the entire issue!
将解决整个问题!