2016-03-15 31 views
0

我正在嘗試使用分頁(顯示單元格作爲預覽)構建集合視圖,並找到this教程。我收到Xcode的錯誤,並猜測這一定是由於Xcode的更新引起的(因爲本教程似乎適用於很多人)。喜歡就如何解決的提示是:Downcast from'[UICollectionViewLayoutAttributes]?' '[UICollectionViewLayoutAttributes]'只展開可選項

Downcast from '[UICollectionViewLayoutAttributes]?' to '[UICollectionViewLayoutAttributes]' only unwraps optionals; did you mean to use '!'?

+0

你可以發佈代碼嗎? – tktsubota

+0

[Here](https://github.com/moopoints/THX-Collection-View-Demo/blob/master/SecondCollectionViewFlowLayout.swift)就是例子! – moopoints

回答

0

你可能做這樣的事情(someOptionalAttributes存在[UICollectionViewLayoutAttributes]?類型):

someOptionalAttributes as! [UICollectionViewLayoutAttributes] 

然而,由於someOptionalAttributes只是[UICollectionViewLayoutAttributes]可選版本,你不需要強制降級,你只需要解開它:

someOptionalAttributes! 

強迫降級只是必需的如果投射到一個全新的類型(通常是子類)。

所以,是的,你的意思是在錯誤中使用!。如果你想要安全,你可以使用許多不同的可選解包技術。

+0

謝謝!我嘗試修改行「如果let attributesForVisibleCells = self.layoutAttributesForElementsInRect(cvBounds)!」但後來我得到這個錯誤:條件綁定的初始化必須有可選類型,而不是'[UICollectionViewLayoutAttributes]' – moopoints

+0

@moopoints啊,因爲當你使用'if let'時,你不需要強制向下轉換。只要將它改爲'if let attributesForVisibleCells = self.layoutAttributesForElementsInRect(cvBounds)' - 沒有'!'。 – tktsubota

+0

工作!謝謝,特洛伊! – moopoints