matlab生成应用程序_生成一个SwiftUI联系人搜索应用程序

matlab生成应用程序

As SwiftUI completes a year, there’s a lot to look forward to from WWDC 2020. Having an out-of-the-box search bar functionality should be on everyone’s wishlist.

随着SwiftUI一年的完成,WWDC 2020会有很多期待。拥有开箱即用的搜索栏功能应该在每个人的心愿单上。

Until then, we can leverage the SwiftUI-UIKit interoperability to build a custom search bar.

在此之前,我们可以利用SwiftUI-UIKit的互操作性来构建自定义搜索栏。

In the following sections, we’ll be building a SwiftUI application for searching contacts.

在以下各节中,我们将构建一个用于搜索联系人的SwiftUI应用程序。

行动计划 (Plan of Action)

  • Use the Contacts framework to fetch your phone contacts.

    使用联系人框架来获取您的电话联系人。
  • Populate them in a SwiftUI List.

    将它们填充到SwiftUI列表中。
  • Use the UIViewRepresentable protocol to create a UISearchBar wrapper for SwiftUI.

    使用UIViewRepresentable协议为SwiftUI创建UISearchBar包装器。

  • Filter the SwiftUI contacts list based on searched text.

    根据搜索到的文本过滤SwiftUI联系人列表。

入门-添加隐私说明 (Get Started — Add Privacy Descriptions)

The Contacts framework lets you read information without making any changes. In order to access that in your application, we first need to set the privacy usage descriptions in the info.plist file. Just add the NSContactsUsageDescription key with a string value explaining the need for the permission.

联系人框架使您无需更改即可阅读信息。 为了访问您的应用程序中的内容,我们首先需要在info.plist文件中设置隐私使用说明。 只需添加带有字符串值的NSContactsUsageDescription键,说明需要权限。

Image for post

用UIViewRepresentable和Coordinators包装UISearchBar (Wrap UISearchBar With UIViewRepresentable and Coordinators)

SwiftUI doesn’t come with built-in functionality for search bars. So, we need to conform our struct to the UIViewRepresentable protocol and initialize the UIKit’s UISearchBar instance in it as shown below:

SwiftUI没有搜索栏的内置功能。 因此,我们需要使结构符合UIViewRepresentable协议,并在其中初始化UIKit的UISearchBar实例,如下所示:

The makeCoordinator() function lets us create a Coordinator class, which is responsible for communicating changes from our UIKit view to the SwiftUI interface. Let’s define the Coordinator class, which also declares the UISearchBarDelegate protocol:

通过makeCoordinator()函数,我们可以创建一个Coordinator类,该类负责将更改从UIKit视图传递到SwiftUI接口。 让我们定义Coordinator类,该类还声明UISearchBarDelegate协议:

创建一个ObservableObject类以获取联系人 (Create an ObservableObject Class for Fetching Contacts)

ObservableObject protocol is a part of the Combine framework and is used to announce changes in the SwiftUI view via a @Published property wrapper.

ObservableObject协议是Combine框架的一部分,用于通过@Published宣布SwiftUI视图中的@Published 属性包装器。

In the following code, we’re fetching contacts after ensuring that the permission is granted and then storing them in a Published property wrapper array:

在下面的代码中,我们在确保授予权限后获取联系人,然后将其存储在Published属性包装器数组中:

SwiftUI needs a way to identify each contact uniquely. In order to do that, we’ve created an extension at the end that conforms to the Identifiable protocol.

SwiftUI需要一种唯一标识每个联系人的方法。 为了做到这一点,我们在最后创建了一个符合Identifiable协议的扩展。

Now, we’re all set to integrate the search bar wrapper and ObservableObject class instance in our SwiftUI View. Let’s do it.

现在,我们已经准备好将搜索栏包装器和ObservableObject类实例集成到我们的SwiftUI视图中。 我们开始做吧。

构建SwiftUI视图 (Build the SwiftUI View)

We’ll use an @EnvironmentObject to retrieve the contacts. The property wrapper lets you access shared data across the application.

我们将使用@EnvironmentObject检索联系人。 使用属性包装器,您可以访问整个应用程序中的共享数据。

In the following code, by using a SwiftUI List, we’re able to populate the contacts and update them using filter based on the searchText value changes.

在以下代码中,通过使用SwiftUI列表,我们可以填充联系人并使用基于searchText值更改的filter更新联系人。

Here’s a look at the application in action when we ran it on a simulator that holds some dummy contacts:

这是当我们在包含一些虚拟联系人的模拟器上运行该应用程序时的情况:

Image for post

结论 (Conclusion)

We managed to set up a SwiftUI contacts application with search functionalities pretty quickly — despite no support for built-in search bar in SwiftUI.

尽管设法不支持SwiftUI中的内置搜索栏,我们还是很快就建立了具有搜索功能的SwiftUI联系人应用程序。

Doing the same thing with UITableView and UISearchBar would take a considerable amount of time, effort, and code. We hope to see SwiftUI 2.0 in WWDC 2020 bolstered with new features.

使用UITableViewUISearchBar进行相同的操作将花费大量的时间,精力和代码。 我们希望看到WWDC 2020中的SwiftUI 2.0具有新功能。

You can download the full source code from the Github Repository.

您可以从Github存储库下载完整的源代码。

That’s it for this one. Thanks for reading.

就这个。 谢谢阅读。

翻译自: https://medium.com/better-programming/build-a-swiftui-contacts-search-application-d41b414fe046

matlab生成应用程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值