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

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

问题描述

我有一个名为 Hashtag 的模型对象.这仅包含一个名为 hashtagName 的可选字符串变量.我从我的 Firebase 数据库中获取数据并将主题标签附加到我的 fashionHashtags,它是一个 [Hashtag].我遇到的问题是我想通过使用 insertElementAtIndexPath 函数将其附加到我的其他 categoriesArray 中.我不能这样做,因为它需要一个字符串数组而不是一个标签数组.当我自动更正它时,它会将其替换为 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天全站免登陆