在Objective-C中使用NSLog时取消引用指针

时间:2022-04-18 22:52:43
NSDate *now = [NSDate date];
NSLog(@"This NSDate object lives at %p", now);
NSLog(@"The date is %@", now);

Ok, from this code, I know that now is a pointer to an NSDate object, but on the code at line 3, how can you dereference a pointer without an asterisk? Why don't we do code like this on the 3rd line:

好的,从这段代码中,我知道现在是一个指向NSDate对象的指针,但是在第3行的代码中,如何在没有星号的情况下取消引用指针?为什么我们不在第3行做这样的代码:

NSLog(@"The date is %@", *now);

2 个解决方案

#1


7  

%@ takes a pointer to an object and sends it the description message, which returns an NSString pointer. (You can override description in your classes to customize the string.)

%@获取指向对象的指针,并向其发送描述消息,该消息返回NSString指针。 (您可以覆盖类中的描述以自定义字符串。)


Added in response to comment:

添加以回应评论:

In Objective-C, you to send messages to objects via a pointer using the [ objectPointer message ] syntax. So, using your NSDate example, you can do:

在Objective-C中,您可以使用[objectPointer message]语法通过指针向对象发送消息。因此,使用您的NSDate示例,您可以:

NSDate * now = [NSDate date];
NSString * dateDescription = [now description];    // Note that "now" points to an object and this line sends it the "description" message
NSLog(dateDescription);

Any instance of a class that inherits from NSObject can be sent the description message and thus a pointer to it can be passed to an %@ format parameter.

可以向描述消息发送从NSObject继承的类的任何实例,因此可以将指向它的指针传递给%@ format参数。

(Technical note: if the object supports the descriptionWithLocale: message, it will be sent that instead.)

(技术说明:如果对象支持descriptionWithLocale:消息,则会发送该消息。)

#2


10  

The %@ format specifier takes a pointer to an object, so there's no need to dereference the pointer in the parameter list. In general, there's no need to dereference pointers to Objective C objects.

%@格式说明符采用指向对象的指针,因此无需在参数列表中取消引用指针。通常,不需要取消引用Objective C对象的指针。

#1


7  

%@ takes a pointer to an object and sends it the description message, which returns an NSString pointer. (You can override description in your classes to customize the string.)

%@获取指向对象的指针,并向其发送描述消息,该消息返回NSString指针。 (您可以覆盖类中的描述以自定义字符串。)


Added in response to comment:

添加以回应评论:

In Objective-C, you to send messages to objects via a pointer using the [ objectPointer message ] syntax. So, using your NSDate example, you can do:

在Objective-C中,您可以使用[objectPointer message]语法通过指针向对象发送消息。因此,使用您的NSDate示例,您可以:

NSDate * now = [NSDate date];
NSString * dateDescription = [now description];    // Note that "now" points to an object and this line sends it the "description" message
NSLog(dateDescription);

Any instance of a class that inherits from NSObject can be sent the description message and thus a pointer to it can be passed to an %@ format parameter.

可以向描述消息发送从NSObject继承的类的任何实例,因此可以将指向它的指针传递给%@ format参数。

(Technical note: if the object supports the descriptionWithLocale: message, it will be sent that instead.)

(技术说明:如果对象支持descriptionWithLocale:消息,则会发送该消息。)

#2


10  

The %@ format specifier takes a pointer to an object, so there's no need to dereference the pointer in the parameter list. In general, there's no need to dereference pointers to Objective C objects.

%@格式说明符采用指向对象的指针,因此无需在参数列表中取消引用指针。通常,不需要取消引用Objective C对象的指针。