Swift: no se puede insertar NSObject en Array ya que en su lugar quiere una [Cadena]
Tengo un objeto modelo llamado Hashtag. Esto simplemente contiene una variable de cadena opcional llamada hashtagName. Recojo los datos de mi base de datos de Firebase y agrego los hashtags a mis fashionHashtags, que es un[Hashtag]
. El problema que tengo es que quiero agregar eso a mis otras categorías Array usando elinsertElementAtIndexPath
función. No puedo hacer esto, ya que quiere una matriz de cadenas y no una matriz de Hashtag. Cuando lo autocorrijo, lo reemplaza confashionHashtags as! [String]
Pero eso crea otro error. ¿Cómo soluciono esto para que me permita hacerlo? Me gustaría apegarme a la forma del Objeto Modelo de hacer las cosas. Gracias chicos. Una respuesta sería muy apreciada. El código está abajo:
class Hashtag: NSObject {
vvar hashtagName: String?
}
private let reuseIdentifier = "Cell"
class HashtagView: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var catagoriesArray: [String] = ["FASHION", "FOOD", "HOBBIES", "MUSIC"]
var fashionHashtags = [Hashtag]()
var foodHashtags = [Hashtag]()
var hobbiesHashtags = [Hashtag]()
var musicHashtags = [Hashtag]()
var hashtagsArray: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
self.hashtagsArray.removeAll()
self.navigationController?.navigationBar.tintColor = .white
navigationItem.title = "Hashtag"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Finished", style: .plain, target: self, action: #selector(finishSelectingHashtags))
self.collectionView?.backgroundColor = .white
self.collectionView?.register(HashtagCell.self, forCellWithReuseIdentifier: reuseIdentifier)
self.collectionView?.contentInset = UIEdgeInsetsMake(10, 0, 0, 0)
handleFetchFashionHashtags()
}
func insertElementAtIndexPath(element: [String], index: Int) {
catagoriesArray.insert(contentsOf: element, at: index)
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 0 {
insertElementAtIndexPath(element: fashionHashtags, index: indexPath.item + 1)
self.collectionView?.performBatchUpdates(
{
self.collectionView?.reloadSections(NSIndexSet(index: 0) as IndexSet)
}, completion: { (finished:Bool) -> Void in
})
}
}