Swift:无法将NSObject插入到Array中,因为它需要一个[String] [英] Swift: Can't insert NSObject into Array as it wants a [String] instead

查看:122
本文介绍了Swift:无法将NSObject插入到Array中,因为它需要一个[String]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Hashtag的模型对象.它仅包含一个称为hashtagName的可选String变量.我从Firebase数据库中获取数据,并将标签附加到我的fashionHashtags(即[Hashtag])上.我遇到的问题是,我想通过使用insertElementAtIndexPath函数将其附加到其他categoryArray中.我不能这样做,因为它需要一个字符串数组而不是一个Hashtag数组.当我自动更正它时,它会用fashionHashtags as! [String]替换它,但这会产生另一个错误.我该如何解决这个问题,以便我可以解决?我想坚持使用模型对象"的方式.感谢大伙们.一个答案将不胜感激.代码如下:

I have a model object called Hashtag. This simply contains a optional String variable called hashtagName. I fetch the data from my Firebase Database and append the hashtags to my fashionHashtags, which is a [Hashtag]. The issue I have is that I want to append that to my other categoriesArray by using the insertElementAtIndexPath function. I cannot do this as it wants an array of Strings and not an array of Hashtag. When I autocorrect it, it replaces it with fashionHashtags as! [String] but that creates another error. How do I fix this so it allows me to do so? I would like to stick to the Model Object way of doing things. Thank you guys. An answer would be highly appreciated. Code is below:

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

            })
        }

    }

推荐答案

根据我的理解,可能有两种不同的方法.这是循环遍历Hashtag对象数组并将hashtagName字符串属性附加到字符串CategoriesArray的方法.

Based upon my understanding, there could be a couple of different approaches. Here would be the approach of looping through the array of Hashtag objects and appending the hashtagName string property to the categoriesArray of strings.

    for hashTagItem in fashionHashtags {
        if let hashTag = hashTagItem.hashtagName {
             // Appends to categoriesArray as a string
             categoriesArray.append(hashTag)
        }
    }

另一种方法是构建一组字符串,然后将其插入.

Another approach would be to build a set of strings and then insert it as it makes sense.

    var hashTagString: [Strings] = []
    for hashTagItem in fashionHashtags {
        if let hashTag = hashTagItem.hashtagName {
             hashTagStrings.append(hashTag)
        }
    }
    // Insert or add the hash tag strings as it makes sense
    categoriesArray += hashTagStrings  // Add all at once if it makes sense

这篇关于Swift:无法将NSObject插入到Array中,因为它需要一个[String]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