如何在Swift中创建接口 [英] How to create an interface in 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
}
}
for more go to - https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html
这篇关于如何在Swift中创建接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!