How does one go about making a delegate, i.e. NSUserNotificationCenterDelegate
in swift?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
- Swift - Snapshotting a view that has not been rend
Delegates always confused me until I realized that a delegate is just a class that does some work for another class. It's like having someone else there to do all the dirty work for you that you don't want to do yourself.
I wrote a little story to illustrate this. Read it in a Playground if you like.
Once upon a time...
In review, there are three key parts to making and using the delegate pattern.
Real life
In comparison to our Bossy Big Brother story above, delegates are often used for the following practical applications:
The great part is that these classes don't need to know anything about each other beforehand except that the delegate class conforms to the required protocol.
I highly recommend reading the following two articles. They helped me understand delegates even better than the documentation did.
One more note
Delegates that reference other classes that they do not own should use the
weak
keyword to avoid strong reference cycles. See this answer for more details.In swift 4.0
Create a delegate on class that need to send some data or provide some functionality to other classes
Like
After that in the class that going to confirm to this delegate
Here's a gist I put together. I was wondering the same and this helped improve my understanding. Open this up in an Xcode Playground to see what's going on.
DELEGATES IN SWIFT 2
I am explaining with example of Delegate with two viewControllers.In this case, SecondVC Object is sending data back to first View Controller.
Class with Protocol Declaration
In First ViewController Protocol conforming is done here:
Protocol method definition in First View Controller(ViewController)
During push the SecondVC from First View Controller (ViewController)
The solutions above seemed a little coupled and at the same time avoid reuse the same protocol in other controllers, that's why I've come with the solution that is more strong typed using generic type-erasure.
output: got new value newValue
I got few corrections to post of @MakeAppPie
First at all when you are creating delegate protocol it should conform to Class protocol. Like in example below.
Second, your delegate should be weak to avoid retain cycle.
Last, you're safe because your protocol is an optional value. That means its "nil" message will be not send to this property. It's similar to conditional statement with
respondToselector
in objC but here you have everything in one line:Above you have an obj-C example and below you have Swift example of how it looks.