如何将 URL 中的信息提取到我的 swift 应用程序中
How to pull information from a URL into my swift app
首先我想说我是 swift(以及一般编码)的完全新手。我想从 URL(即:体育赛事列表)中提取一些信息到我应用程序的 viewcontroller 视图之一。我已经查找了各种引用 kanna 和 JSON 的线程,但正如我所提到的,我对此很陌生,所以我现在还不太理解。
任何人都可以提供一些简单的步骤或知道一些 videos/documentation 我可以阅读以实现此目的吗?
谢谢,
我将使用 Alamofire 作为答案,这是一个非常好的框架,当您在 Swift[=17= 中与 Web APIs 交互时,可以检查它]
您首先要创建一个自定义模型,您可以使用该模型将 JSON 数据映射到收到数据时,我将其称为 Fixture
。我不知道你用的是哪个 API 或者你的模型需要包含什么所以我会做一些事情
struct Fixture {
var id: Int?
var name: String?
init(from dict: Dictionary<String, AnyObject>) {
// We'll this out later
}
}
然后您需要使用 Alamofire 创建一个函数来调用 API 并获得响应。这是一个非常简单的函数,没有任何参数或 headers.
Alamofire.request(/*endpoint url*/, method: .get, parameters: nil, encoding: JSONEncoding.prettyPrinted, headers: nil).responseJSON { response in
if response.response?.statusCode == 200 {
if let JSON = response.result.value {
if let response = JSON as? Dictionary<String, AnyObject> {
// This is where to take the values out of the JSON and cast them as Swift types.
//For this example I will imagine that one fixture is returned in a dictionary called "fixture"
if let dict = response["fixture"] as? Dictionary<String, AnyObject> {
let fixture = Fixture(from: dict)
}
}
}
}
}
这是一个非常简单的例子。根据 API 响应的确切结构,它看起来会有所不同。如果您使用您将要呼叫的端点更新您的问题,我可以在更多帮助下更新此答案。
至于 Fixture
模型中的 init
方法,我们现在可以将其更新为如下所示:
init(from dict: Dictionary<String, AnyObject>) {
id = dict["id"] as? Int
name = dict["name"] as? String
// Again these will need to be changed to accomodate the exact response
}
你的问题有多个部分。
您需要从远程服务器获取数据,然后需要解析它。第一部分,下载,可以使用NSURLSession
(在Swift中改名为URLSession
3.
我在 Github 上有一个名为 Async_demo 的示例项目,它演示了如何使用 URLSession
下载数据。
要解析 JSON,您可以使用 JSONSerialization
,这使得将 JSON 数据转换为 Swift 对象变得非常简单。您应该能够在 Swift JSONSerialization
上搜索以在此处或其他地方找到示例。
您还可以使用像 SwiftyJSON
或 AlamoFire
这样的第三方库来进行 JSON 解析(以及下载,就此而言)。
使用 URLSession
和 JSON 序列化并不难,但是,这是学习如何使用 Apple 在 Xcode 中出色的 API 文档的一个很好的练习并学习应用程序框架。
首先我想说我是 swift(以及一般编码)的完全新手。我想从 URL(即:体育赛事列表)中提取一些信息到我应用程序的 viewcontroller 视图之一。我已经查找了各种引用 kanna 和 JSON 的线程,但正如我所提到的,我对此很陌生,所以我现在还不太理解。
任何人都可以提供一些简单的步骤或知道一些 videos/documentation 我可以阅读以实现此目的吗?
谢谢,
我将使用 Alamofire 作为答案,这是一个非常好的框架,当您在 Swift[=17= 中与 Web APIs 交互时,可以检查它]
您首先要创建一个自定义模型,您可以使用该模型将 JSON 数据映射到收到数据时,我将其称为 Fixture
。我不知道你用的是哪个 API 或者你的模型需要包含什么所以我会做一些事情
struct Fixture {
var id: Int?
var name: String?
init(from dict: Dictionary<String, AnyObject>) {
// We'll this out later
}
}
然后您需要使用 Alamofire 创建一个函数来调用 API 并获得响应。这是一个非常简单的函数,没有任何参数或 headers.
Alamofire.request(/*endpoint url*/, method: .get, parameters: nil, encoding: JSONEncoding.prettyPrinted, headers: nil).responseJSON { response in
if response.response?.statusCode == 200 {
if let JSON = response.result.value {
if let response = JSON as? Dictionary<String, AnyObject> {
// This is where to take the values out of the JSON and cast them as Swift types.
//For this example I will imagine that one fixture is returned in a dictionary called "fixture"
if let dict = response["fixture"] as? Dictionary<String, AnyObject> {
let fixture = Fixture(from: dict)
}
}
}
}
}
这是一个非常简单的例子。根据 API 响应的确切结构,它看起来会有所不同。如果您使用您将要呼叫的端点更新您的问题,我可以在更多帮助下更新此答案。
至于 Fixture
模型中的 init
方法,我们现在可以将其更新为如下所示:
init(from dict: Dictionary<String, AnyObject>) {
id = dict["id"] as? Int
name = dict["name"] as? String
// Again these will need to be changed to accomodate the exact response
}
你的问题有多个部分。
您需要从远程服务器获取数据,然后需要解析它。第一部分,下载,可以使用NSURLSession
(在Swift中改名为URLSession
3.
我在 Github 上有一个名为 Async_demo 的示例项目,它演示了如何使用 URLSession
下载数据。
要解析 JSON,您可以使用 JSONSerialization
,这使得将 JSON 数据转换为 Swift 对象变得非常简单。您应该能够在 Swift JSONSerialization
上搜索以在此处或其他地方找到示例。
您还可以使用像 SwiftyJSON
或 AlamoFire
这样的第三方库来进行 JSON 解析(以及下载,就此而言)。
使用 URLSession
和 JSON 序列化并不难,但是,这是学习如何使用 Apple 在 Xcode 中出色的 API 文档的一个很好的练习并学习应用程序框架。