How to implement a protocol that has the same name as another class

In Objective C, @interface and @protocol can have the same name. How do I create a class in Swift to accept protocol only?

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 NICellObject protocol without subclassing the NICellObject class

+3


source to share


2 answers


When you import objectclass c into swift, "Protocol" will be automatically appended to the protocol name. So you should just accept XXXXXProtocol.

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

OLD ANSWER BEFORE CHANGE

You are wrong, they are two different things with the same name. One of them is the NICellObject protocol as well as the NICellObject class.

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



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

Some protocols group together a number of unrelated methods (rather than creating several separate small protocols). These protocols are usually associated with a class that is the main expression of the protocol. In these cases, the convention must provide the protocol with the same name as the class.

And it cannot be done in Swift. I can't find a document saying this, but some clues. By using a language reference, the ad introduces a new name or contract into your program.

"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 a new named enum, structure, class, and protocol types. You can also use a declaration to extend the behavior of an existing named type and import symbols into your program that are declared elsewhere.

"

摘录 来自: Apple Inc. "Fast programming language". iBooks. https://itun.es/cn/jEUH0.l

+5


source


Just add a suffix Protocol

to the protocol name to distinguish between Class and Protocol :



class A: BaseObject, NICellObjectProtocol { }

      

+1


source







All Articles