问题描述
我知道关于指针的问题很多,尤其是对于Objective-C.但是我正在寻找更高层次的答案,以帮助我理解Objective-C中的范例.
我听说有人说在Objective-C中使用指针是一个问题或经验,即某些类要求使用指针,而另一些则不需要.这是真的?就是在Objective-C中使用指针的程度.
基本上,除了要显式地将引用变量传递给方法之外,Objective-C中指针的规则是什么?
在引用堆中的内容时,总是使用指针 ;有时,当引用时,通常不使用指的是堆栈上的东西.
由于Objective-C对象总是分配在堆上(Blocks除外,但这与本讨论正交),因此,您始终使用指向Objective-C对象的指针. id
和Class
类型实际上都是指针.
对于某些原始类型和简单结构,您不使用指针的地方. NSPoint
,NSRange
,int
,NSUInteger
等…通常通过堆栈访问 ,并且通常您不使用指针. /p>
对于Why the * in Objective-C?
,您可能会发现以下问题感兴趣.
I know there are a lot of questions on pointers out there, particularly now for Objective-C. But I'm looking for some higher level answers to help me understand the paradigms in Objective-C.
I've heard some people say that using pointers in Objective-C is a matter or experience, i.e. some classes demand that you use pointers, others don't. Is this true? And is that the extent of using pointers in Objective-C.
Basically, apart from when you want to explicitly pass reference variable to methods, what are the rules for pointers in Objective-C?
You use a pointer always when referring to something on the heap and sometimes, but usually not when referring to something on the stack.
Since Objective-C objects are always allocated on the heap (with the exception of Blocks, but that is orthogonal to this discussion), you always use pointers to Objective-C objects. Both the id
and Class
types are really pointers.
Where you don't use pointers are for certain primitive types and simple structures. NSPoint
, NSRange
, int
, NSUInteger
, etc... are all typically accessed via the stack and typically you do not use pointers.
As for Why the * in Objective-C?
, you might find this question of interest.
这篇关于何时以及何时不使用Objective-C中的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!