2016-11-20 38 views
3

我正在將我的應用程序翻譯成Swift 3.我偶然發現了一個使用乾淨的方式爲UICollectionView設置數據源和委託的問題在一個UITableViewCell裏面,描述爲hereSwift 3泛型:在UITableViewCell中設置UICollectionView的UICollectionView的UICollectionViewDatasource&Delegate問題

的代碼如下:

func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>> 
(_ dataSourceDelegate: D, forRow row: Int) { 

collectionView.delegate = dataSourceDelegate 
collectionView.dataSource = dataSourceDelegate 
collectionView.tag = row 
collectionView.reloadData()} 

它拋出一個警告,指出:

'protocol<...>' composition syntax is deprecated; join the protocols using '&'

當我接受建議的解決方案,它改變了D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>D: (UICollectionViewDatasource & UICollectionViewDelegate)通話,而是拋出一個錯誤:

Expected a type name or protocol composition restricting 'D'

我會很ob如果有人比我更瞭解Swift 3泛型可以提出解決方案。

回答

6

不需要使用protocol<>,因爲編譯器已經知道了。剛剛加入該協議是這樣的:D: UITableViewDelegate & UITableViewDataSource

+2

不知道爲什麼Xcode的修復建議拋出括號,只需刪除它們固定的問題。非常感謝! – michalronin

1

setCollectionViewDataSourceDelegateswift3

extension PollTableViewCell { 

     func setCollectionViewDataSourceDelegate<D: UICollectionViewDataSource & UICollectionViewDelegate>(_ dataSourceDelegate: D, forRow row: Int) { 

     theCollectionView.delegate = dataSourceDelegate 
     theCollectionView.dataSource = dataSourceDelegate 
     theCollectionView.tag = row 
     theCollectionView.setContentOffset(theCollectionView.contentOffset, animated:false) // Stops collection view if it was scrolling. 
     theCollectionView.reloadData() 
    } 

    var collectionViewOffset: CGFloat { 
     set { 
      theCollectionView.contentOffset.x = newValue 
     } 

     get { 
      return theCollectionView.contentOffset.x 
     } 
    } 
} 
相關問題