1

我们可以在 Objective-C 中使用像这样的 __strong 双点。

NSString *__strong * tmp_pointer;
NSString * target = @"first data";
tmp_pointer = target;
*tmp_pointer = @"second data";
NSLog(@"%@", target);

Output : first data

但是对象变量呢?

示例:DataObject 有一个 NSString* 类型变量“item1”。

DataObject *dataObject = [[DataObject alloc] init];
NSString *__strong * tmp_pointer;
tmp_pointer = &dataObject.item1; <- Address of property expression requested error occurred.

我尝试了几种表达方式,但一切都失败了。

tmp_pointer = &(dataObject.item1);
tmp_pointer = &(NSString *)dataObject.item1;
tmp_pointer = &((NSString *)dataObject.item1);

有谁知道我该如何解决这个问题?

谢谢

4

1 回答 1

2

通过点符号访问属性是方法调用的语法糖,所以你真正在做的是

&[dataObject item1]

这是语法不允许的。

的参数&是必须是左值函数指示符 [1]的表达式,而方法调用两者都不是。

If want the address of the value returned by the getter, you have to turn it into a lvalue first. The most straightforward way is to assign it to a local variable:

NSString * item1 = dataObject.item1;        // now item1 is a lvalue
NSString * __strong * tmp_pointer = &item1; // so it can be the argument of &

[1] A function designator as an expression that has function type.
An object is a region of storage that can be examined and stored into and an lvalue is an expression that refers to such an object.
(source)

于 2013-09-09T21:53:33.600 回答