Expected method to read array element not found on object of type 'id<ProtocolName>'

不羁的心 提交于 2020-01-16 07:49:07

问题


I want to update XCode version.

But while updating I receive several compiler errors. The are the same as this:

id<MyProtocol> objectToDelete = group[index.unsignedIntegerValue];

Expected method to read array element not found on object of type 'id'

id<MyProtocol> _Nonnull group

@protocol MyProtocol <NSObject>

@property (copy, nonatomic) NSString* name;
@property (copy, nonatomic) NSString* id;
@property (copy, nonatomic) NSString* internalType;
@property (strong, nonatomic) NSMutableArray<id<SomeAnotherProtocol>>* objects;

- (instancetype)initWithObject:(MyProtocol*)object;
// Search
- (BOOL)isContainsObjectWithID:(NSString*)myID;
- (NSUInteger)indexForObjectID:(NSString*)myID;
- (id<SomeAnotherProtocol>)objectWithID:(NSString*)myID;
- (NSString*)groupID;

@end

This error appears only on XCode 9.3 version.

Does id<MyProtocol> objectToDelete = ((NSArray *) group)[index.unsignedIntegerValue]; the only solution?


回答1:


First, I'm assuming that group is not actually an NSArray, but is in fact:

id<MyProtocol> _Nonnull group

This says that group is "some object that conforms to MyProtocol". Nothing in MyProtocol says that this object can be subscripted. If you want it to be subscriptable by indexes, then you need to say this in the protocol:

@protocol MyProtocol <NSObject>
- (id<MyProtocol>)objectAtIndexedSubscript:(NSUInteger)idx;
...

And you of course need to implement objectAtIndexedSubscript: in anything that conforms (but this seems to already be the case, since it works when you cast it).




回答2:


try this one: [group objectAtIndexedSubscript:idx]



来源:https://stackoverflow.com/questions/49627298/expected-method-to-read-array-element-not-found-on-object-of-type-idprotocolna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!