2015-09-25 83 views

回答

3

您目前使用的陣列上的下標,並然後調用返回的Stringappend。相反,只需撥打append陣列上:

result.append(newItems.entries![i].id) 
-1

您不能使用下標語法將新項目追加到 數組的末尾。

但顯然你可以使用append

您需要在數組上調用它,而不是像現在這樣在數組的最後一個元素上調用它。

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

+0

我想如何爲我的數組添加值? –

+0

通過使用'append'。只需閱讀文檔。 – oarfish

+0

爲什麼這是低調的? – oarfish

1

從截圖來看,它看起來像你想創建從另一個從預定義的指數開始的數組。

另一種方式來做到這一點沒有一個循環是對數組的一部分使用map

func getFollowingArticles(index: Int) -> [String] { 
    let count = newsItems.entries.count 
    if index <= count { 
     return newsItems.entries[index..<count].map({ $0.id }) 
    } 
    return [] 
} 

我還添加了最小誤差在我的例子檢查。