Expected method to read array element not found on

2019-08-31 22:31发布

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?

2条回答
家丑人穷心不美
2楼-- · 2019-08-31 23:04

try this one: [group objectAtIndexedSubscript:idx]

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-31 23:18

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

查看更多
登录 后发表回答