swift调用api接口_如何在Swift中进行API调用

swift调用api接口

If you are looking to become an iOS developer, there are some fundamental skills worth knowing. First, it's important to be familiar with creating table views. Second, you should know how to populate those table views with data. Third, it's great if you can fetch data from an API and use this data in your table view.

如果您想成为一名iOS开发人员,那么有一些基本技能值得了解。 首先,熟悉创建表视图很重要。 其次,您应该知道如何用数据填充这些表视图。 第三,如果您可以从API提取数据并在表格视图中使用此数据,那就太好了。

The third point is what we'll cover in this article. Since the introduction of Codable in Swift 4, making API calls is much easier. Previously most people used pods like Alamofire and SwiftyJson (you can read about how to do that here). Now the Swift way is much nicer out of the box, so there's no reason to download a pod.

第三点是我们将在本文中介绍的内容。 自从Swift 4中引入Codable以来,进行API调用变得更加容易。 以前,大多数人都使用过诸如Alamofire和SwiftyJson之类的Pod(您可以在此处了解如何做)。 现在,Swift方法开箱即用,因此无需下载Pod。

Let's go through some building blocks that are often used to make an API call. We'll cover these concepts first, as they are important parts to understanding how to make an API call.

让我们看一下一些通常用于进行API调用的构造块。 我们将首先介绍这些概念,因为它们是理解如何进行API调用的重要部分。

  • Completion handlers

    完成处理程序
  • URLSession

    URLSession

  • DispatchQueue

    DispatchQueue

  • Retain cycles

    保留周期

Finally we'll put it all together. I'll be using the open source Star Wars API to build this project. You can see my full project code on GitHub.

最后,我们将它们放在一起。 我将使用开源的《星球大战》 API构建该项目。 您可以在GitHub上查看我的完整项目代码。

Disclaimer alert: I am new to coding and am largely self-taught. Apologies if I misrepresent some concepts.

免责声明:我是编码的新手,而且自学成才。 抱歉,如果我歪曲了一些概念。

完成处理程序 (Completion handlers)

Remember the episode of Friends where Pheobe is glued to the phone for days waiting to speak with customer service? Imagine if at the very start of that phone call, a lovely person called Pip said: "Thanks for calling. I have no idea how long you'll need to wait on hold, but I'll call you back when we're ready for you." It wouldn't have been as funny, but Pip is offering to be a completion handler for Pheobe.

还记得在《 Friends》一集中,Pheobe被粘在电话上数天,等待与客户服务交谈吗? 想象一下,如果在电话刚开始的时候,一个叫Pip的可爱的人说:“谢谢您的通话。我不知道您需要等待多长时间,但是准备就绪后我会给您回电。为了你。” 不会那么有趣,但是Pip愿意成为Pheobe的完成处理程序。

You use a completion handler in a function when you know that function will take a while to complete. You don't know how long, and you don't want to pause your life waiting for it to finish. So you ask Pip to tap you on the shoulder when she's ready to give you the answer. That way you can go about your life, run some errands, read a book, and watch TV. When Pip taps on you on the shoulder with the answer, you can take her answer and use it.

当您知道某个函数需要一段时间才能完成时,可以在函数中使用完成处理程序。 您不知道要等多久,也不想停下来等待生活的结束。 因此,当她准备好为您提供答案时,您可以要求Pip轻拍您的肩膀。 这样一来,您就可以过上自己的生活,办一些差事,读书和看电视。 当Pip用答案轻拍您的肩膀时,您可以使用她的答案并使用它。

This is what happens with API calls. You send a URL request to a server, asking it for some data. You hope the server returns the data quickly, but you don't know how long it will take. Instead of making your user wait patiently for the server to give you the data, you use a completion handler. This means you can tell your app to go off and do other things, such as loading the rest of the page.

这就是API调用发生的情况。 您将URL请求发送到服务器,要求它提供一些数据。 您希望服务器快速返回数据,但是您不知道需要多长时间。 您可以使用完成处理程序来代替让用户耐心等待服务器为您提供数据。 这意味着您可以告诉您的应用程序关闭并

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值