在swift3.0中连接:二元运算符' +'不能应用于' []'类型的操作数和' []?'

时间:2016-11-09 04:06:19

标签: swift

在Swift3.0中获取错误:

  

二元运算符' +'不能应用于' [FutureTrainee]'和' [FutureTrainee]?'

代码:

height: 200px

1 个答案:

答案 0 :(得分:0)

正如错误所示,您无法使用+添加[FutureTrainee](又名Array<FutureTrainee>)和[FutureTrainee]?(又名Optional<Array<FutureTrainee>>

newTrainees的类型为[FutureTrainee]?,因为您使用data下标"data"的可选链接。这段代码:

let newTrainees = data?["data"].arrayValue.map({ (json) in
                        return FutureTrainee(data: json)
                    }) 

就像:

var newTrainees: [FutureTrainee]?
if let data = data {
    newTrainees = data["data"].arrayValue.map({ (json) in
                      return FutureTrainee(data: json)
                  })
else {
    newTrainees = nil
}

如果newTrainees不为零,您需要确保只附加var trainees = self.futureTraineeCollection if let newTrainees = data?["data"].arrayValue.map{ FutureTrainee(data: $0) } { // If that expression is not nil, the result is bound to "newTrainees" trainees += newTrainees } ,如下所示:

Name: r_DTS, dtype: category
Categories (4, object): [Bottom < 2 < Top < Missing]