Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent for java interfaces or objective c protocols in swift?

I've been looking in to the new Swift language trying to find what's the equivalent for an interface(in java) or a protocol(in objective-c) in Swift, after surfing on the internet and searching in the book provided by Apple, I still can't seem to find it.

Does any one know what's the name of this component in swift and what's its syntax?

like image 984
Martin Cazares Avatar asked Jun 05 '14 22:06

Martin Cazares


People also ask

Are protocols in Swift similar to interfaces?

Essentially protocols are very similar to Java interfaces except for: Swift protocols can also specify properties that must be implemented (i.e. fields)

Can Swift protocol be used in Objective C?

We can use swift protocols in Objective C with few changes to the code. Also, @objc declared protocols let you have optional and required methods without default implementations. It comes with pros and cons. We could actually re-name the protocol name to a more descriptive when using in Objective C.

What is a Swift interface?

The Compatible Interface Programme is for SWIFT interfaces developed by partners and customers. It verifies that interfaces meet interoperability and security requirements.

What is difference between Objective C protocol and Swift protocol?

In Swift, calling a method will be decided at compile time and is similar to object-oriented programming, whereas in Objective C, calling a method will be decided at runtime, and also Objective C has special features like adding or replacing methods like on a class which already exists.

What is the difference between Java 8 interface and Swift protocol?

The Swift protocol magic comes from the extension. 2). Java 8 interface can have default implementations, but it cannot be done "retroactively." its implementations if needed) to any class or structure. Swift protocols do not follow the generic (i.e <..>) customization pattern, but a typealias scheme (i.e. Associated Types).

What is the difference between Swift programming language and Objective C?

Swift is one of the most preferred programming languages while Objective C is a degrading one. Apps development in Swift is faster than in Objective C. The only one disadvantage of Swift app development is that it is not stable.

What is the difference between Java interfaces and protocols?

Essentially protocols are very similar to Java interfaces except for: Swift protocols can also specify properties that must be implemented (i.e. fields) Swift protocols need to deal with value/reference through the use of the mutating keyword (because protocols can be implemented by structs and classes)

What is the Swift programming language?

The first release of Swift took place in 2014. It was positioned as a faster and more efficient programming language for creating iOS and macOS applications. It has become the fastest-growing programming language in history. Unlike Objective-C, Swift was built for the average developer.


1 Answers

Protocols in Swift are very similar to Objc, except you may use them not only on classes, but also on structs and enums.

protocol SomeProtocol {      var fullName: String { get } // You can require iVars      class func someTypeMethod() // ...or class methods } 

Conforming to a protocol is a bit different:

class myClass: NSObject, SomeProtocol // Specify protocol(s) after the class type 

You can also extend a protocol with a default (overridable) function implementation:

extension SomeProtocol {       // Provide a default implementation:       class func someTypeMethod() {            print("This implementation will be added to objects that adhere to SomeProtocol, at compile time")            print("...unless the object overrides this default implementation.")       } } 

Note: default implementations must be added via extension, and not in the protocol definition itself - a protocol is not a concrete object, so it can't actually have method bodies attached. Think of a default implementation as a C-style template; essentially the compiler copies the declaration and pastes it into each object which adheres to the protocol.

like image 135
chris stamper Avatar answered Sep 28 '22 00:09

chris stamper