Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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 如何在同一脚本中检索扩展类外部的数据数组_Ios_Swift_Xcode_Swift5 - Fatal编程技术网

Ios 如何在同一脚本中检索扩展类外部的数据数组

Ios 如何在同一脚本中检索扩展类外部的数据数组,ios,swift,xcode,swift5,Ios,Swift,Xcode,Swift5,我的应用程序有一个自定义的滚动视图,我的主委托类中有一个数据数组。我还有一个单独的扩展名(在同一个文件中)来处理数据,但我似乎无法访问数据,因为有两个错误使用未解析的标识符数据: class HomeViewController: UIViewController { fileprivate let data = [ CustomData(title: "Test", image: #imageLiteral(resourceName: "splash_icon"),

我的应用程序有一个自定义的滚动视图,我的主委托类中有一个数据数组。我还有一个单独的扩展名(在同一个文件中)来处理数据,但我似乎无法访问数据,因为有两个错误
使用未解析的标识符数据

class HomeViewController: UIViewController {

    fileprivate let data = [
        CustomData(title: "Test", image: #imageLiteral(resourceName: "splash_icon"), url: "clipifyapp.com"),
        CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: "clipifyapp.com"),
        CustomData(title: "Test2", image: #imageLiteral(resourceName: "notificationIcon"), url: "clipifyapp.com")
    ]

}

extension UIViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width/2.5, height: collectionView.frame.width/2)
    }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count // ERROR ONE
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
        cell.data = self.data[indexPath.row] // ERROR TWO
        return cell
    }

}

我是否遗漏了一些明显的内容?

您正在扩展
UIViewController
,但您不是想扩展
HomeViewController


如果扩展
UIViewController
,则不会扩展实际包含
数据的子类。此外,您将扩展整个应用程序中的每一个UIViewController,这可能不是您想要对UICollectionViewDataSource执行的操作。

数据
仅在
HomeViewController
中可用,而不在基类
UIViewController

替换

extension UIViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {


数据
UIViewController
的子类中定义,因此您无法从超类或超类的扩展访问它
extension HomeViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {