2016-08-15 10 views
2

私は以下をキャストしようとしていますが、以下のエラーが発生しています。UICollectionViewLayoutサブクラスをサブクラスにキャスト

私は正常に動作し、次のObjective Cのバージョンを、達成しようとしています:

LSSwipeToDeleteCollectionViewLayoutUICollectionViewFlowLayoutのサブクラスであり、客観C.

のObjective Cで書かれています:

LSSwipeToDeleteCollectionViewLayout *layout = (LSSwipeToDeleteCollectionViewLayout *)self.collectionView.collectionViewLayout; 

スウィフト:

let layout : LSSwipeToDeleteCollectionViewLayout = (self.collectionView.collectionViewLayout as AnyObject) as! LSSwipeToDeleteCollectionViewLayout 
     layout.swipeToDeleteDelegate = self 

'UICollectionViewFlowLayout'の値を (0x111edda00)から 'LSSwipeToDeleteCollectionViewLayout'(0x11040e3c0)にキャストできませんでした。

答えて

0

は通常、あなただけだろう:

let layout = self.collectionView.collectionViewLayout as! LSSwipeToDeleteCollectionViewLayout

はあなたがコレクションビューを初期化するときに最初にLSSwipeToDeleteCollectionViewLayoutのインスタンスを作成し、UICollectionView.init(frame:collectionViewLayout:)方法でcollectionViewLayout引数として設定しますか?

+0

こんにちは、あなたの答えに2番目の部分はどういう意味ですか? –

+0

コレクションビューを作成するときは、レイアウトを担当するクラスを設定する必要があります。そのクラスは 'LSSwipeToDeleteCollectionViewLayout'でなければなりません。または、デフォルトの' UICollectionViewFlowLayout'クラスを取得する必要があります。あなたは、コレクションビューをどのように作成しているかに応じて、あなたのペンで、または 'UICollectionView'初期化子への引数として直接レイアウトクラスを設定します。 – par

関連する問題