Swift Advanced - Setting extensions during run time - ios

Is there a way to set extensions during run time in Swift?
I got a protocol named "CuteProtocol" and an extension to that protocol named "CuteExtension". When I want to add this extension to classes I just do as follows:
class CuteClass: UIViewController, CuteProtocol {
}
However I have many of these classes which should implement this protocol and I don't want to add them one by one, I don't want to make a base class either.
Is there a way to set extensions-protocols during run time as follows:
let cuteClass = CuteClass()
cuteClass. // ADD EXTENSION-PROTOCOL SOMEHOW HERE.

No, but you can extension for example UIViewController, or other base class
extension UIViewController: CuteProtocol {
// your code conforming to Cute Protocol goes here.
}

you can play with runtime modifiers in ObjC freely, but in Swift such kinda pattern is not really common.
NOTE: you can find more information about this in the ObjC Runtime Library, if you are interested.

You can not set extensions in runtime, BUT you don't have to set them in class definition either. you can extend classes like:
extension CuteClass: CuteProtocol {
// your code conforming to Cute Protocol goes here.
}
Update 1
Or if you want to have default implementations to CuteProtocol, you can extend CuteProtocol itself too:
protocol CuteProtocol {
func f1()
}
extension CuteProtocol {
func f1() {
// default implementation
}
}
This way each class can change f1 implementation if want to or use the default implementation.
You can even add conditional extensions like:
extension CuteProtocol where Self: CuteClass {
func f1() { }
func f2() { }
}
so if you write extension CuteClass: CuteProtocol {} all CuteClass instances and subclasses will have access to method f1 and f2.
But be-aware that functions added in extensions support dynamic-dispatch IFF they're defined in protocol.
In the sample I provided, f1 will be called with dynamic-dispatch but f2 will be called with static-dispatch. i.e: if CuteChildClass: CuteClass changes f2 implementation and you call f2 from a CuteProtocol variable, the code you provided in the extension will be called.

Related

Is it possible to add condition where protocol is confirmed by either of class giving multiple options in where clause Self

lets suppose we have
protocol xyz {
}
class A: xyz {
}
class B: xyz {
}
class C: xyz{
}
extension of xyz is called only if the instance is of type either A or B class
extension xyz where Self: A, B {
}
So you want to define an extension to protocol XYZ when Self is A or B right?
The answer is: you can’t.
However
You can achieve a similar result doing something like this
protocol XYZ { }
protocol AOrB: XYZ { }
class A: AOrB { }
class B: AOrB { }
class C: XYZ { }
extension XYZ where Self: AOrB {
func foo() { }
}
A().foo()
B().foo()
You can only specify one class (either A or B) but not both, because Swift doesn't support multiple inheritance.
A class can only extend one class. So extension xyz where Self: A, B both A and B are classes. You can't write an extension like that. Have a common conformation protocol for A and B. And write the extension for that type.
In order for this extension to be implemented, either there is some shared interface that both A and B provide that you plan to use, or you plan to do some kind of type-checking inside of the functions.
If the former, then that's a protocol, and that's what you should be writing to, rather than two specific classes. Several answers here address that case. Create a protocol that covers the interface you need, and retroactively conform A and B to it by just saying they conform:
protocol MyProtocol {
// ... various methods that A already has ...
}
extension A: MyProtocol {}
Now, A conforms to MyProtocol.
If you're planning to do things like type(of:) to implement this extension, I strongly encourage you not to do that. Instead, create a protocol that provides the interface you want to use in your extension, and then conform A and B to it with extensions:
protocol MyProtocol {
// ... various methods that A doesn't have ...
}
extension A: MyProtocol {
// ... implementations that bridge A to MyProtocol ...
}
In either case, you can then apply your extensions where Self: MyProtocol.
Alternately, if the conditional type(of:) code would be very different between A and B, then you can mix these approaches, and include class-specific code in extensions marked where Self: A and where Self: B.
The point is to think about the interfaces you need rather than focusing on "exactly one of these two classes."

Hide logic that class conforms to protocol

