-1

我是新来的午睡。如何获取整个数组并传递给我的模型?此外,我如何使用参数发布?在他们的文档中,我找不到任何这些。

4

2 回答 2

1

我也是午睡的新手。我可以在这里找到有关请求的文档http://bustoutsolutions.github.io/siesta/guide/requests/

基本上你会设置你的资源,然后调用:

resource.request(.post, json: ["foo": [1,2,3]])
于 2017-02-13T15:31:00.757 回答
0

您的问题太复杂了,所以我将尝试以简单的方式向您解释。

  1. Siesta 提供了一种使用自定义转换器映射模型的好方法。一种简单的方法是实现 Swift 4+ 提供的 Decodable 协议:

假设我们要解码这个 JSON 响应:

{
    "id": 1,
    "name": "Oswaldo",
    "email": "omaestra@gmail.com"
}

进入我们User实现 Decodable 协议的伟大类:

class User: Decodable {
    var id: Int
    var name: String
    var email: String

    private enum CodingKeys: String, CodingKey {
        case id, name, email
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        id = try container.decode(Int.self, forKey: .id
        name = try container.decode(String.self, forKey: .name)
        email = try container.decode(String.self, forKey: .email)
    }
}

惊人的!现在我们可以将来自服务器的 JSON 响应解码为我们很棒的User类。

接下来,在我们的Siesta.Service类中,我们可以为特定资源配置自定义转换器:

class API: Siesta.Service {
    init() { 
        super.init(baseURL: "https://api.example.com")
        // Some initial configuration
    }

    let myAPI = API()

    // –––––– Global configuration ––––––
    let jsonDecoder = JSONDecoder()

    // –––––– Mapping from specific paths to models ––––––
    // These all use Swift 4’s JSONDecoder, but you can configure arbitrary transforms on arbitrary data types.
    configureTransformer("rest/user/") {
        // Input type inferred because the from: param takes Data.
        // Output type inferred because jsonDecoder.decode() will return User
        try jsonDecoder.decode(User.self, from: $0.content)
    }

    // MARK: - Resources
    var userResource: Siesta.Resource { return resource("rest/user/") }
}

最后,我们可以在我们的内部实现我们的资源ViewController

class UserViewController: UIViewController {
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var emailLabel: UILabel!!

    override func viewDidLoad() {
        super.viewDidLoad()

        myAPI.userResource
            .addObserver(self)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        myAPI.userResource.loadIfNeeded()
    }
}

extension UserViewController: ResourceObserver {
    func resourceChanged(_ resource: Resource, event: ResourceEvent) {
        let user: User? = resource.typedContent()
        nameLabel.text = user?.name
        emailLabel.text = user?.email
    }
}

注意:Siesta 是一个非常灵活和可定制的框架,您可以找到多种方式来配置您的服务和资源。这只是实现您所要求的一种简单方法。


  1. 要使用参数发出 POST 请求,您可以在Service类中执行以下操作:

实现一个update发出 POST 请求的方法。

func update(user: User, newName: String) -> Siesta.Request {
    return usersResource
        .child(user.id)
        .child("update")
        .request(.post, json: ["name": newName])
}

然后,在您的中,ViewController您可以调用该方法来提交POST请求并评估其响应:

myAPI.update(user: user, newName: "NEW NAME")
    .onSuccess({ (_) in
            print("Successfully updated user's name.")
        })
    .onFailure({ (error) in
            print("Error trying to update user's name.")
        })
于 2019-02-26T16:49:30.180 回答