我想創建一個訪問我的web api的通用存儲庫(模式)。我無法理解協議如何在objective-c中工作(我來自c#,其中接口有點不同)。Objective-C協議作爲另一種協議的參數
我想要做的是將ProtocolA作爲另一個ProtocolB中的參數,然後在ProtocolA上實現ProtocolB訪問方法,因爲傳遞給ProtocolB的對象必須實現ProtocolA本身。我正確地思考這個問題嗎?
這是我迄今爲止,但似乎無法得到它的工作 - 也許我的邏輯是錯的:
//PGenericModel.h
@protocol PGenericModel <NSObject>
- (void)testMethod;
@end
//PGenericRepository.h
#import "PGenericModel.h"
@protocol PGenericRepository <NSObject>
@required
- (void)Get:(id<PGenericModel>*)entity;
@end
//GenericRepository.m
#import "GenericRepository.h"
@implementation GenericRepository
- (void)Get:(id<PGenericModel>*)entity
{
//GET
[entity testMethod] <-- this doesn't work...
}
@end
即使你來自C#,請不要在Obj-C中使用C#命名約定。例如,所有方法名稱應以小寫字母開頭並描述它們的參數,例如, 'getForEntity:' – Sulthan
@蘇丹指出,謝謝! – crizzwald