Compiler error when assigning the Delegate for a Protocol in Swift iOS
The Known Issues section of the Release Notes says:
You cannot conditionally assign to a property of an optional object.
(16922562)For example, this is not supported:
let window: NSWindow? = NSApplication.sharedApplication.mainWindow
window?.title = "Currently experiencing problems"
So you should do something like if let realObject = object { ... }
In Swift, does an object conforming to a protocol absolutely need a delegate variable in order to work with the protocol?
If the object already conforms to the protocol by implementing the variables and/or methods, then what is the reason for creating a variable called delegate and setting the type to that of the protocol?
The whole purpose of the protocol in the protocol-delegate pattern is that the only thing this class, which is going to be sending delegate messages to the delegate, needs to know or care about, is that the delegate is an adopter of that protocol — i.e., that it implements the variables / methods. This class doesn't need to know the actual class of the delegate; it just needs to know that that the delegate can be sent the delegate messages.
Thus, it's all about the compiler. The object acting as delegate may conform to the protocol, but the compiler doesn't know that unless this variable is typed as a protocol-adopter. And if the compiler doesn't know, it won't let us send delegate messages to the delegate object! So that's how we type it. That's the minimum the compiler needs to know in order to allow us to send the delegate messages.
Assigning and using protocol-typed variable to a member of a class
delegates and protocols in ios are used for callbacks just like interfaces in java if you are familiar with java programming language. So a protocol contains some methods which needs to be implemented by all the classes that confirm to that protocol and the delegate variable is used to call those methods from the above mentioned protocol.
The delegate variables are generally optionals because there might be a case where no object confirms to the protocol and the delegate variable could be nil.
Delegates are used to call the methods from protocols so it is necessary that it be the type of the protocol
If all the non optional methods in swift are initialised in the init before calling super.init() then the compiler has no problems
By assigning delegate to self the object declares that it implements ColorServiceManagerDelegate.
Hope this helps.
expected type while implementing delegate
Add the following at the top. Since the protocol TopBarDelegate
is defined above the class TopBarViewController
, at the point you define the protocol, the compiler doesn't know there is a class called TopBarViewController
. This line tells it there really is a class with that name defined somewhere.
@class TopBarViewController;
getting a warning setting up delegate for a custom protocol
You are running into an issue caused by an ambiguity in how Objective-C finds a matching selector and dealing with an id
reference.
UIStoryboardSegue destinationViewController
returns an id
. Your code then tries to call the setDelegate
method on this id
reference. Since there is no information about what this id
actually references, it doesn't know which setDelegate:
method you might mean (there are many). So the compiler scans through the list it knows of and picks one. In this case it chose the setDelegate:
method from the NSFileManager
class. Since self
doesn't conform to the NSFileManagerDelegate
protocol, you get the warning.
You could ignore the warning and your code will work fine in this case.
The better solution is to help the compiler by adding a cast:
[(SomeSecondCustomViewController *)segue.destinationViewController setDelegate:self];
This will let the compiler know which setDelegate:
method you really mean.
BTW - Adding NSFileManagerDelegate
to your class is not a valid solution even if it works at the moment. A simple reordering of some import
statements could lead the compiler to make a different choice and your warning would return but complain about not conforming to some other protocol.
Setting a delegate generates a compile error
The issue is that when you store something as a Protocol
, even if it is a class, swift considers them to be a value
type, instead of the reference
type you are expecting them to be. Therefore, no part of it is allowed to be changed. Take a look at this reference for more information.
Protocols and delegate trouble in swift
More than likely delegate is nil. Add a breakpoint and check the value of delegate before that line executes. Or change the "?" to a "!" and it will crash if delegate is nil, letting you know what's wrong.
Your code in the other class:
var delegate:PayButtonProtocol?
defines a variable named delegate that is of type PayButtonProtocol?
.
The variable delegate
will contain nil until you assign something to it:
delegate = <someObjectThatConformsToPayButtonProtocol>
Related Topics
Xcode 6 & Swift: Black Bars Appear Above and Below the Viewcontroller on iOS 7 iPhone 5 Device
Memory Usage Keeps Rising on Older Devices Using Metal
Using Haneke to Cache Then Play Mp4 Files with Avplayer
How to Access My Swift Classes from My UI Tests
iOS - Send Image to Instagram - Documentinteraction
Storyboard Reference to Cocoapods Storyboard Seems Broken
How Parsing Firebase Fdatasnapshot JSON Data in Swift
How to Give Cornerradius for Uibezierpath
Converting an Existing Project into Customizable Framework
Errortype' Is Not Convertible to 'Nserror'
Rotate Image in Share Extension
Show Bounding Box While Detecting Object Using Arkit 2
How to Convert This Opengl Pointer Math to Swift
App Groups Forsecurityapplicationgroupidentifier Returns Nil
How to Fill a Circle Color by Percentage Value
Setcollectionviewlayout' Animation Broken When Also Changing Collection View Frame