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

爱⌒轻易说出口 提交于 2019-12-02 04:45:31

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