如何在Swift中创建接口

编程入门 行业动态 更新时间:2024-10-28 09:22:45
本文介绍了如何在Swift中创建接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想快速创建类似接口的功能,我的目标是当我调用另一个类时假设我正在调用API,并且该类的响应我想反映到当前屏幕中,因此在android接口中用于实现但是我该如何快速使用呢?谁能帮我举个例子. android的代码在下面给出...

i want to create functionality like interface in swift, my goal is when i call another class suppose i'm calling API and the response of that class i want to reflect in to my current screen, in android interface is used to achieve but what should i use in swift for that? can anyone help me with example. the code for android is given below...

public class ExecuteServerReq { public GetResponse getResponse = null; public void somemethod() { getResponse.onResponse(Response); } public interface GetResponse { void onResponse(String objects); } } ExecuteServerReq executeServerReq = new ExecuteServerReq(); executeServerReq.getResponse = new ExecuteServerReq.GetResponse() { @Override public void onResponse(String objects) { } }

推荐答案

而不是界面swift拥有协议.

Instead of interface swift have Protocols.

协议定义了适合特定任务或功能的方法,属性和其他要求的蓝图.然后,该协议可以由类,结构或枚举采用,以提供这些要求的实际实现.满足协议要求的任何类型都被认为符合该协议.

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.

让我们参加考试.

protocol Animal { func canSwim() -> Bool }

并且我们有一个确认此协议名称为Animal

and we have a class that confirm this protocol name Animal

class Human : Animal { func canSwim() -> Bool { return true } }

有关更多信息,请访问- developer .apple/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

for more go to - developer.apple/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

更多推荐

如何在Swift中创建接口

本文发布于:2023-06-04 19:05:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   如何在   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!