菜鸟教程小白 发表于 2022-12-11 20:34:49

ios - 如何在 Alamofire 中使用 PUT 请求


                                            <p><p>我是 swift 新手,我也在尝试使用 Alamofire 从 API 调用数据。我对如何使用 <strong>PUT 请求</strong> 来更新数据感到很困惑。我已经在 SO 中阅读了一些解决方案,但我不知道我将如何应用到我的应用程序中。我正在创建一个Event App,场景应该是,当参与者点击<strong>Check In</strong>按钮时,它会将<code>registered_flag</code>更新为<code>true</code>的意思参与者将被标记为<em>已注册</em>,按钮将更改为<strong> checkout </strong>。我真的不知道我的 API 服务是否正确。希望你能帮助我。非常感谢。</p>

<p><strong>事件参与者的 JSON 中的注册标志应在 checkInOutButton 后更新一次</strong></p>

<pre><code>{
    &#34;event_name&#34;: &#34;Q &amp; A&#34;,
    &#34;event_participants&#34;: [
      {
            &#34;participant_id&#34;: &#34;70984656-92bc-4c36-9314-2c741f068523&#34;,
            &#34;employee_number&#34;: null,
            &#34;last_name&#34;: &#34;Surname&#34;,
            &#34;first_name&#34;: &#34;FirstName&#34;,
            &#34;middle_name&#34;: null,
            &#34;display_name&#34;: &#34;Surname, FirstName &#34;,
            &#34;department_name&#34;: &#34;Medical Informatics&#34;,
            &#34;position_name&#34;: &#34;Application Developer&#34;,
            &#34;registered_flag&#34;: true,
            &#34;registered_datetime&#34;: &#34;2018-09-13T08:54:40.150&#34;,
            &#34;registration_type&#34;: 1,
            &#34;delete_flag&#34;: false,
            &#34;manual_reg_flag&#34;: false,
            &#34;out_flag&#34;: false,
            &#34;out_datetime&#34;: null,
            &#34;classification&#34;: 6,
            &#34;others&#34;: &#34;Guest&#34;
          }
      }
</code></pre>

<p><strong>更新 JSON 以进行签到</strong></p>

<pre><code>{
   &#34;registered_flag&#34;: true,
   &#34;registration_type&#34;: 1
}
</code></pre>

<p><strong>updateType</strong></p>

<pre><code>enum UpdateParticipantType: String {

case checkIn = &#34;Check In&#34;
case checkOut = &#34;Check Out&#34;
}
</code></pre>

<p><strong>UpdateParticipant 的 APIService</strong></p>

<pre><code> func updateParticipant(updateType: UpdateParticipantType,
                     participantID: String,
                     successBlock: @escaping () -&gt; Void,
                     failureBlock: @escaping (Error) -&gt; Void)

{

   let updateParticipantURL = URL(string: &#34;\(REGISTER_PARTICIPANT_URL)/\(updateType)/\(participantID)&#34;)

    Alamofire.request(updateParticipantURL!, method: .put).responseJSON { (response) in
      print(response)

      if let error = response.error
      {
            failureBlock(error)
            print(error)
            return
      }

      if let jsonArray = response.result.value as? [] {
            for anItem in jsonArray {
                if let eventparticipants = anItem[&#34;event_participants&#34;] as? [] {
                  var extractedAttendees = ()
                  for participants in eventparticipants{
                        let success = Attendee.init(JSON: participants)
                        extractedAttendees.append(success!)
                  }
                  successBlock(extractedAttendees)
                }
            }
      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>根据 <a href="https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#http-methods" rel="noreferrer noopener nofollow">Alamofire</a>文档:</p>

<pre><code>let parameters: Parameters = [
    &#34;foo&#34;: &#34;bar&#34;,
    &#34;baz&#34;: [&#34;a&#34;, 1],
    &#34;qux&#34;: [
      &#34;x&#34;: 1,
      &#34;y&#34;: 2,
      &#34;z&#34;: 3
    ]
]

Alamofire.request(&#34;https://httpbin.org/post&#34;, method: .post, parameters: parameters)
</code></pre>

<p>对于给定的 json</p>

<pre><code> {
    &#34;registered_flag&#34;: true,
    &#34;registration_type&#34;: 1
}

let parameters: Parameters = [
   &#34;registered_flag&#34;: true
   &#34;registration_type&#34;: 1
]
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Alamofire 中使用 PUT 请求,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52307722/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52307722/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Alamofire 中使用 PUT 请求