强类型集合中为空?
Null in a strongly typed collection?
我有一个强类型集合:
NSMutableArray<SupportClass *> *_items;
我需要将一个类似 null
的对象放入其中。当你需要它与 ObjC 中的非类型化集合时 - 你使用 [NSNull null]
object:
[_items replaceObjectAtIndex:index withObject:[NSNull null]];
但是对于键入的,我在该行收到以下警告:
Incompatible pointer types sending 'NSNull * _Nonnull' to parameter of
type 'SupportClass * _Nonnull'
我理解 为什么 此示例中混合了类型化集合和 [NSNull null]
对象时会出现警告。
拥有具有编译时类型检查的强类型集合并同时能够将 null
类对象放入其中的正确方法是什么?
I have a strongly typed collection:
NSMutableArray<SupportClass *> *_items;
合并您的两条评论:
But [the majority of] programming languages known to me allows to have a typed collection with compile time type checks and at the same time being able to put null objects into it. So I'm just looking for a way to do the same with ObjC.
您面临的问题是因为您正在使用 Objective-C 的最新添加,旨在改善与 Swift 的互操作性,而 Swift 不允许您混入以这种方式将空值放入您的“强类型集合”中。
Swift模型一般是将你习惯的“reference or null”模型替换为“non-null reference and optional”模型。在 Swift 中,对 SomeClassType
的引用是非空的,而类型 SomeClassType?
对于 Optional<SomeClassType>
是 shorthand。 Optional
类型有两种情况:.none
和 .some(v)
,Swift 允许您通过与 nil
比较来测试 .none
。
Swift 并没有发明这个模型,(像语言的大部分部分一样)它是从其他语言继承而来的,就像许多 函数式语言 的强大用户一样这样 标记的联合 .
My question is: Which one is the preferred to deal with the issue from the experienced ObjC developer point of view?
使用 NSNull
,如您所愿。这是 Apple 推荐的方法,raison d'être NSNull
,但可以追溯到 Apple 引入 轻量级泛型 之前帮助 Obj-C 和 Swift.
之间的桥梁
如何在 post 轻量级泛型世界中使用“引用或空”模型?我认为您知道大多数替代方案,它们包括:
- 不要。要么不要在 Obj-C 中使用强类型集合,要么按照 Swift 模型使用它们。
- 使用Swift。如果你想要静态强类型而不是运行时类型,请使用基于它的语言。
- 使用子class。覆盖每个 method/property 以抛出非法使用 null 异常。添加一个
isNull
谓词。等等
- 使用有区别的对象。不喜欢继承,那么只需创建一个 class 的唯一实例来充当 null 案例。添加适当的methods/properties支持
- 在 Obj-C 中构建您自己的
Optional
。
- 突破纸墙。轻量级泛型确实是标准集合之上的薄纸层,如果需要,只需 break through it。
- 等等
没有单一的“正确方法”可以解决您的问题。查看您的设计,确定“空引用”模型是否是关键,选择适合您情况的解决方案。
HTH
我有一个强类型集合:
NSMutableArray<SupportClass *> *_items;
我需要将一个类似 null
的对象放入其中。当你需要它与 ObjC 中的非类型化集合时 - 你使用 [NSNull null]
object:
[_items replaceObjectAtIndex:index withObject:[NSNull null]];
但是对于键入的,我在该行收到以下警告:
Incompatible pointer types sending 'NSNull * _Nonnull' to parameter of type 'SupportClass * _Nonnull'
我理解 为什么 此示例中混合了类型化集合和 [NSNull null]
对象时会出现警告。
拥有具有编译时类型检查的强类型集合并同时能够将 null
类对象放入其中的正确方法是什么?
I have a strongly typed collection:
NSMutableArray<SupportClass *> *_items;
合并您的两条评论:
But [the majority of] programming languages known to me allows to have a typed collection with compile time type checks and at the same time being able to put null objects into it. So I'm just looking for a way to do the same with ObjC.
您面临的问题是因为您正在使用 Objective-C 的最新添加,旨在改善与 Swift 的互操作性,而 Swift 不允许您混入以这种方式将空值放入您的“强类型集合”中。
Swift模型一般是将你习惯的“reference or null”模型替换为“non-null reference and optional”模型。在 Swift 中,对 SomeClassType
的引用是非空的,而类型 SomeClassType?
对于 Optional<SomeClassType>
是 shorthand。 Optional
类型有两种情况:.none
和 .some(v)
,Swift 允许您通过与 nil
比较来测试 .none
。
Swift 并没有发明这个模型,(像语言的大部分部分一样)它是从其他语言继承而来的,就像许多 函数式语言 的强大用户一样这样 标记的联合 .
My question is: Which one is the preferred to deal with the issue from the experienced ObjC developer point of view?
使用 NSNull
,如您所愿。这是 Apple 推荐的方法,raison d'être NSNull
,但可以追溯到 Apple 引入 轻量级泛型 之前帮助 Obj-C 和 Swift.
如何在 post 轻量级泛型世界中使用“引用或空”模型?我认为您知道大多数替代方案,它们包括:
- 不要。要么不要在 Obj-C 中使用强类型集合,要么按照 Swift 模型使用它们。
- 使用Swift。如果你想要静态强类型而不是运行时类型,请使用基于它的语言。
- 使用子class。覆盖每个 method/property 以抛出非法使用 null 异常。添加一个
isNull
谓词。等等 - 使用有区别的对象。不喜欢继承,那么只需创建一个 class 的唯一实例来充当 null 案例。添加适当的methods/properties支持
- 在 Obj-C 中构建您自己的
Optional
。 - 突破纸墙。轻量级泛型确实是标准集合之上的薄纸层,如果需要,只需 break through it。
- 等等
没有单一的“正确方法”可以解决您的问题。查看您的设计,确定“空引用”模型是否是关键,选择适合您情况的解决方案。
HTH