2

我正在玩弄/学习 Swift 和 JSON atm,并且在正确转换我的响应时遇到问题。

所以,我想要实现的是通过 Alamofire 请求显示“帖子”列表。我在显示“第一级”json 项目(如 post-ID 或“消息”)时没有问题,但是当涉及到作者数组时,我有点迷失了。

json 响应以没有名称的数组形式出现,因此第一个 [

[
-{
__v: 1,
_id: "54428691a728c80424166ffb",
createDate: "2014-10-18T17:26:15.317Z",
message: "shshshshshshshhshshs",
-author: [
-{
_id: "54428691a728c80424166ffa",
userId: "543270679de5893d1acea11e",
userName: "foo"
}
]
} 

这是我对应的VC:

    Alamofire.request(.GET, "\(CurrentConfiguration.serverURL)/api/posts/\(CurrentConfiguration.currentUser.id)/newsfeed/\(CurrentConfiguration.currentMode)",encoding:.JSON)
            .validate()
            .responseJSON {(request, response, jsonData, error) in

                let JSON = jsonData as? NSArray
                self.loadPosts(JSON!)
        }

        tableView.delegate = self

        tableView.dataSource = self

    }

    func loadPosts(posts:NSArray) {
        for post in posts {
            let id = post["_id"]! as NSString!
            let message = post["message"]! as NSString!

            var authorArray = post["author"]! as? [Author]!
            println(authorArray)

            var author:Author = Author()
            author.userName = "TEST ME"

            var postObj:Post = Post()
            postObj.id = id
            postObj.message = message
            postObj.author = author
            uppDatesCollection.append(postObj)
        }
        println(self.uppDatesCollection.count)
        dispatch_async(dispatch_get_main_queue()) {
            self.tableView.reloadData()
        }
    }

我的帖子模型

class Post {
    var id:String!
    var message:String!
    var createDate: NSDate!

    var author:Array<Author>!

    init () {

    }
} 

和作者

class Author {
        var id:String?
        var userId:String?
        var userName:String?

        init () {
    }

这里最好的方法是什么?您是否应该将返回的数组重新转换为字典,然后通过 .valueforkey 访问它?您是否以某种方式遍历数组以取出这些东西?

显然你不能说 author.name = authorArray[3] as String

4

1 回答 1

2

假设您有Post这样的课程:

class Post : Printable {
    var identifier:String!
    var message:String!
    var createDate: NSDate!
    var authors:[Author]!

    var description: String { return "<Post; identifier = \(identifier); message = \(message); createDate = \(createDate); authors = \(authors)" }
}

您的 JSON 中只有一个作者,但鉴于它似乎是 JSON 中的一个数组,我假设它也应该是对象模型中的一个数组,如上所示。该类Author可能被定义为:

class Author : Printable {
    var identifier:String!
    var userId:String!
    var userName:String!

    var description: String { return "<Author; identifier = \(identifier); userId = \(userId); userName = \(userName)>" }
}

我不知道为什么你让一些选项隐式解包(用 定义!)而另一些不(用 定义?),所以我只是让它们都隐式解包。根据您的业务规则进行调整。

另外,假设您的 JSON 看起来像这样(我不太确定-您的问题中的 JSON 是什么,所以我清理了它并添加了第二位作者):

[
    {
        "__v": 1,
        "_id": "54428691a728c80424166ffb",
        "createDate": "2014-10-18T17:26:15.317Z",
        "message": "shshshshshshshhshshs",
        "author": [
            {
                "_id": "54428691a728c80424166ffa",
                "userId": "543270679de5893d1acea11e",
                "userName": "foo"
            },
            {
                "_id": "8434059834590834590834fa",
                "userId": "345903459034594355cea11e",
                "userName": "bar"
            }
        ]
    }
]

然后解析它的例程可能如下所示:

func loadPosts(postsJSON: NSArray) {
    var posts = [Post]()

    for postDictionary in postsJSON {
        let post = Post()
        let createDateString = postDictionary["createDate"] as String
        post.message = postDictionary["message"] as String
        post.identifier = postDictionary["_id"] as String
        post.createDate = createDateString.rfc3339Date()

        if let authorsArray = postDictionary["author"] as NSArray? {
            var authors = [Author]()
            for authorDictionary in authorsArray {
                let author = Author()
                author.userId = authorDictionary["userId"] as String
                author.userName = authorDictionary["userName"] as String
                author.identifier = authorDictionary["_id"] as String
                authors.append(author)
            }
            post.authors = authors
        }

        posts.append(post)
    }

    // obviously, do something with `posts` array here, either setting some class var, return it, whatever
}

这是我从String到的转换例程NSDate

extension String {

    /// Get NSDate from RFC 3339/ISO 8601 string representation of the date.
    ///
    /// For more information, see:
    ///
    /// https://developer.apple.com/library/ios/qa/qa1480/_index.html
    ///
    /// :returns: Return date from RFC 3339 string representation

    func rfc3339Date() -> NSDate? {
        let formatter = NSDateFormatter()

        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
        formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

        return formatter.dateFromString(self)
    }
}
于 2014-10-18T20:46:18.167 回答