This question already has answers here:
Swift protocol error: 'weak' cannot be applied to non-class type
(5 answers)
What exactly does `: class` do in a protocol declaration?
(1 answer)
Swift: how to work around issue where weak variable of type 'protocol' is illegal
(1 answer)
Closed 5 years ago.
Since I don't know what the name is for the following thing, I don't know how to use proper words to describe my question. ANyway, I am wondering what is the :class ? What does it mean? and what does it do?
protocol MyDelegate: class {
func doTask()
}
==== update ===
OK, I got the answer, thanks guys, but it would be nice not only tell it is class-only protocol. But also what about enum & struct only protocol, because naturally this question will raise up, is there a way to define enum-only and struct-only protocol then?
class in your case means that classes only can implement your protocol and not structs.
That means that only classes (not structs) can implement it.
It means that you can conform to this protocol only classes, not structures or enums.
Related
This question already has answers here:
swift difference between final var and non-final var | final let and non-final let
(2 answers)
What is the difference between final Class and Class?
(7 answers)
Closed 3 years ago.
A newbie question.
I'm reading swift documentation AccessControl. It says there are 5 access specifiers.
Open
Public
Internal
File-private
Private
I thought Final is also an access specifier. If not what is it? And can someone give link to the documentation?
It's not an Access Control Specifier, it's a Declaration Modifier.
final
Apply this modifier to a class or to a property, method, or subscript member of a class. It’s applied to a class to indicate that the class can’t be subclassed. It’s applied to a property, method, or subscript of a class to indicate that a class member can’t be overridden in any subclass.
Source: Swift Language Reference - Declaration – Declaration Modifiers
This question already has answers here:
Implementing a function with a default parameter defined in a protocol
(1 answer)
Is it possible to satisfy Swift protocol and add defaulted arguments?
(4 answers)
Closed 5 years ago.
I have extended the following protocol with a default parameter.
protocol XDelegate {
func close(response: Bool, status: Status)
}
extension XDelegate{
func close(response: Bool, status: Status = .UNDEFINED){}
}
whenever I call delegate.close(true) it won't EVER call the function on the class that adopted this protocol.
I MUST do delegate.close(true, status: .UNDEFINED) so it would call the function.
Is this the expected behavior?
I'm using Swift 2.3
EDIT: See the duplicate link and also see this answer and the comments below it.
This question already has answers here:
In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?
(5 answers)
Closed 6 years ago.
Given there are 2 Protocols P1 and P2, it is possible to specify a type that conforms to both protocols, eg:
typealias P = protocol<P1, P2>
Is there a similar way to specify a type that is kind of a class and also conforms to a protocol, e.g. something like this (which does not work):
typealias P = UIView: P1
Unfortunately it's imposible in Swift 2.2 and won't be added in Swift 3.0.
The idea that you want to create Type rule for types that inherit some class and implement protocol.
So it's not very common that you will have such hierarchy and will have property where you want to store one of this classes.
Also in POP paradigm you should have additional protocol that give you properties that you need from UIView.
Maybe you have such usecase:
Then create additional class:
And your type will be P1Base
P.S. That's why if you create #property (nonatomic) UIViewController<UITableViewDelegate> *protocolClassProperty; in Obj-c, it will be bridged as UIViewController! in Swift
This question already has answers here:
In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?
(5 answers)
Closed 7 years ago.
I need to define a property of a Class to be of Type UIViewController and to implement protocol MyProtocol. In objective-C I can write something like
UIViewController<MyProtocol> myProperty;
I can't find anything on the documentation and so I suppose that this is not possible :/ can you confirm... here is the example code to add some more info
class SignInPresenter {
var myProperty:UIViewController and MyProtocol <--- here is the problem.
You have two choices. You can either use a Generic with type constraints or use protocol extension with type constraints.
In the case of Generics it looks like:
protocol P1 {
}
class X {
}
class Y : X, P1 {
}
class Z<T: X,P1> {
var myProp : T?
}
In the case of protocol extensions, you can use the protocol to provide the required methods and properties and use the extension to implement them for the specific case when the protocol is implemented by the class (e.g. UIViewController). This may be a better option if you don't really need to require UIViewController, but it will be used in practice. I prefer this kind of design as it allows for looser coupling.
First, multiple protocols are defined with a comma. For example,
class SearchResultsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, APIControllerProtocol
Secondly, variables do not conform to protocols. From Apple's documentation, "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."
This question already has answers here:
Why Choose Struct Over Class?
(17 answers)
Closed 7 years ago.
I learn Swift from some time, I know the differences between structure and class. The main difference is structure is of value type and class is of reference type but didn't understand when to use structure instead of a class. Please explain it.
For example, In case of Protocols:
First, We have just a protocol of struct type:
protocol SomeProtocol{
func doSomeStuff()
}
Second, We make protocol of class type like this:
protocol SomeProtocol: class{
func doSomeStuff()
}
So, Please explain me, when we have to use protocol of struct type or of class type.
Firstly structs are passed by value (copied), and a class is passed by reference (copied just the memory address to the object).You may want to use structs for simpler types, because you get a free init for all the properties your struct has.And with protocols, the first one you can use it on class,struct and enum, the second you say that you only use that on classes,and you may want to put class if your protocol is a delegate or a data source,because you want the property(of the type of your protocol) weak to avoid the memory cycle. IMHO use classes for multi-scene apps because you don't need to take care to update value when you edited something in an another scene.
The protocol is not "of struct type" or "of class type", that is wrong terminology.
If you write SomeProtocol: class you make sure only classes can conform to that protocol, structs cannot. If you don't include the class both classes and structs can conform.
The docs (scroll down to "Class-Only Protocols") tell you that
You can limit protocol adoption to class types (and not structures or enumerations) by adding the class keyword to a protocol’s inheritance list. The class keyword must always appear first in a protocol’s inheritance list, before any inherited protocols.
Use a class-only protocol when the behavior defined by that protocol’s requirements assumes or requires that a conforming type has reference semantics rather than value semantics. For more on reference and value semantics, see Structures and Enumerations Are Value Types and Classes Are Reference Types.