Swift protocol error: 'weak' cannot be applied to non-class type
Swift >= 4:
protocol A : AnyObject { ... {
Swift < 4:
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 thata
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.
Why can the keyword weak only be applied to class and class-bound protocol types
weak
is a qualifier for reference types (as opposed to value types, such as struct
s and built-in value types).
Reference types let you have multiple references to the same object. The object gets deallocated when the last strong reference stops referencing it (weak references do not count).
Value types, on the other hand, are assigned by copy. Reference counting does not apply, so weak
modifier does not make sense with them.
weak' cannot be applied to non-class type ' error type '
Is the button you are using placed in a local framework? if so try to rebuild the framework first, if you are not sure try next steps.
If you say that everybody else can run the project, it seems that your build folder got mess. You can try this steps in order to clean it
- Remove app from device
- In Xcode within your project press
cmd
+alt
+shift
+K
- Clean Derived data of the project (Go to window > Projects > Don't press Delete, press the arrow next to the path, remove all files and folders from there)
Now, when you will press start, a new and clean build will be made
Update
you mentioned you use pods to manage your project, do a pod outdated
- to check if there are any pods outdated, you are interested in the one with the button mostly, if so do an update on it
Alamofire: 'weak' cannot be applied to non-class type error
Paulw11 is correct. Because I am using static
, that's why self
does not make any sense. Low level mistake. Problem solved.
Related Topics
Type Conversion When Using Protocol in Swift
Can You Override Between Extensions in Swift or Not? (Compiler Seems Confused!)
Firebase Snapshot.Key Not Returning Actual Key
How to Use an Nsattributedstring with a Scrollview in Swiftui
How to Create Swift Class for Category
Uicollectionview - Resizing Cells on Device Rotate - Swift
How to Get Directory Size with Swift on Os X
Arkit: Placing an Scntext at a Particular Point in Front of the Camera
How Does One Trap Arithmetic Overflow Errors in Swift
What Is the Swift Syntax " .Bar" Called
Wkwebview Does Not Load Links to Pdfs
Swift Optional Inout Parameters and Nil
Unowned Vs. Weak. Why We Should Prefer Unowned
How to Loop Through View Outlets in a Uiviewcontroller with Swift
How to Use Alamofire with Custom Headers
Padding a Swift String for Printing
Dynamic Row Hight Containing Texteditor Inside a List in Swiftui
How We Can Get and Read Size of a Text with Geometryreader in Swiftui