Swift: Can't insert NSObject into Array as it wants a [String] instead

别来无恙 提交于 2019-12-02 11:58:41

问题


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

            })
        }

    }

回答1:


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


来源:https://stackoverflow.com/questions/43172959/swift-cant-insert-nsobject-into-array-as-it-wants-a-string-instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!