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

前端 未结 1 431
天涯浪人
天涯浪人 2021-01-23 18:31

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 m

相关标签:
1条回答
  • 2021-01-23 18:49

    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
    
    0 讨论(0)
提交回复
热议问题