简体   繁体   中英

iOS Objective-C: weakSelf Syntax

Nearly every example of the weakSelf pattern I encounter online has the syntax

__weak typeof(self) weakSelf = self;

But the compiler complains about this syntax in the latest version of Xcode, requiring:

__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.

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.

Click on the project at the top of the Project Navigator window, click on Build Settings , and choose to View All : 选择设置

Next scroll down to Apple LLVM n . n - Language and examine the settings: 语言设定

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.

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. Better than introducing something by changing it because I am a lazy typist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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