Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
Arrays Swift,Firebase:如何将模型对象的数组插入(而不是附加)到不同的数组中_Arrays_Firebase_Swift3_Firebase Realtime Database_Uicollectionview - Fatal编程技术网

Arrays Swift,Firebase:如何将模型对象的数组插入(而不是附加)到不同的数组中

Arrays Swift,Firebase:如何将模型对象的数组插入(而不是附加)到不同的数组中,arrays,firebase,swift3,firebase-realtime-database,uicollectionview,Arrays,Firebase,Swift3,Firebase Realtime Database,Uicollectionview,我的Firebase数据库中有一系列的类别和一系列的体育爱好。我在视图加载时获取它们,并将它们分别附加到categoriesArray=[cabiods]和sportsaray=[cabiods]中 我的问题是,在我的didSelectItemAtIndexPath方法中,我想插入,而不是将所有的sportsArray追加到运动的indexPath处的categoriesArray中,但我要么得到一个错误,说它无法转换[爱好]的值类型更改为预期的参数类型或错误,表示希望插入字符串数组。我该怎么做

我的Firebase数据库中有一系列的类别和一系列的体育爱好。我在视图加载时获取它们,并将它们分别附加到
categoriesArray=[cabiods]
sportsaray=[cabiods]

我的问题是,在我的didSelectItemAtIndexPath方法中,我想插入,而不是将所有的sportsArray追加到运动的indexPath处的categoriesArray中,但我要么得到一个错误,说它无法转换[爱好]的值类型更改为预期的参数类型或错误,表示希望插入字符串数组。我该怎么做?谢谢你们。。。意义重大

我的数据库:

import UIKit
import Firebase

class Hobbies: NSObject {

    var hobbieName: String?
}

private let reuseIdentifier = "Cell"

class CategoriesView: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var categoriesArray = [Hobbies]()
    var sportsArray = [Hobbies]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.navigationBar.tintColor = .white
        navigationItem.title = "Hashtag"

        self.collectionView?.backgroundColor = .white
        self.collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView?.contentInset = UIEdgeInsetsMake(10, 0, 0, 0)

        handleFetchCategories()
        handleFetchSports()
    }

    func handleFetchCategories() {

        let ref = FIRDatabase.database().reference().child("categories")

        ref.observe(.childAdded, with: { (snapshot) in

            if let hobbieNames = snapshot.value as? String {

                let hobbie = Hobbies()

                hobbie.hobbieName = hobbieNames

                self.categoriesArray.append(hobbie)
            }

            DispatchQueue.main.async {
                self.collectionView?.reloadData()
            }

        }, withCancel: nil)
    }

    func handleFetchSports() {

        let sportsRef = FIRDatabase.database().reference().child("sports")

        sportsRef.observe(.childAdded, with: { (snapshot) in

            if let sportsNames = snapshot.value as? String {

                let hobbie = Hobbies()

                hobbie.hobbieName = sportsNames

                self.sportsArray.append(hobbie)
            }

        }, withCancel: nil)
    }

    func insertElementAtIndexPath(element: [String], index: Int) {
        categoriesArray.insert(contentsOf: element, at: index)
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        if indexPath.item == 1 {
            insertElementAtIndexPath(element: sportsArray, index: indexPath.item + 1)

            self.collectionView?.performBatchUpdates(
                {
                    self.collectionView?.reloadSections(NSIndexSet(index: 0) as IndexSet)
            }, completion: { (finished:Bool) -> Void in

            })
        }

    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return categoriesArray.count
    }

问题 您尝试在此处将
字符串数组插入到
爱好数组中

func insertElementAtIndexPath(element: [String], index: Int) {
    categoriesArray.insert(contentsOf: element, at: index)
}
解决方案 只需将
insertElementAtIndexPath(元素:index:)
参数中的数组类型更改为
嗜好

func insertElementAtIndexPath(element: [Hobbies], index: Int) {
    categoriesArray.insert(contentsOf: element, at: index)
}