gpt4 book ai didi

ios - NSMutableArray 新协议(protocol)类型

转载 作者:行者123 更新时间:2023-11-28 19:25:49 26 4
gpt4 key购买 nike

  1. 如何摆脱警告而不使用 [@[] mutableCopy]:

//Incompatible pointer types assigning to 'NSMutableArray *' from 'NSMutableArray *'

@protocol Coordinatorable <NSObject>

@property (nonatomic, strong, readonly) UINavigationController *presenter;
@property (nonatomic, strong, readonly) NSMutableArray<Coordinatorable>*childCoordinators;

@end


self.childCoordinators = [[NSMutableArray alloc] init]; //Incompatible pointer types assigning to 'NSMutableArray<Coordinatorable> *' from 'NSMutableArray *'
self.childCoordinators = [@[] mutableCopy]; //works without issue

最佳答案

我认为问题的根源在于您申报属性(property)的方式。线路:

@property (nonatomic, strong, readonly) NSMutableArray<Coordinatorable>*childCoordinators;

应该是:

@property (nonatomic, strong, readonly) NSMutableArray<id<Coordinatorable>>*childCoordinators;

id<SomeeProtocol>的使用是引用符合协议(protocol)的对象时所需要的。

关于ios - NSMutableArray 新协议(protocol)类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com