Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an interface in Swift

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) {
    }
}
like image 625
Jayprakash Singh Avatar asked Aug 31 '17 05:08

Jayprakash Singh


People also ask

WHAT IS interfaces in Swift?

Interface. When one refers to an object's interface, they are referring to how an object is visible from the outside. We already know that an object contains methods that can be set as private or public. The object's interface consists of its public methods, which is the way we communicate with certain types of objects ...

What is Interface Builder in Swift?

Interface Builder The first is an editor, where you write the Swift code that makes your application run. The second is Interface Builder, which is the part where you lay out the graphical interface of your program—the buttons, toolbars, menus, images, and text that make up how your users interact with your program.

Are protocols in Swift like interfaces?

3 Answers. Show activity on this post. Essentially protocols are very similar to Java interfaces except for: Swift protocols can also specify properties that must be implemented (i.e. fields)


2 Answers

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.

lets take an exam .

protocol Animal {
    func canSwim() -> Bool
}

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

like image 181
Surjeet Rajput Avatar answered Sep 19 '22 19:09

Surjeet Rajput


What are you finding is `Protocol'. Interfaces are same as protocol in Swift.

protocol Shape {
    func shapeName() -> String
}

class Circle: Shape {
    func shapeName() -> String {
        return "circle"
    }

}

class Triangle: Shape {
    func shapeName() -> String {
        return "triangle"
    }
}

class and struct both can implements the protocol.

like image 27
Jaydeep Vora Avatar answered Sep 19 '22 19:09

Jaydeep Vora