Are there any ways to hide that class conforms to some protocol? Like in Objective-C - just used to add Protocol in .m file and other classes (from another files) didn't see it.
For example. I have a test cell which has a textfield. I want to hide, that this cell conforms to protocol. Something like that:
class TestCell: UITableViewCell {
}
fileprivate extension TestCell : UITextFieldDelegate {
}
But compiler swears me. Any elegant solution?
This capability has been stated by the Swift team as "unlikely" to be implemented. Here is the original thread about it: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160229/011666.html
The specific statement about this particular scenario was:
Private conformances
Right now, a protocol conformance can be no less visible than the
minimum of the conforming type’s access and the protocol’s access.
Therefore, a public type conforming to a public protocol must provide
the conformance publicly. One could imagine removing that restriction,
so that one could introduce a private conformance:
public protocol P { }
public struct X { }
extension X : internal P { … } // X conforms to P, but only within this module
The main problem with private conformances is the interaction with
dynamic casting. If I have this code:
func foo(value: Any) {
if let x = value as? P { print(“P”) }
}
foo(X())
Under what circumstances should it print “P”? If foo() is defined
within the same module as the conformance of X to P? If the call is
defined within the same module as the conformance of X to P? Never?
Either of the first two answers requires significant complications in
the dynamic casting infrastructure to take into account the module in
which a particular dynamic cast occurred (the first option) or where
an existential was formed (the second option), while the third answer
breaks the link between the static and dynamic type systems—none of
which is an acceptable result.

How to define optional methods in a Swift protocol with Swift parameters?

