问题标签 [objective-c-nullability]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
299 浏览

objective-c - 在 Objective C 的某些情况下如何使用 nonnull

我已经阅读了许多关于可空性的帖子,但我无法终生让我的警告消失。这里有几个例子:

其中每一个都会收到此警告:“从可空指针 'NSData * _Nullable' 隐式转换为不可空指针类型 'NDSata* _Nonnull'

用 NS_ASSUME_NONNULL_BEGIN/End 包围代码也不起作用。

我尝试了多种(非空)__nonnull 等组合,但似乎无法找到关键字的神奇位置来使警告消失。

0 投票
0 回答
113 浏览

objective-c - 将 Objective-C 对象指针类型与 Clang 的 LibTooling 中的可空性说明符匹配

可以说我有这样的代码:

现在,我想创建一个匹配器来匹配变量声明。

目前我有这样的事情:

有,objcObjectPointerType()但它只匹配a。有一些指针/引用匹配器,但它们根本不适用于这些变量声明。此外,我尝试过,isAnyPointer()但它适用于任何指针,无论是否为 obj-c。

我试图type()确保我所做的一切都是正确的,并且它有效。

那么,我怎样才能只匹配 obj-c 变量声明,有或没有可空性说明符?

谢谢!

0 投票
2 回答
609 浏览

objective-c++ - Nullability rules for C++ objects in Objective-C++

(Please edit this post if I use incorrect C++ terms. I'm a total C++ noob.)

How does Objective-C nullability work with C++ objects in an Objective-C++ class?

For example, given the following type and function:

Is it legal to redefine GetFoo thusly?

Will I get any warnings from either clang or the static analyzer if I call GetFoo thusly?

0 投票
1 回答
114 浏览

ios - CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION 删除了吗?

似乎CLANG_WARN_NULLABLE_TO_NONNULL_CONVERSION在 Xcode 8 中被删除了。它提供了一些非常好的警告,以防nil被传递给nonnull参数/返回类型。知道为什么将其删除以及是否存在替代品吗?

0 投票
3 回答
7237 浏览

ios - How do I safely cast a `_Nullable` to a `_Nonnull` in Objective-C?

When compiling with -Wnullable-to-nonnull-conversion, we get a proper warning with the following code:

How do I safely cast foo from an NSString * _Nullable to an NSString * _Nonnull?

The best solution I have so far

The best I've come up with is this macro:

Which is used like:

And which produces an error if assigned to a wrongly-typed variable:

And which produces an error if cast to the wrong type:

Unfortunately, if you need to cast to a generic type with multiple arguments, you have to resort to preprocessor hacks:

Things I've tried that don't work

Assigning maybeFoo directly to an NSString * _Nonnull doesn't work. It produces the same error as before:

And casting to maybeFoo to NSString * _Nonnull isn't safe because if maybeFoo's type changes, the compiler won't break:

I also tried using __typeof__, when casting, but __typeof__ carries the nullability specifier, so when you try to cast to __typeof__(maybeFoo) _Nonnull you get a nullability conflict:

Everything was run with the deep static analyzer and compiled with Xcode 8.2.1 with the following flags:

0 投票
1 回答
399 浏览

objective-c - Objective-C 函数的结果默认是非空的吗?

在这个关于实现委托方法的线程UITextInput中,发帖人说当他们对他们的代码运行静态分析时,他们在这个函数上得到一个错误:

错误是“从预期返回非空值的方法返回 nil”。

函数声明的结果没有可空性说明符。默认情况下函数结果是非空的吗?(这些天我主要在 Swift 中工作,并且对 Objective-C 的最新变化不是专家。)

0 投票
1 回答
1188 浏览

objective-c - 使用多级指针类型处理 _Nonnull 或 Nullable

0 投票
1 回答
686 浏览

objective-c - 将普通 -init 标记为具有可为空的返回类型而不会发出警告

我有一个继承自它的 ObjC 类,NSObject它包装了一些第三方库宏,以便我可以在 Swift 中使用它们的功能。该类有一个 ivar,它是库中的容器类型。容器的创建可能会失败,如果确实如此,我的类的实例化也应该失败。所以我标记-initnullable

不幸的是,该-init声明会产生编译器警告

返回类型上的可空性说明符冲突,“_Nullable”与现有说明符“非空”冲突

指着NSObject的声明。

作为一种解决方法,我用编译指示将警告标记为忽略,并且该方法在 Swift 中显示为失败。

我考虑的另一个选择是标记普通-init不可用并创建一个类方法+empty,来代替。但这很烦人,因为 a) 我不能使用 Swift 初始化语法,并且 b)init在类自己的实现中也不可用。

有没有办法在init没有警告的情况下覆盖继承的可空性?

0 投票
2 回答
1344 浏览

objective-c - 防止 Xcode 在新标头上创建 NS_ASSUME_NONNULL_BEGIN 和 NS_ASSUME_NONNULL_END

我已经更新了 Xcode,并且自从更新以来,我一直在为我创建的每个新类的每个新头文件获取NS_ASSUME_NONNULL_BEGIN和宏。NS_ASSUME_NONNULL_END

我知道它的作用,但我对它的可空性注释不感兴趣,当我尝试nil在我的应用程序中设置一些属性时,它会导致不必要的警告(完全可以设置为nil)。

每次创建新文件时,如何防止 Xcode 创建这些文件?(除了明显的:单独删除它们)

0 投票
1 回答
460 浏览

objective-c - id _Nullable __autoreleasing * _Nonnull 是什么意思?

(out id _Nullable *)我对代码文档中的使用以及我的代码段向我抛出的错误感到困惑。

无法使用“bool *”类型的左值初始化“id _Nullable __autoreleasing * _Nonnull”类型的参数

是什么_Nullable * _Nonnull意思,我在代码中做错了什么?

参考:

getResourceValue:forKey:error: