Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 292 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare Protocol in Swift vs Interface in Java

#1
I'm going through the iOS tutorial from [Apple developer page][1].


[1]:

[To see links please register here]


It seems to me that `protocol` and `interface` almost have the same functionality.


- Are there any differences between the two?

- the different usage in the project?


**Updated**

**Yes**, I did read the link above and I'm still not sure what the differences and usage between `protocol` and `interface`. When I ask a question like this, I would like to see a simple explanation about the topic. Sometime it could be tough to get everything from the documentation.
Reply

#2
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 structures, enumerations or classes).
* you can combine protocols at any point using "Protocol Composition". This replaces the older swift `protocol<A, B>` way of protocol composition. For example, declaring a function parameter that must adhere to protocol `Named` and `Aged` as:
```swift
func wishHappyBirthday(to celebrator: Named & Aged) {}
```
These are the immediately apparent differences for a Java developer (or at least what I've spotted so far). There's more info [here](

[To see links please register here]

).
Reply

#3
Just adding one thing because I've been checking protocols combining:
> you can combine protocols at any point with the protocol<> keyword.

This is no longer true.
Based on this:

[To see links please register here]


This is correct:

```
func foo ( var1 : A & B ){}
```
Reply

#4
Complementing @Thomas Schar's answer. The Swift protocol magic comes from the extension.

- Swift protocols can get implementations via the extension (Swift
2). Java 8 interface can have default implementations, but it cannot be
done "retroactively."
- In Swift, you can "retroactively" add protocol requirements (and
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). Can be confusing at the start, but can avoid
"angle bracket blindness" in some cases.
- Swift has an advanced type pattern matching, allowing to be very
specific on where and how protocol requirements and extensions get applied. It can be confusing when coming from Java, but it has a lot of power.
- A swift protocol can be composed for a property/param (i.e. celebrator:
protocol<Named, Aged>)

One thing that got me scratching my head for a couple of hours is that not all protocols can be used as property types. For example, if you have a protocol with typealias, you cannot directly use it as a type of property (it makes sense when you think about it, but coming from Java we really want to have a property like userDao: IDao<User>).




Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through