Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 类型';ViewController';不符合协议';UICollectionViewDataSource';_Ios_Swift_Objc Protocol - Fatal编程技术网

Ios 类型';ViewController';不符合协议';UICollectionViewDataSource';

Ios 类型';ViewController';不符合协议';UICollectionViewDataSource';,ios,swift,objc-protocol,Ios,Swift,Objc Protocol,我正在学习使用UIPickerController操作相机的教程。但是,在实现uiCollectionViewDatsSource时,我收到一个错误,指出ViewController不符合UICollectionViewDataSource协议 class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayou

我正在学习使用
UIPickerController
操作相机的教程。但是,在实现
uiCollectionViewDatsSource
时,我收到一个错误,指出
ViewController
不符合
UICollectionViewDataSource
协议

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate 

你知道如何解决这个问题吗

将协议定义添加到自定义类是不够的。您必须提供协议所需的功能。请参阅协议的文档,您必须至少实现:

collectionView:numberOfItemsInSection:
collectionView:cellForItemAtIndexPath:

必须在
ViewController
类中实现这两种方法:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}

注意-您的函数原型应与上述函数完全匹配。(删除任何
!'
如果存在)

必须实现UICollectionViewDataSource有两个函数!(它们是协议的必需功能)

要解决此问题,只需实现以下两个功能:

必须在集合视图的ViewController类中实现以下两种方法:

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    <#code#>
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    <#code#>
}
func collectionView(collectionView:UICollectionView,numberofitemsinssection:Int)->Int{
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
}

对我来说,这是一个“!”在cellForItemAtIndexPath函数中的每个参数之后,Swift的默认行为似乎是一个红色错误,因为没有实现所需的协议方法,而在Objective-C Xcode中只给出一个黄色警告。您必须至少删除所需的方法才能消除错误。