swift protocol 'weak' cannot be applied to non-class type

boog :

I'm a bit confused. What's the difference between protocol A : class { ... } and protocol A{ ... }, and which one we should use in swift?

PS: we got an error when we wrote like this

protocol A{ ... }

weak var delegate: A

error : 'weak' cannot be applied to non-class type

Martin R :
protocol A : class { ... }

defines a "class-only protocol": Only class types (and not structures or enumerations) can adopt this protocol.

Weak references are only defined for reference types. Classes are reference types, structures and enumerations are value types. (Closures are reference types as well, but closures cannot adopt a protocol, so they are irrelevant in this context.)

Therefore, if the object conforming to the protocol needs to be stored in a weak property then the protocol must be a class-only protocol.

Here is another example which requires a class-only protocol:

protocol A { 
    var name : String { get set }
}

func foo(a : A) {
    a.name = "bar" // error: cannot assign to property: 'a' is a 'let' constant
}

This does not compile because for instances of structures and enumerations, a.name = "bar" is a mutation of a. If you define the protocol as

protocol A : class { 
    var name : String { get set }
}

then the compiler knows that a is an instance of a class type to that a is a reference to the object storage, and a.name = "bar" modifies the referenced object, but not a.

So generally, you would define a class-only protocol if you need the types adopting the protocol to be reference types and not value types.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift. unowned may only be applied to class and class-bound protocol types. weak works fine

Swift protocol & weak references with class

Swift: binary operator '==' cannot be applied to operands of type "protocol"

Weak property in a Swift protocol may only be a class or class-bound protocol type

xcode 7 Type arguments cannot be applied to non-parameterized class

Swift warning: 'weak' should not be applied to a property declaration in a protocol

swift protocol, IBOutlet property cannot have non-object type

Swift - Type 'any Protocol' cannot conform to 'Protocol'

weak Delegate and class protocol

Why can the keyword "weak" only be applied to class and class-bound protocol types

weak may only be applied to class and class-bound protocol types not <<errortype>>

'weak' may only be applied to class and class-bound protocol types, not '(ViewController) -> () -> ViewController'

Swift: the type of a class that implements a protocol

A weak property in Objective-C class implementing a Swift protocol

Type arguments cannot be applied to non-parameterized class BFTask in PFAnalytics and PFObject

Swift - Binary operator cannot be applied to operands of type

Swift: '!=' cannot be applied to operands of type 'String' and 'Any'

Error: method in class cannot be applied to given type

Protocol requirement cannot be satisfied by a non-final class because it uses 'Self' in a non-parameter, non-result type

Swift protocol property check if weak

Why Swift disallows weak reference for non-optional type?

Cannot assign class instance to its protocol type?

Swift generic type that conform to protocol cannot be used to refer protocol?

Swift Generic Protocol Class Type in Array

Get the class type that implements a protocol swift

Are mixed class/protocol type constraints allowed in Swift protocol extensions?

Inheritance from non-protocol, non-class Type

Inheritance from non-protocol, non-class type 'CGPoint'

Inheritance from non-protocol, non-class type 'V'