2017-04-02 7 views
1

해시 태그라는 모델 객체가 있습니다. 여기에는 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 

      }) 
     } 

    } 

답변

0

내 이해에 따라 몇 가지 접근법이 있습니다. 여기에 Hashtag 객체의 배열을 반복하고 hashTagName 문자열 속성을 strings의 categoriesArray에 추가하는 방법이 있습니다.

for hashTagItem in fashionHashtags { 
     if let hashTag = hashTagItem.hashtagName { 
      // Appends to categoriesArray as a string 
      categoriesArray.append(hashTag) 
     } 
    } 

또 다른 접근법은 문자열 세트를 작성한 다음 의미가있는대로 삽입하는 것입니다.

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

기본적으로 categoriesArray가 있으며 해시 태그 배열이 있습니다. hashtag 객체는 해시 태그 이름을 포함합니다. 그 (것)들의 이름은 일련의 문자열로 fashionArray에 추가됩니다. 그런 다음 해당 fashionArray를 categoriesArray에 추가하려고하지만 대신 문자열의 배열이 필요합니다. – Jimmy