Arrays Swift,Firebase:如何将模型对象的数组插入(而不是附加)到不同的数组中
我的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中,但我要么得到一个错误,说它无法转换[爱好]的值类型更改为预期的参数类型或错误,表示希望插入字符串数组。我该怎么做
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)
}