Alamofire请求在Swift 4项目中不起作用
我试图使用Swift.org和AlamofireGithub页面上提供的示例代码,使用Alamofire和Swift触发GET请求。显然,请求没有得到执行 环境:Alamofire请求在Swift 4项目中不起作用,swift,alamofire,Swift,Alamofire,我试图使用Swift.org和AlamofireGithub页面上提供的示例代码,使用Alamofire和Swift触发GET请求。显然,请求没有得到执行 环境: macOS 10.13.3 Swift 4.0.3 阿拉莫菲尔4.6.0 代码9.2 首先,我要提出: Alamofire进入包。swift: // swift-tools-version:4.0 // The swift-tools-version declares the minimum version of Swift re
- macOS 10.13.3
- Swift 4.0.3
- 阿拉莫菲尔4.6.0
- 代码9.2
包。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"]),
]
)
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!")
然后我添加到main.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"]),
]
)
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示例代码中的print
语句都不会执行。请求也不会执行,这可以在Alamofire.request
调用指向本地web服务器时观察到
我做错了什么?使用
DispatchGroup
等待网络请求完成:
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()
使用
DispatchGroup
等待网络请求完成:
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()