Vyacheslav
Vyacheslav

Reputation: 27221

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

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?

Upvotes: 0

Views: 161

Answers (2)

Rob Napier
Rob Napier

Reputation: 299575

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).

Upvotes: 2

try this one: [group objectAtIndexedSubscript:idx]

Upvotes: -1

Related Questions