Interview questions from the Protocol feature in Swift
In an interview, when discussing the features of Swift with the interviewer, we talked about the Protocol of Swift.
The Protocol in Swift can be written to its Extension method.
If the Protocol class or structure is not overridden for Protocol methods, the default method of Extension in Protocol is called.
This is easy to understand in code, as you can see from the previous code:
protocol Chef {
func makeFood()
}
extension Chef {
func makeFood() {
print("make food")
}
}
class A: Chef {
func makeFood() {
print("A food")
}
}
class B: Chef {
}
let a = A()
let b = B()
a.makeFood()
b.makeFood()
Copy the code
The following logs are displayed
A food
make food
Copy the code
“What if I wanted Protocol in OC to have features similar to Protocol Extension in Swift?”
That was the question my interviewer asked me.
The soul of Protocol in OC
Protocol in OC can use @Request and @optional to implement mandatory and optional implementations in compliant classes, but what if you want a class that calls the default implementation that complies with the Protocol?
After thinking for a long time, I found it in an accidental coding.
Let’s first create a protocol and create the file ClassNameConvertible. H
@protocol ClassNameConvertible <NSObject>
+ (NSString *)className;
- (NSString *)className;
@end
Copy the code
It’s easy to see what this protocol is trying to do. All it’s trying to do is get the string of the class name. I don’t write OC very often, so I’m missing some important details about OC.
Q: What types of protocols can be complied with in OC?
A: Objects!
Look at the format for defining an OC protocol
@protocol Protocol name <NSObject> Some methods...... @endCopy the code
You know when you create it.
That is, the types that comply with the protocol must all be objects, and what is the base class of objects?
NSObject is!
If NSObject follows protocol A defined, and implements the default methods of protocol A, any other class will follow protocol A, and once it calls protocol A’s methods, other classes will call protocol A’s default methods in NSObject if they don’t override protocol A’s methods.
How do I make NSObject comply with the defined protocol A?
You can create A class of NSObject to comply with protocol A!
That solves the above problem.
How to implement default Extension in Protocol in OC
To create a class for NSObject+ClassName:
.h files
#import <Foundation/Foundation.h>
#import "ClassNameConvertible.h"
NS_ASSUME_NONNULL_BEGIN
@interface NSObject (ClassName)<ClassNameConvertible>
@end
NS_ASSUME_NONNULL_END
Copy the code
.m files
#import "NSObject+ClassName.h"
@implementation NSObject (ClassName)
+ (NSString *)className {
return NSStringFromClass(self);
}
- (NSString *)className {
return [[self class] className];
}
@end
Copy the code
And then we create an arbitrary SomeView class that inherits from UIView
H and.m files
NS_ASSUME_NONNULL_BEGIN
@interface SomeView : UIView
@end
NS_ASSUME_NONNULL_END
Copy the code
@implementation SomeView
@end
Copy the code
Finally, let’s test the code:
SomeView *someView = [SomeView new]; // call NSString *instaceName = [someView className]; NSString *className = [SomeView className]; NSLog(@"instaceName:%@", instaceName); NSLog(@"className:%@", className); If ([someView conformsToProtocol:@protocol(ClassNameConvertible)]) {NSLog(@"someView complies with ClassNameConvertible Agreement "); }Copy the code
Print logs:
InstaceName :SomeView className:SomeView SomeView complies with the ClassNameConvertible protocolCopy the code
Some would say that adding a ClassNameConvertible protocol to the classification of NSObject is essentially extending the system method, and that’s too expensive.
You don’t have to comply with protocol A in the class of NSObject, you can comply with protocol A in the class of specialized classes that you define.
In this way, the default implementation of Protocol Extension is implemented, and the impact force is in your own hands. And you have a class that obeys the protocol.
Once this idea is extended, the idea of generic classes in OC becomes clearer.
Well, that may be a false proposition.
This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign