菜鸟教程小白 发表于 2022-12-11 18:23:07

ios - 如何在 swift 3 中使用 Backendless REST API


                                            <p><p>我正在尝试使用 IOS swift 3 连接回无尽的休息 api。
我正在添加应用程序 ID,如下所示的 key </p>

<pre><code>request.addValue(&#34;0C896F8C-D3CE-BD08-FF1D-2B087CE77B00&#34;, forHTTPHeaderField: &#34;application-id&#34;)
request.addValue(&#34;9D9A2BCD-F272-16E8-FF01-CD5AFD8CC300&#34;, forHTTPHeaderField: &#34;secret-key&#34;)
</code></pre>

<p>我也得到了<br/></p>

<blockquote>
<p>fatal error: unexpectedly found nil while unwrapping an Optional value</p>
</blockquote>

<p>在这行代码中</p>

<pre><code> let strData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
            print(&#34;Body: \(strData)&#34;)
</code></pre>

<p>谁能告诉我发生了什么事...</p>

<pre><code>let request = NSMutableURLRequest(url: NSURL(string:&#34;/v1/users/login&#34;) as! URL)
      let session = URLSession.shared
      request.httpMethod = &#34;POST&#34;
      let params = [&#34;name&#34;:&#34;mm&#34;, &#34;password&#34;:&#34;mm&#34;] as Dictionary&lt;String, String&gt;

      request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
      request.addValue(&#34;0C896F8C-D3CE-BD08-FF1D-2B087CE77B00&#34;, forHTTPHeaderField: &#34;application-id&#34;)
      request.addValue(&#34;9D9A2BCD-F272-16E8-FF01-CD5AFD8CC300&#34;, forHTTPHeaderField: &#34;secret-key&#34;)
      request.addValue(&#34;application/json&#34;, forHTTPHeaderField: &#34;Content-Type&#34;)
      request.addValue(&#34;REST&#34;, forHTTPHeaderField: &#34;application-type&#34;)

      request.addValue(&#34;application/json&#34;, forHTTPHeaderField: &#34;Accept&#34;)

       let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -&gt; Void in
            print(&#34;Response: \(response)&#34;)
            let strData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
            print(&#34;Body: \(strData)&#34;)

      let json = try! JSONSerialization.jsonObject(with: data!, options: .mutableLeaves)
      print(json)
      //JSONObjectWithData(data, options: .MutableLeaves, error: &amp;err) as? NSDictionary

            // Did the JSONObjectWithData constructor return an error? If so, log the error to the console
      if(error != nil) {
               // print(err!.localizedDescription)
                let jsonStr = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
                print(&#34;Error could not parse JSON: &#39;\(jsonStr)&#39;&#34;)
            }
            else {
                // The JSONObjectWithData constructor didn&#39;t return an error. But, we should still
                // check and make sure that json has a value using optional binding.
                if let parseJSON = json as? NSDictionary   {                   // Okay, the parsedJSON is here, let&#39;s get the value for &#39;uccess&#39; out of it
                  let success = parseJSON[&#34;success&#34;] as? Int
                  print(&#34;Succes: \(success)&#34;)
                }
                else
                {
                  // Woa, okay the json object was nil, something went worng. Maybe the server isn&#39;t running?
                  let jsonStr = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
                  print(&#34;Error could not parse JSON: \(jsonStr)&#34;)
                }
            }
      })

      task.resume()
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你需要先检查数据不为零:</p>

<pre><code>if let d = data {
    let strData = NSString(data: d, encoding: String.Encoding.utf8.rawValue)
    print(&#34;Body: \(strData)&#34;)
} else {
    print(error)
</code></pre>

<p>}</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 swift 3 中使用 Backendless REST API,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41278215/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41278215/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 swift 3 中使用 Backendless REST API