简体   繁体   English

iOS Objective-C:weakSelf语法

[英]iOS Objective-C: weakSelf Syntax

Nearly every example of the weakSelf pattern I encounter online has the syntax 我在网上遇到的weakSelf模式的几乎所有例子都有语法

__weak typeof(self) weakSelf = self;

But the compiler complains about this syntax in the latest version of Xcode, requiring: 但编译器在最新版本的Xcode中抱怨这种语法,要求:

__weak __typeof__(self) weakSelf = self;

Did the syntax change, or am i missing an import? 语法是否改变,或者我错过了导入? I tried pulling in <objc/runtime.h> but no change. 我试着拉入<objc/runtime.h>但没有改变。

Stealthy bonus question: Why not just cast it explicitly? 隐形奖金问题:为什么不明确地投出它?

__weak MyBoffoClass *weakSelf = self;

I found one answer here that said it was better to cast it directly, but no reasoning as to why. 我在这里找到一个答案,说最好直接投射,但没有理由为什么。

After the help from @danh pointing me at Difference between typeof, __typeof and __typeof__ (Objective-c) , I discovered the issue was in the compiler settings. 在@danh帮助我指出typeof,__ typeof和__typeof __(Objective-c)之间的差异之后 ,我发现问题出现在编译器设置中。

Click on the project at the top of the Project Navigator window, click on Build Settings , and choose to View All : 单击Project Navigator窗口顶部的Project Navigator ,单击Build Settings ,然后选择View All 选择设置

Next scroll down to Apple LLVM n . 接下来,向下滚动到Apple LLVM n n - Language and examine the settings: n - 语言并检查设置: 语言设定

In my case, the language dialect was set to C99, which doesn't support the GNU compiler macros and other extensions to the C language. 在我的例子中,语言方言被设置为C99,它不支持GNU编译器宏和C语言的其他扩展。

Not sure why it's set like this for the project (the project architect doesn't recall why), I just have to allow for the fact in my reading of StackOverflow code snippets. 不确定为什么它为项目设置这样(项目架构师不记得原因),我只需要在阅读StackOverflow代码片段时考虑到这一点。 Better than introducing something by changing it because I am a lazy typist. 因为我是一个懒惰的打字员而不是通过改变它来介绍一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM