ARC不允许将非Objective-C指针类型void*隐式转换为NSString*__ strong*

use*_*991 6 cocoa objective-c ios automatic-ref-counting

迁移到ARC时出现上述错误.这是代码:

static NSString *cashBalanceKeyPath = @"test";

...

[xxx forKeyPath:cashBalanceKeyPath options:NSKeyValueObservingOptionNew context:&cashBalanceKeyPath];

...

-(void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {

    if (&cashBalanceKeyPath == context)   < error here
    {
      ...
    }
Run Code Online (Sandbox Code Playgroud)

当我用桥时:

if (&cashBalanceKeyPath == (__bridge NSString *)context)
Run Code Online (Sandbox Code Playgroud)

我收到了错误: Comparison of distinct pointer types (NSString *__strong* and NSString *)

我怎样才能进行转换?提前致谢.

cob*_*bal 1

如果去掉__strong错误消息中的 ,就会更清楚发生了什么:

Comparison of distinct pointer types (NSString ** and NSString *)
Run Code Online (Sandbox Code Playgroud)

&cashBalanceKeyPath是一个指向 NSString 对象或 的指针NSString**,而上下文被转换为NSString*, 或普通 NSString 对象(它不是)。

因此,要解决此问题,请将强制类型转换更改为(NSString * const *),显然需要使用 const 来安抚 ARC。