简体   繁体   English

Objective-C:对象实例指针

[英]Objective-C: object instance pointers

I'm new to Objective-C and trying to get my head around pointers. 我是Objective-C的新手,正在努力吸引指针。 This isn't my first port of call; 这不是我的第一站电话。 I'm hoping somebody will be able to discern the source of my confusion and explain this concept in plain English. 我希望有人能够辨别出我的困惑,并以通俗的英语解释这个概念。

In the following code (from a Treehouse lesson) an instance, ball, of an object class, Sphere, is created. 在以下代码中(从Treehouse课程中),创建了一个对象类Sphere的实例球。

#import <Cocoa/Cocoa.h>
#import "Sphere.h"

int main()
{
Sphere *ball = [[Sphere alloc] init];

[ball setRadius:25];

NSLog(@"ball radius %f", [ball radius]);

return 0;
}

I understand that in Objective-C, all instances of an object class are pointers. 我了解在Objective-C中,对象类的所有实例都是指针。

I understand that 'the ball pointer points to the memory that is allocated for the ball object', as someone else noted for me elsewhere. 我理解“球指针指向为球对象分配的内存”,正如其他人在我这里指出的那样。

But what's happening in terms of actual memory allocation? 但是在实际的内存分配方面发生了什么?

Are the 'instance' and the 'pointer' the same thing, created at the same time? “实例”和“指针”是同时创建的吗?

Sphere *ball

This allocates some memory big enough to fit a pointer. 这样会分配一些内存,足以容纳一个指针。 (on the stack) (在堆栈上)

[Sphere alloc]

This allocates some memory elsewhere (in the heap) big enough to fit an instance of the class Sphere, and sets all the instance variables to 0. It returns the address of the space allocated for the object (In other words a pointer to the new object). 这会在其他地方(堆中)分配足够大的内存以适合Sphere类的实例,并将所有实例变量设置为0。它返回为对象分配的空间的地址(换句话说,指向新指针的指针)。宾语)。

[[Sphere alloc] init]

The init method then initialises all the fields and other initialising. 然后,init方法将初始化所有字段并进行其他初始化。 (Assuming you've overriding init in Sphere, the implementation of init in NSObject does nothing). (假设您已在Sphere中覆盖了init,则在NSObject中实现init不会执行任何操作)。 This has nothing to do with allocating space in memory for the object itself, though it often allocates space for other objects referenced by the object. 这与为对象本身在内存中分配空间无关,尽管它通常为对象引用的其他对象分配空间。 It returns the memory address of the object, most of the time this is the same address that was returned by alloc. 它返回对象的内存地址,大多数情况下,它与alloc返回的地址相同。

The memory address returned by [[Sphere alloc] init] is then stored in the ball variable. 然后,由[[Sphere alloc] init]返回的内存地址存储在ball变量中。 (by the = ) (通过=

A pointer is just a number, a memory address that corresponds to a memory location. 指针只是一个数字,对应于存储位置的存储地址。 An object is an instance of a class, and the object refers to the actual memory allocated for the object. 对象是类的实例,并且对象是指为该对象分配的实际内存。

instance and pointers are not 100% identical, although in the code they are both represented by your ball variable. 实例指针并非100%相同,尽管在代码中它们都由ball变量表示。

The answer to your question can be found in the line: 您的问题的答案可以在以下行中找到:

Sphere *ball = [[Sphere alloc] init];

Let's take a closer look, this line contains two method calls: alloc and init . 让我们仔细看看,此行包含两个方法调用: allocinit

alloc is a class method implemented in NSObject , it is described as follows in the documentation : Returns a new instance of the receiving class. alloc是在NSObject实现的类方法,在文档中描述如下: 返回接收类的新实例。

This means that at this point, the operating system is going to allocate memory for an object of type Sphere . 这意味着此时,操作系统将为Sphere类型的对象分配内存。

init is the initializer and described in the docs as follows: Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated. init初始化程序,并在文档中进行了如下描述: 由子类实现,以在为其分配内存后立即初始化新对象(接收方)。 You can override this method in your implementation of the Sphere class to perform your own acxtions (typically you would set values for the class's properties here). 您可以在Sphere类的实现中重写此方法以执行您自己的修改(通常在此处为类的属性设置值)。

After this line of code has been executed, you have 1. allocated the physical memory for the instance of class Sphere and 2. initialized the same instance. 执行完此行代码之后,您已经1.为Sphere类的实例分配了物理内存,并且2.初始化了相同的实例。 This means that this instance (or object , these terms are equivalent) is now ready for your use. 这意味着该实例(或object ,这些术语等效)现在可以使用了。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM