如何实现与另一个类同名的协议

时间:2023-01-05 16:45:59

In Objective C, the @interface and @protocol can have same names. How to create a class in Swift to adopt only the protocol?

在Objective C中,@ interface和@protocol可以具有相同的名称。如何在Swift中创建一个只采用协议的类?

Nimbus NICellFactory.h

@interface NICellObject : NSObject <NICellObject>

// Designated initializer.
- (id)initWithCellClass:(Class)cellClass userInfo:(id)userInfo;
- (id)initWithCellClass:(Class)cellClass;

+ (id)objectWithCellClass:(Class)cellClass userInfo:(id)userInfo;
+ (id)objectWithCellClass:(Class)cellClass;

@property (nonatomic, strong) id userInfo;

@end

How to adopt the protocol NICellObject without subclass class NICellObject

如何在没有子类NICellObject的情况下采用NICellObject协议

2 个解决方案

#1


5  

When you import the objective c class into swift, "Protocol" will be automatically append to the name of the protocol. So you should just adopt the XXXXXProtocol.

将目标c类导入swift时,“Protocol”将自动附加到协议名称。所以你应该采用XXXXXProtocol。

I just tried it in my local project. In your case NICellObjectProtocol.

我刚刚在我的本地项目中尝试过它。在你的情况下NICellObjectProtocol。

OLD ANSWER BEFORE EDIT

You take it wrong, they are two different thing with the same name. One is protocol named NICellObject, and a class also named NICellObject.

你弄错了,它们是同名的两个不同的东西。一个是名为NICellObject的协议,另一个名为NICellObject的类。

How to implement two protocols in swift with one protocol also is a class In Nimbus there is NICellObject which is a protocol and a class, see the code below. There must have many same cases.

如何使用一个协议在swift中实现两个协议也是一个类在Nimbus中有一个NICellObject,它是一个协议和一个类,请参阅下面的代码。必须有许多相同的案例。

From this link, https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html.

从此链接,https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html。

Some protocols group a number of unrelated methods (rather than create several separate small protocols). These protocols tend to be associated with a class that is the principal expression of the protocol. In these cases, the convention is to give the protocol the same name as the class.

一些协议将许多不相关的方法组合在一起(而不是创建几个单独的小协议)。这些协议往往与作为协议主要表达的类相关联。在这些情况下,惯例是为协议提供与类相同的名称。

And it is not possible to do this in Swift. I can't find the document saying so, but some hint From the language reference, declaration introduces a NEW name or contract into your program.

并且在Swift中不可能这样做。我找不到这样说的文件,但有些提示从语言参考中,声明会在您的程序中引入新名称或合同。

“A declaration introduces a new name or construct into your program. For example, you use declarations to introduce functions and methods, variables and constants, and to define new, named enumeration, structure, class, and protocol types. You can also use a declaration to extend the behavior of an existing named type and to import symbols into your program that are declared elsewhere.

“声明在您的程序中引入了新名称或构造。例如,您使用声明来引入函数和方法,变量和常量,以及定义新的命名枚举,结构,类和协议类型。您还可以使用声明来扩展现有命名类型的行为,并将符号导入到其他地方声明的程序中。

摘录来自: Apple Inc. “The Swift Programming Language”。 iBooks. https://itun.es/cn/jEUH0.l

摘要来自:Apple Inc.“The Swift Programming Language”.iBooks。 https://itun.es/cn/jEUH0.l

#2


1  

Just add a Protocol suffix to the protocol name, to distinguish the Class and the Protocol:

只需在协议名称中添加一个Protocol后缀,以区分Class和Protocol:

class A: BaseObject, NICellObjectProtocol { }

#1


5  

When you import the objective c class into swift, "Protocol" will be automatically append to the name of the protocol. So you should just adopt the XXXXXProtocol.

将目标c类导入swift时,“Protocol”将自动附加到协议名称。所以你应该采用XXXXXProtocol。

I just tried it in my local project. In your case NICellObjectProtocol.

我刚刚在我的本地项目中尝试过它。在你的情况下NICellObjectProtocol。

OLD ANSWER BEFORE EDIT

You take it wrong, they are two different thing with the same name. One is protocol named NICellObject, and a class also named NICellObject.

你弄错了,它们是同名的两个不同的东西。一个是名为NICellObject的协议,另一个名为NICellObject的类。

How to implement two protocols in swift with one protocol also is a class In Nimbus there is NICellObject which is a protocol and a class, see the code below. There must have many same cases.

如何使用一个协议在swift中实现两个协议也是一个类在Nimbus中有一个NICellObject,它是一个协议和一个类,请参阅下面的代码。必须有许多相同的案例。

From this link, https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html.

从此链接,https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html。

Some protocols group a number of unrelated methods (rather than create several separate small protocols). These protocols tend to be associated with a class that is the principal expression of the protocol. In these cases, the convention is to give the protocol the same name as the class.

一些协议将许多不相关的方法组合在一起(而不是创建几个单独的小协议)。这些协议往往与作为协议主要表达的类相关联。在这些情况下,惯例是为协议提供与类相同的名称。

And it is not possible to do this in Swift. I can't find the document saying so, but some hint From the language reference, declaration introduces a NEW name or contract into your program.

并且在Swift中不可能这样做。我找不到这样说的文件,但有些提示从语言参考中,声明会在您的程序中引入新名称或合同。

“A declaration introduces a new name or construct into your program. For example, you use declarations to introduce functions and methods, variables and constants, and to define new, named enumeration, structure, class, and protocol types. You can also use a declaration to extend the behavior of an existing named type and to import symbols into your program that are declared elsewhere.

“声明在您的程序中引入了新名称或构造。例如,您使用声明来引入函数和方法,变量和常量,以及定义新的命名枚举,结构,类和协议类型。您还可以使用声明来扩展现有命名类型的行为,并将符号导入到其他地方声明的程序中。

摘录来自: Apple Inc. “The Swift Programming Language”。 iBooks. https://itun.es/cn/jEUH0.l

摘要来自:Apple Inc.“The Swift Programming Language”.iBooks。 https://itun.es/cn/jEUH0.l

#2


1  

Just add a Protocol suffix to the protocol name, to distinguish the Class and the Protocol:

只需在协议名称中添加一个Protocol后缀,以区分Class和Protocol:

class A: BaseObject, NICellObjectProtocol { }