使用Swift 2编写的代码可以使用但不适用于Swift 3.
ShoppingManager.getShoppingItemsForPage(currentPage, pageSize: 40) { (shopItems: [ShopItemModel]?, error: Error?) -> () in
if error == nil {
...
self.shopItems.append(Array(shopItems[10..<40]))
我收到错误
value of optional type [ShopItemModel] not unwrapped
。
我尝试使用?
,但它不起作用。
这是我的ShopItemModel
class ShopItemModel: NSObject {
//RestKitMapping
var itemID: String?
var shopName: String?
答案 0 :(得分:0)
使用guard let
语句展开你的shopItem,否则你可以使用!
强制解包,但如果shopItems为零则会崩溃
guard let shopItemArray = shopItems else {
return // shopItems is nil if it goes here
}
self.shopItems.append(Array(shopItemArray[10..<40]))
答案 1 :(得分:0)
编译:
ShoppingManager.getShoppingItemsForPage(currentPage, pageSize: 40) { (shopItems: [ShopItemModel]?, error: Error?) -> () in
if error == nil {
let lastItem = self.shopItems.count
if self.currentPage == 1 {
self.shopItems.append(contentsOf: Array(shopItems![10..<40]))
} else {
self.shopItems.append(contentsOf: Array(shopItems!))
}