如何覆盖swift类中的objective-c类的抽象类级别方法?

时间:2023-01-15 14:59:07

I have an Objective-c class level method as follows

我有一个Objective-c类级方法,如下所示

+(SObjectData *)createSObjectData:(NSDictionary *)soupDict{
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You Must override %@ in a sub class",NSStringFromSelector(_cmd)],userInfo:nil]
}

I want to override this method in a subclass of this in swift

我想用swift重写这个方法的子类

I tried the following

我尝试以下

override class func createSObjectData(soupDict:NSDictionary)->SObjectData
{
//some code
}

But its is giving me an error that

但它给了我一个错误

method does not override any method from super class

方法不覆盖超类中的任何方法

1 个解决方案

#1


1  

First fix syntax error in the method and make it look like

首先修复方法中的语法错误,使其看起来像。

+(SObjectData *)createSObjectData:(NSDictionary *)soupDict
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You Must override %@ in a sub class",NSStringFromSelector(_cmd)]userInfo:nil];
}

In your BaseClass.h

在你BaseClass.h

+(SObjectData *)createSObjectData:(NSDictionary *)soupDict;

In your BaseClass.m

在你BaseClass.m

+(SObjectData *)createSObjectData:(NSDictionary *)soupDict
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You Must override %@ in a sub class",NSStringFromSelector(_cmd)]userInfo:nil];
}

In your swift class

迅速在你的类

override class func createSObjectData(soupDict: [NSObject : AnyObject]!) -> SObjectData
{
    return SObjectData();
}

Update:

更新:

If your are sure the dictionary is never nil use NSDictionary * _Nonnull on the other had if NSDictionary can be nil use NSDictionary * _Nullable and update the swift code to soupDict: [NSObject : AnyObject] or soupDict: [NSObject : AnyObject]? respectively. In case NSDictionary is nil then use guard let for optional checking

如果你确定字典从来都不是nil使用NSDictionary * _Nonnull另一个如果NSDictionary可以是nil使用NSDictionary * _Nullable并将swift代码更新为soupDict: [NSObject: AnyObject]或soupDict: [NSObject: AnyObject]?分别。如果NSDictionary是nil,那么使用guard let进行可选检查

#1


1  

First fix syntax error in the method and make it look like

首先修复方法中的语法错误,使其看起来像。

+(SObjectData *)createSObjectData:(NSDictionary *)soupDict
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You Must override %@ in a sub class",NSStringFromSelector(_cmd)]userInfo:nil];
}

In your BaseClass.h

在你BaseClass.h

+(SObjectData *)createSObjectData:(NSDictionary *)soupDict;

In your BaseClass.m

在你BaseClass.m

+(SObjectData *)createSObjectData:(NSDictionary *)soupDict
{
    @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You Must override %@ in a sub class",NSStringFromSelector(_cmd)]userInfo:nil];
}

In your swift class

迅速在你的类

override class func createSObjectData(soupDict: [NSObject : AnyObject]!) -> SObjectData
{
    return SObjectData();
}

Update:

更新:

If your are sure the dictionary is never nil use NSDictionary * _Nonnull on the other had if NSDictionary can be nil use NSDictionary * _Nullable and update the swift code to soupDict: [NSObject : AnyObject] or soupDict: [NSObject : AnyObject]? respectively. In case NSDictionary is nil then use guard let for optional checking

如果你确定字典从来都不是nil使用NSDictionary * _Nonnull另一个如果NSDictionary可以是nil使用NSDictionary * _Nullable并将swift代码更新为soupDict: [NSObject: AnyObject]或soupDict: [NSObject: AnyObject]?分别。如果NSDictionary是nil,那么使用guard let进行可选检查