[英]How to override an abstract class level method of an objective-c class in a swift class?
我有一個Objective-c類級別方法如下
+(SObjectData *)createSObjectData:(NSDictionary *)soupDict{
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You Must override %@ in a sub class",NSStringFromSelector(_cmd)],userInfo:nil]
}
我想在swift的子類中重寫此方法
我嘗試了以下內容
override class func createSObjectData(soupDict:NSDictionary)->SObjectData
{
//some code
}
但它給了我一個錯誤
方法不會覆蓋超類中的任何方法
首先修復方法中的語法錯誤並使其看起來像
+(SObjectData *)createSObjectData:(NSDictionary *)soupDict
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You Must override %@ in a sub class",NSStringFromSelector(_cmd)]userInfo:nil];
}
在你的BaseClass.h中
+(SObjectData *)createSObjectData:(NSDictionary *)soupDict;
在你的BaseClass.m中
+(SObjectData *)createSObjectData:(NSDictionary *)soupDict
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You Must override %@ in a sub class",NSStringFromSelector(_cmd)]userInfo:nil];
}
在你的快速課堂上
override class func createSObjectData(soupDict: [NSObject : AnyObject]!) -> SObjectData
{
return SObjectData();
}
更新:
如果您確定字典永遠不會使用NSDictionary * _Nonnull
另外如果NSDictionary可以使用NSDictionary * _Nullable
並將swift代碼更新為soupDict: [NSObject : AnyObject]
或soupDict: [NSObject : AnyObject]?
分別。 如果NSDictionary為nil,則使用guard let
進行可選檢查
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.