如何在Swift中将自定义类对象附加到数组? 下面是我的代码,但它显示错误。
错误:
"无法指定'()'的值输入[PhotoVC]"
代码:
var photoVCs = [PhotoVC]()
for index in 0 ..< photos.count {
if let vc = getPhotoController(index) {
photoVCs = photoVCs.append(vc)
}
}
func getPhotoController(index: Int) -> PhotoVC? {
if index < 0 || index == NSNotFound {
return nil
}
else if index == photos.count {
return nil
}
let PhotoVCID = "PhotoVCID"
if let storyboard = storyboard,
pageVC = storyboard.instantiateViewControllerWithIdentifier(PhotoVCID) as? PhotoVC {
pageVC.photoName = photos[index]
pageVC.photoIndex = index
return pageVC
}
return nil
}
我应该能够做到,但问题是什么?
答案 0 :(得分:6)
append
不会返回任何内容。删除作业:
photoVCs = photoVCs.append(vc) // wrong
photoVCs.append(vc) // ok