如何在Swift中创建接口
问题描述:
我想在swift中创建类似接口的功能,我的目标是当我调用另一个类时,假设我调用了API并且该类的响应我想反映到当前屏幕中,在android界面用于实现,但我应该在swift中使用那个?谁能帮我举例。 Android的代码如下......如何在Swift中创建接口
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) {
}
}
答
相反接口雨燕有协议。
协议定义了适合特定任务或功能块的方法,属性和其他需求的蓝图。协议然后可以被类,结构或枚举采用来提供这些需求的实际实现。据说满足协议要求的任何类型都符合该协议。
让我们参加考试。
protocol Animal {
func canSwim() -> Bool
}
,我们有一个确认这个协议名称动物
class Human : Animal {
func canSwim() -> Bool {
return true
}
}
答
你在寻找的'协议”。接口与Swift中的协议相同。
protocol Shape {
func shapeName() -> String
}
class Circle: Shape {
func shapeName() -> String {
return "circle"
}
}
class Triangle: Shape {
func shapeName() -> String {
return "triangle"
}
}
class
和struct
既可以实现。
+0
函数不能工作....任何其他示例? –
在swift协议中作为interface.Google协议在swift中工作。 –
你可以创建相同的协议 –
@TusharSharma任何给我的例子.. –