嵌套中的Swift ObjectMapper循环

时间:2015-09-24 16:05:24

标签: swift

我有这个映射器:

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'

我如何在子数组中循环?

1 个答案:

答案 0 :(得分:1)

我不知道ObjectMapper是什么,但错误信息很简单。你在说:

class Article: Mappable {
    var children: Article!
}

因此,文章的children是一篇文章。那么当你说:

for child in article.children {

......编译器会阻止你;一篇文章,children就是for ... in,而不是你可以说GetProject