嵌套

2015-09-24 60 views
0

斯威夫特ObjectMapper循環我有這樣的映射:嵌套

import Foundation 
import ObjectMapper 

class Article: Mappable { 
    var id: Int! 
    var name: String! 
    var image: String! 
    var children: Article! 

    required init?(_ map: Map) { 
     mapping(map) 
    } 

    func mapping(map: Map) { 
     id <- map["id"] 
     name <- map["name"] 
     image <- map["image"] 
     children <- map["children"] 
    } 
} 

,然後我需要循環,並通過ID找到文章:

func getArticleName(aid) { 
    for article in articleList { 
     if aid == article.id { 
      return article.name 
     } 
     for child in article.children { 
      if aid == child.id { 
       return child.name 
      } 
     } 
    } 
    return "" 
} 

articleListArticle可映射類的數組。

現在,當我在環兒,我收到此錯誤: Value of type 'Article' has no member 'Generator'

我怎麼能循環兒童陣列?

+0

這是什麼問題?你不明白那個錯誤信息嗎? – matt

+0

是的,我如何循環兒童數組? –

+1

這不是一個數組。這就是錯誤信息告訴你的。 – matt

回答

1

我不知道ObjectMapper是什麼,但錯誤信息很簡單。你在說:

class Article: Mappable { 
    var children: Article! 
} 

因此,一篇文章的children是一篇文章。那麼當你說:

for child in article.children { 

......編譯器阻止你;一篇文章,這是children是什麼,不是你可以說的for ... in

+0

我的不好,忘了''''''''',謝謝.. –