해시 태그라는 모델 객체가 있습니다. 여기에는 hashtagName이라는 선택적 String 변수가 포함됩니다. 내 Firebase 데이터베이스에서 데이터를 가져 와서 해시 태그를 [Hashtag]
인 fashionHashtags에 추가합니다. 문제는 insertElementAtIndexPath
함수를 사용하여 다른 categoriesArray에 추가하려는 것입니다. 나는 Hashtag의 배열이 아닌 String 배열을 원하기 때문에 이것을 할 수 없다. 내가 자동 수정하면 fashionHashtags as! [String]
으로 바뀌지 만 또 다른 오류가 발생합니다. 이렇게하면 문제를 해결할 수 있습니까? 저는 Model Object의 일을 계속 고수하고 싶습니다. 고마워. 대답은 높게 평가 될 것입니다. 코드는 다음과 같습니다.스위프트 : NSObject를 [String] 대신 Array에 삽입 할 수 없습니다.
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
})
}
}
기본적으로 categoriesArray가 있으며 해시 태그 배열이 있습니다. hashtag 객체는 해시 태그 이름을 포함합니다. 그 (것)들의 이름은 일련의 문자열로 fashionArray에 추가됩니다. 그런 다음 해당 fashionArray를 categoriesArray에 추가하려고하지만 대신 문자열의 배열이 필요합니다. – Jimmy