Override Objective-C class method in Swift - ios

I'm trying to use JSONModel in a Swift project.
I would like to override the method keyMapper from JSONModel but I don't find how to override an Objective-C class method in the model class.
The signature of the method is:
+(JSONKeyMapper*)keyMapper;
How can I do that?

You do it just like you override an instance method, except with the class keyword:
override class func keyMapper() -> JSONKeyMapper! {
//code here
}

Related

Use generic extended swift class in Objective-c

In my project i have many swift classes that extend generic classes. And Now i'm in a need to send data from objective-c class to these swift classes. Is there any possible way to do this ?
I have tried using #objc before class as shown below
but it didn't allow me to do this.
I followed this answer stack link. But i checked when i run my app with this code it dint create the variable in the Project-swift class as shown in below image
as the variable is not created here so it gives error while calling the variable in objective-c class.
Kindly help out with better solution.
I got my solution by creating another class which contain a protocol and i indirectly call the generic extended class.
class EntityDataTable :BaseData<EntitySw> {
}
this is the generic extended class
and the another class i made to indirectly access this class in objective-c is
#objc class EntityService : NSObject {
var entityProtocol:EntityDataProtocol?
override init() {
super.init()
entityProtocol = returnDataObject()
}
func returnDataObject() -> EntityDataTable {
return EntityDataTable()
}
}
#objc protocol EntityDataProtocol{
func addFromService(source: CWEntity)
func getAll() -> [EntitySw]
}
Then i imported the Project-swift.h file in objective c class and made the object of swift class

How to override a oc category method in .swift

I have a OC UIViewController category , which decleared a class method of UIViewController
+(BOOL)hasStoryBoard;
And I also import this category into xxx-Bridging-Header.h file
My swift class CustomVC is inherited UIViewController. I want override +(BOOL)hasStoryBoard to give a BOOL-type value.This method will called in other class to determine CustomVC's new instance's some feature.
But in my CustomVC.swift I cannot find method like this:
override class func hasStoryBoard()->bool{}
I must override this method to give a YES,or give it's subclass a NO,so on...
I searched this site and find this Swift: How to call a category or class method from Objective-C. In this discussion , explained and tell you how to call the method,not how to override the method .
Can you find solution to override oc category's class method? Or give a workaround in my case.
All my code is :
#interface UIViewController (StoryBoard)
+(BOOL)hasStoryBoard;
-(void)haha;
#end
.
#implementation UIViewController (StoryBoard)
static BOOL hasStoryboard = NO;//默认没有
+(BOOL)hasStoryBoard{
return hasStoryboard;
}
-(void)haha{}
#end
.
class CustomVC: UIViewController {
//this override is correct,have syntax input prompt
override func haha() {
}
//this override is incorrect ,no syntax input prompt
override func hasStoryBoard()->Bool{
return true
}
}
+(BOOL)hasStoryBoard;
defines a class method in Objective-C, but in your Swift subclass
you define an instance method:
override func hasStoryBoard()->Bool{
return true
}
That must be a class method as well:
override class func hasStoryBoard()->Bool{
return true
}
Tip: If you open the Objective-C interface (.h) file in Xcode and
select "Navigate->Jump to Generated Interface" from the Xcode menu
then you'll see excacly how the Objective-C interface is imported to
Swift. In your case:
extension UIViewController {
open class func hasStoryBoard() -> Bool
open func haha()
}
Now you can copy the function definition and use it to define
the overriding method in your subclass.

Override function

I am taking a iOS course online provided by a famous university. I don't understand why the following code use override and it is legal.
According to the official definition, we use override to override superclass' methods. Where is the subclass and superclass in the following code?
What's been override and by what?
public override var description: String {
return "\(url.absoluteString) (aspect ratio = \(aspectRatio))"
}
Here is an example:
Your original class:
class Person {
func walk() {
//do something
}
}
Your subclass:
class Runner: Person {
override func walk() {
//do something that is different from Person's walk
}
}
In the Runner class, there is an override with the function walk. That is because it is a subclass of Person, and it can override Person's walk function. So If you instantiate a Runner:
var usainBolt = Runner()
And you call the walk function:
usainBolt.walk()
Then that will call the overriden function that you wrote in the Runner class. If you don't override it, it will call the walk function that you wrote in Person.
According to the official definition, we use override to override superclass' methods.
That's correct. The superclass in your example is the class that encloses the override of description property. This could be NSObject, some other class derived from it (directly or indirectly), or some class unrelated to NSObject that has var description: String property.
description is a property that Swift classes commonly have as a way to present themselves as a string, because description provides conformance to CustomStringConvertible protocol. This is similar to toString() method of Java, and to str() method of Python.
What's been override and by what?
The implementation of the property is what's being overridden. The class that has the implementation does the overriding.

Swift: Unable to override operationDidFinish from GroupOperation class

In a custom subclass of GroupOperation, I'm trying to override operationDidFinish(). When I attempt to implement the function in my subclass, I get this error message:
Method does not override any method from its superclass
If I remove the override keyword, I get
Method 'operationDidFinish(:withErrors:)' with Objective-C selector
'operationDidFinish:withErrors:' conflicts with method
'operationDidFinish(:withErrors:)' from superclass 'GroupOperation'
with the same Objective-C selector
Weirdness. I'm pretty sure my method signature is spot on, and I'm not trying to overload an obj-c method, so all should be well. What gives?
For reference, my class looks like this:
class ServerAuthenticationOperation: GroupOperation {
// properties... initializer stuff...
override func operationDidFinish(operation: NSOperation, withErrors errors: [NSError]) {
print("I finished!")
}
}
I assume you're using Swift 2.
Objective-C does not support method overloading, so you have to select a different name for your method. Or, you can try these options:
Rename the method using the #objc(newMethodName:)
Use #nonobjc
Edit:
It seems working for the repo you provided, you can check it here. https://www.dropbox.com/s/hb07u3hyjhjuews/OverrideTest.zip?dl=0

Override Objective-C class method with "new" prefix in Swift

I have a problem with overriding class method from Objective-C in Swift. I can invoke it from Swift no problem but not override it.
Method signature:
+ (id<MyInterface>)newObjectWithId:(NSString *)objectId properties:(NSDictionary *)properties andClient:(Client *)client;
Class hierarchy:
NSObject <-- MyObject1 <-- MyObject2 <-- MySwiftClass
MyObject1 declares and implements aforementioned method and so does MyObject2. However trying to override it in Swift does not work. It is not visible in autocompletion and when I write it manually it says Method does not override any method from its superclass. Signature I wrote looks like this:
override class func newObjectWithId(objectId: String!, properties: NSDictionary!, andClient client: Client!) -> MyInterface
I did overriding for some other class methods in this class too and they work without a problem. I assume it's a problem with new prefix in this method. Has anyone encountered something like this and resolved it successfully?

Resources