我试图了解Swift中协议和委托的用法,以及它们在c#for iOS中的等效性。
微软对此有一份不错的文档
here
但对我来说,它缺乏c代码的swift或objectiveC对应物,无法让我理解该做什么。
我正在移植一个
existing Swift gist
c#Xamarin iOS。
我有这个协议:
protocol GridLayoutDelegate: class {
func scaleForItem(inCollectionView collectionView: UICollectionView, withLayout layout: UICollectionViewLayout, atIndexPath indexPath: IndexPath) -> UInt
func itemFlexibleDimension(inCollectionView collectionView: UICollectionView, withLayout layout: UICollectionViewLayout, fixedDimension: CGFloat) -> CGFloat
func headerFlexibleDimension(inCollectionView collectionView: UICollectionView, withLayout layout: UICollectionViewLayout, fixedDimension: CGFloat) -> CGFloat
}
我已经转换为:
public interface IGridLayoutDelegate
{
uint ScaleForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath);
nfloat ItemFlexibleDimension(UICollectionView collectionView, UICollectionViewLayout layout, nfloat fixedDimension);
nfloat HeaderFlexibleDimension(UICollectionView collectionView, UICollectionViewLayout layout, nfloat fixedDimension);
}
到现在为止,一直都还不错。
在代码的后面,我有一个(目前)奇怪的声明:
weak var delegate: GridLayoutDelegate?
我担心的是,它在swift代码的后面永远不会被分配。
现在我把c#类声明为:
public class GridLayout : UICollectionViewLayout, IGridLayoutDelegate
{
...
}
c#中的对应代码是什么?