TL;DR
How can I define optional methods in a swift protocol that accept custom typed parameters?
The case
I understand that the way to define optional methods in a swift protocol is using the #objc flag. Like so:
#objc protocol someSweetProtocol {
optional func doSomethingCool()
}
But when I wanted to use my custom type for the parameter, like so:
#objc protocol someSweetProtocol {
optional func doSomethingCool(🚶🏼: HSCoolPerson)
}
I got this error:
Which is not cool.
How can this problem be solved? Also can you explain why this is happening?
Addendum
This is how HSCoolPerson is defined:
class HSCoolPerson {
...
...
...
}
Nothing special there...
The problem is this:
class HSCoolPerson {
// ...
}
As the error message plainly tells you, that type, the class HSCoolPerson, is completely invisible to Objective-C. And a protocol optional method is an Objective-C language feature; Swift merely borrows it, as it were. (That's why you have to say #objc protocol to get this feature.) So any time you want to define a protocol optional method, you have to do it in a way that Objective-C can understand, because it is Objective-C that is going to do the work for you.
To expose this class to Objective-C, simply derive it from NSObject:
class HSCoolPerson : NSObject {
// ...
}
Problem solved.
Put the default implementations in an extension, like so:
class HSCoolPerson {}
protocol SomeSweetProtocol {
func doSomethingCool()
}
extension SomeSweetProtocol {
func doSomethingCool(🚶🏼: HSCoolPerson) {
// default implementation here
}
}
class SomeSweetClass: SomeSweetProtocol {
// no implementation of doSomethingCool(_:) here, and no errors
}
you can declare func like that in protocol
#objc optional func doSomethingCool(🚶🏼: HSCoolPerson)

Swift Declare Class Func in Protocol

I had following confusion. As far as I know the main difference between static and class keywords when declaring method is that the second one could be overridden in subclasses.
The problem
However when I declare a protocol in Swift 1.2 like this:
protocol MyProtocol
{
class func dummyClassMethod()
}
compiler gives an error:
Class methods are only allowed within classes; use 'static' to declare
a static method
The error is pretty descriptive as obviously MyProtocol is not a class, however I want to make a class func part of the protocol.
What I've tried
I've found that if I declare interface in protocol as static, compiler is happy and I could use this static method in all classes that adopt this protocol:
protocol MyProtocol
{
static func dummyClassMethod()
}
The question
So my question basically is is this right? This declaration states that my class method cannot be overridden in children, however in my implementation I could write and use the following:
class ClassA: MyProtocol
{
class func dummyClassMethod() {
}
}
class ClassB: ClassA
{
override class func dummyClassMethod() {
}
}
and now my dummyClassMethod is not static anymore...
Compiler is Ok and everything works - but why?
Is it specific to the fact that interface itself is static, however
it's implementation is not?
Is there a better alternative for class func in protocols?
Objective-C solution
In ObjC this is pretty easy and compile & run flawlessly:
#protocol MyProtocol
+(void)dummyClassMethod;
#end
You can review Apple's Documentation (subsection Method Requirements).
There says:
As with type property requirements, you always prefix type method requirements with the static keyword when they are defined in a protocol. This is true even though type method requirements are prefixed with the class or static keyword when implemented by a class
In practice, You can do it as follow:
First, declare your protocol:
protocol SomeProtocol {
static func someMethod()
}
Then, in your class you've 2 options:
First:
class SomeClass : SomeProtocol {
class func someMethod()
}
Second:
class SomeClass : SomeProtocol {
static func someMethod()
}
I hope, this may clarify your doubt..
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html
A protocol defines a blueprint of methods, properties, and other
requirements that suit a particular task or piece of functionality.
The protocol doesn’t actually provide an implementation for any of
these requirements—it only describes what an implementation will look
like. The protocol can then be adopted by a class, structure, or
enumeration to provide an actual implementation of those requirements.
After this protocol definition it becomes reasonable that
As with type property requirements, you always prefix type method
requirements with the static keyword when they are defined in a
protocol. This is true even though type method requirements are
prefixed with the class or static keyword when implemented by a class...
To make protocol method static and final implement that method with static keyword
class ClassA: MyProtocol{
static func dummyClassMethod() {
}
}
and now you cant override dummyClassMethod function anymore. If you want to prevent overriding only you must declare protocol method as final. About class functions, they were not fully supported in Swift 1.0 and now in Swift 1.2 I think that they are moving towards static functions

Check if optional protocol method is implemented in Swift?

I have a swift protocol:
#objc protocol SomeDelegate {
optional func myFunction()
}
I one of my classes I did:
weak var delegate: SomeDelegate?
Now I want to check if the delegate has myFunction implemented.
In objective-c I can do:
if ([delegate respondsToSelector:#selector(myFunction)]) {
...
}
But this is not available in Swift.
Edit: This is different from: What is the swift equivalent of respondsToSelector? I focus on class protocols not on classes.
How do I check if my delegate has an optional method implemented?
Per The Swift Programming Language:
You check for an implementation of an optional requirement by writing
a question mark after the name of the requirement when it is called,
such as someOptionalMethod?(someArgument). Optional property
requirements, and optional method requirements that return a value,
will always return an optional value of the appropriate type when they
are accessed or called, to reflect the fact that the optional
requirement may not have been implemented.
So the intention is not that you check whether the method is implemented, it's that you attempt to call it regardless and get an optional back.
You can do
if delegate?.myFunction != nil {
}
I've found it successful to add an extension to the protocol that defines basic default implementation and then any class implementing the protocol need only override the functions of interest.
public protocol PresenterDelegate : class {
func presenterDidRefreshCompleteLayout(presenter: Presenter)
func presenterShouldDoSomething(presenter: Presenter) -> Bool
}
then extend
extension PresenterDelegate {
public func presenterDidRefreshCompleteLayout(presenter: Presenter) {}
public func presenterShouldDoSomething(presenter: Presenter) -> Bool {
return true
}
}
Now any class needing to conform to the PresenterDelegate protocol has all functions already implemented, so it's now optional to override it's functionality.
I normally implement it like this:
self.delegate?.myFunction?()
if the delegate methods returns a value:
var result = defaultValue
if let delegateResult = self.delegate?.myFunction?() else {
result = delegateResult
}
//do something with result
Declaration
#objc public protocol nameOfDelegate: class {
#objc optional func delegateMethod(_ varA: int, didSelect item: Item)
}
Implimetation
if let delegate = nameOfDelegate {
delegate.delegateMethod?(1, didDeselect: node)
}
I know this question is 5 years old, but I would like to share what I found. My solution works as of 2021, XCode 11+, Swift 5.
Say I wanted to figure out whether the function sign follows the GIDSignInDelegate protocol and also know what all the optional functions for GIDSignInDelegate are.
I have to look at the source code of the GIDSignIn module, and this is how.
Click on jump to definition on the main module that is imported. It will lead to a file like this:
Copy the entire line, import GoogleSignIn.GIDSignIn and paste it in the ViewController or whatever .swift file (doesn't really matter).
Within the swift file, right click on the GIDSignIn part of the import line GoogleSignIn.GIDSignIn and jump to definition. This will lead you to the actual module with all the available functions (the functions not marked optional may be stubs, which are required functions in the delegate protocol):
From this file, I can see that there is a sign function that is a stub of GIDSignInDelegate and an optional sign function that is implemented as a method overload.
I used this for GIDSignInDelegate, but you can use the same method to figure out whether any function follows any delegate protocol.

Resources