Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Obj C-理解指针_Objective C_Oop - Fatal编程技术网

Objective c Obj C-理解指针

Objective c Obj C-理解指针,objective-c,oop,Objective C,Oop,我在理解Objective C中的指针方面花了很多时间。它们的行为不像我根据各种C教程所假设的那样 例如: // Define Name and ID NSString *processName = [[NSProcessInfo processInfo] processName]; NSNumber *processID = [NSNumber numberWithInt:[[NSProcessInfo processInfo] processIdentifier]]; // Print N

我在理解Objective C中的指针方面花了很多时间。它们的行为不像我根据各种C教程所假设的那样

例如:

// Define Name and ID
NSString *processName = [[NSProcessInfo processInfo] processName];
NSNumber *processID = [NSNumber numberWithInt:[[NSProcessInfo processInfo] processIdentifier]];

// Print Name and ID
NSLog(@"Process Name: %@  Process Identifier: %@", processName, processID);
据我所知,processName是指向NSString类型对象的指针。processID是指向NSNumber类型的对象的指针。在NSLog()中调用两者时,它们的名称前面没有星号,因此应该返回指针值。为什么Obj C中没有“地址”字符?为什么这个代码可以工作


谢谢您的时间。

格式字符串中的
%@
告诉
NSLog
调用相关对象的
-description
,并将该字符串用作其“显示值”。如果确实需要指针的地址,则应在64位上使用
%x
%qx

目标c中的对象表示为指向包含所有对象数据的c结构的指针。如果对象是一个实际的结构(而不是指向一个结构的指针),那么像将对象作为方法参数传递这样的事情将效率大大降低。因此,初始化对象后:

NSString *aString = /* initial value */;
您几乎总是使用指针aString,而不是去引用它(即.*aString)


NSLog()函数中的%@标记需要指针类型,并将调用该对象上的description方法来确定要输出的值。在NSString对象上调用description方法时,它会返回接收方,因此%@标记会被输出中字符串的内容替换。

或使用%p,它会打印一个指针地址,并为指针提供更好的格式。