Alamofire请求在Swift 4项目中不起作用 [英] Alamofire request not working in Swift 4 project

查看:122
本文介绍了Alamofire请求在Swift 4项目中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swift.org和Alamofire GitHub页面上提供的示例代码使用Alamofire和Swift触发GET请求。显然,该请求没有得到执行。

I am trying to trigger a GET request using Alamofire and Swift using example code provided on Swift.org and the Alamofire GitHub page. Apparently, the request does not get executed.

环境:


  • macOS 10.13 .3

  • Swift 4.0.3

  • Alamofire 4.6.0

  • Xcode 9.2

  • macOS 10.13.3
  • Swift 4.0.3
  • Alamofire 4.6.0
  • Xcode 9.2

首先,我创建新的可执行文件包:

[u@h ~/swift]$ mkdir Foo
[u@h ~/swift]$ cd Foo/
[u@h ~/swift/Foo]$ swift package init --type executable
Creating executable package: Foo
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/Foo/main.swift
Creating Tests/

Alamofire获取添加为依赖项 Package.swift 中:

Alamofire gets added as a dependency in Package.swift:

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Foo",
    dependencies: [
        .package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.0.0")
    ],
    targets: [
        .target(
            name: "Foo",
            dependencies: ["Alamofire"]),
    ]
)

然后我添加示例代码 main.swift

import Alamofire

print("Hello, world!")

Alamofire.request("https://httpbin.org/get").responseJSON { response in
    print("Request: \(String(describing: response.request))")   // original url request
    print("Response: \(String(describing: response.response))") // http url response
    print("Result: \(response.result)")                         // response serialization result

    if let json = response.result.value {
        print("JSON: \(json)") // serialized json response
    }

    if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
        print("Data: \(utf8Text)") // original server data as UTF8 string
    }
}

print("Goodbye, world!")

之后,我尝试运行它:

[u@h ~/swift/Foo]$ swift run
Fetching https://github.com/Alamofire/Alamofire.git
Cloning https://github.com/Alamofire/Alamofire.git
Resolving https://github.com/Alamofire/Alamofire.git at 4.6.0
Compile Swift Module 'Alamofire' (17 sources)
Compile Swift Module 'Foo' (1 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/Foo
Hello, world!
Goodbye, world!

如您所见,所有印刷品 Alamofire示例代码中的>语句被执行。该请求也不会执行,这在 Alamofire.request 调用指向本地Web服务器时可以观察到。

As you can see, none of the print statements in the Alamofire example code gets executed. The request does not get executed either, which can be observed when the Alamofire.request call points to a local web server.

我在做什么错了?

推荐答案

使用 DispatchGroup 等待网络请求完成:

Use DispatchGroup to wait for the network request's completion:

import Alamofire
import Foundation

print("Hello, world!")

let group = DispatchGroup()

group.enter()
Alamofire.request("https://httpbin.org/get").responseJSON { response in
    // handle the response
    group.leave()
}

group.notify(queue: DispatchQueue.main) {
    print("Goodbye, world!")
    exit(0)
}
dispatchMain()

这篇关于Alamofire请求在Swift 4项目中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