首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将响应NSArray转换为对象数组swift3

将响应NSArray转换为对象数组swift3
EN

Stack Overflow用户
提问于 2016-12-12 14:05:20
回答 1查看 509关注 0票数 0

嗨,我能从服务器得到数组列表的响应。但我不知道如何在对象数组Feed中解析该响应,并将其发送到完成处理程序上。我的守则如下:

代码语言:javascript
运行
复制
class FeedsService {

    private var feedsEndPoint: String = "https://jsonplaceholder.typicode.com/posts"

    public func getFeeds(completion: ([Feed]) -> Void) {
        Alamofire.request(feedsEndPoint, method: .get)
            .validate(statusCode: 200..<300)
            .validate(contentType: ["application/json"])
            .responseJSON{ response in
                print("Response \(response.result.value)")

        }
    }
}

饲料模型如下:

代码语言:javascript
运行
复制
class Feed {
    private var title: String
    private var body: String

    init(title:String, body: String) {
        self.title = title
        self.body = body
    }

    public func getTitle() -> String {
        return self.title
    }

    public func getBody() -> String {
        return self.body;
    }
}

我希望将其解析为Feed,并在完成回调时将其发送出去。我正在使用Alamofire rest库从rest服务器加载数据。

EN

Stack Overflow用户

回答已采纳

发布于 2016-12-12 14:11:22

你可以试试这个:

代码语言:javascript
运行
复制
    class FeedsService {

    private var feedsEndPoint: String = "https://jsonplaceholder.typicode.com/posts"

    public func getFeeds(completion: @escaping ([Feed]) -> Void) {
        Alamofire.request(feedsEndPoint, method: .get)
            .validate(statusCode: 200..<300)
            .validate(contentType: ["application/json"])
            .responseJSON{ response in
                print("Response \(response.result.value)")
                var feeds = [Feed]()
                if let jsonArray = response.result.value as? [[String: Any]] {
                    for json in jsonArray {
                        if let feed = Feed(json: json) {
                            feeds.append(feed)
                        }
                    }
                    completion(feeds)
                } else {
                    // handle error
                }
        }
    }
}

另外,更新Feed类:

代码语言:javascript
运行
复制
class Feed {
    private var title: String
    private var body: String

    init(title:String, body: String) {
        self.title = title
        self.body = body
    }

    init?(json: [String: Any]) {
        guard let title = json["title"] as? String,
            let body = json["body"] as? String else {
                return nil
        }
        self.title = title
        self.body = body
    }

    public func getTitle() -> String {
        return self.title
    }

    public func getBody() -> String {
        return self.body;
    }
}

只需将"title“和"body”更改为json响应中适当的键即可。

正如El Tomato所指出的,Feed init不起作用,这是一个可以在操场上试用的测试代码:

代码语言:javascript
运行
复制
let testFeedJson = ["title": "Test Title", "body" : "Test Body"]
if let testFeed = Feed(json: testFeedJson) {
    print(testFeed.getTitle())
    print(testFeed.getBody())
}
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41102643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档