Xcode 13 - Count 不再在 NSMutableArray 上工作
Xcode 13 - Count not working on NSMutableArray anymore
我的代码中有这样的东西,多年来一直运行良好
NSMutableArray* DeviceList = [NSMutableArray alloc] init]; // Multi-dimensional (n x m) mutable Array filled throughout the code before arriving here:
if ([[DeviceList objectAtIndex:i] count] == 9))
{
[DeviceList removeObjectAtIndex:i];
}
好久没碰项目了,现在升级到Xcode13,报如下错误:
发现多个名为 'count' 的方法具有不匹配的结果、参数类型或属性
左侧窗格中的错误列表显示了 12 个选项,其中已声明 count 个。
简单的
发生了什么变化
someNSUInteger = [[someArray] count];
创建一个错误,在 100 多个实例中修复它而不用 (NSMutableArray*) 强制转换每个实例的好方法是什么?
非常感谢!
objectAtIndex returns “id”(任何类型的对象)。以前它是宽容的,你可以调用任何方法(比如“计数”)。
编写此代码的更好方法是声明一个中间变量:
NSArray *deviceRow = [DeviceList objectAtIndex:i];
if ([deviceRow count] == 9) {
或者如果您知道数组中项目的类型,假设您有字符串:
NSArray<NSString *> *deviceRow = [DeviceList objectAtIndex:i];
if ([deviceRow count] == 9) {
甚至更好:
NSArray<NSString *> *deviceRow = DeviceList[i];
if (deviceRow.count == 9) {
另一个问题是这样的例子太多了,你不想到处碰它。
解决这 100 次出现的大部分问题的一种方法是为 DeviceList 定义一个包装器 class,而不是使用“裸”NSMutableArray:
@interface MyDeviceList
- (nonnull instancetype)init;
- (nullable NSArray *)objectAtIndex:(NSUInteger)index;
... any other NSMutableArray methods used in the code ...
@end
并将数组实例化替换为:
MyDeviceList *DeviceList = [MyDeviceList new];
希望您的实例化比不安全的用法少得多。
你也可以在 MyDeviceList 中有一些辅助方法,比如“countAtIndex”,如果你碰巧需要它的话。
我的代码中有这样的东西,多年来一直运行良好
NSMutableArray* DeviceList = [NSMutableArray alloc] init]; // Multi-dimensional (n x m) mutable Array filled throughout the code before arriving here:
if ([[DeviceList objectAtIndex:i] count] == 9))
{
[DeviceList removeObjectAtIndex:i];
}
好久没碰项目了,现在升级到Xcode13,报如下错误:
发现多个名为 'count' 的方法具有不匹配的结果、参数类型或属性
左侧窗格中的错误列表显示了 12 个选项,其中已声明 count 个。
简单的
发生了什么变化someNSUInteger = [[someArray] count];
创建一个错误,在 100 多个实例中修复它而不用 (NSMutableArray*) 强制转换每个实例的好方法是什么?
非常感谢!
objectAtIndex returns “id”(任何类型的对象)。以前它是宽容的,你可以调用任何方法(比如“计数”)。
编写此代码的更好方法是声明一个中间变量:
NSArray *deviceRow = [DeviceList objectAtIndex:i];
if ([deviceRow count] == 9) {
或者如果您知道数组中项目的类型,假设您有字符串:
NSArray<NSString *> *deviceRow = [DeviceList objectAtIndex:i];
if ([deviceRow count] == 9) {
甚至更好:
NSArray<NSString *> *deviceRow = DeviceList[i];
if (deviceRow.count == 9) {
另一个问题是这样的例子太多了,你不想到处碰它。
解决这 100 次出现的大部分问题的一种方法是为 DeviceList 定义一个包装器 class,而不是使用“裸”NSMutableArray:
@interface MyDeviceList
- (nonnull instancetype)init;
- (nullable NSArray *)objectAtIndex:(NSUInteger)index;
... any other NSMutableArray methods used in the code ...
@end
并将数组实例化替换为:
MyDeviceList *DeviceList = [MyDeviceList new];
希望您的实例化比不安全的用法少得多。
你也可以在 MyDeviceList 中有一些辅助方法,比如“countAtIndex”,如果你碰巧需要它的话。