指针,我正确使用它们吗?目标/可可 [英] Pointers, am I using them correctly? Objective-c/cocoa

查看:135
本文介绍了指针,我正确使用它们吗?目标/可可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的@interface中有这个

I have this in my @interface

struct track currentTrack;
struct track previousTrack;
int anInt;

因为这些不是对象,我不必像int * anInt一样
如果设置非对象值,如ints,boolean等,我不必释放旧的值(假设非GC环境)?

Since these are not objects, I do not have to have them like int* anInt right? And if setting non-object values like ints, boolean, etc, I do not have to release the old value right (assuming non-GC environment)?

struct包含对象:

The struct contains objects:

typedef struct track {
NSString* theId;
NSString* title;
} *track;

我这样做是否正确?

最后,我访问结构像这样:

Lastly, I access the struct like this:

[currentTrack.title ...];
currentTrack.theId = @"asdf"; //LINE 1

我也手动管理内存(从setter) this:

I'm also manually managing the memory (from a setter) for the struct like this:

[currentTrack.title autorelease];
currentTrack.title = [newTitle retain];

如果我正确理解垃圾回收,我应该可以沟通,像LINE 1(上面)?

If I'm understanding the garbage collection correctly, I should be able to ditch that and just set it like LINE 1 (above)?

还有垃圾收集,我不需要dealloc方法吗?
如果我使用垃圾回收这意味着它只能运行在OS 10.5+?在切换到垃圾收集代码之前,我应该知道的任何其他事情吗?

Also with garbage collection, I don't need a dealloc method right? If I use garbage collection does this mean it only runs on OS 10.5+? And any other thing I should know before I switch to garbage collected code?

对不起,有这么多问题。

Sorry there are so many questions. Very new to objective-c and desktop programming.

感谢

推荐答案


我在@interface中有这个

I have this in my @interface

struct track currentTrack;
struct track previousTrack;
int anInt;

由于这些不是对象,我不必像 int * anInt right?

Since these are not objects, I do not have to have them like int* anInt right?

这样会声明一个 int 存储在其他地方。

That would declare a pointer to an int stored somewhere else.


如果设置非对象值如ints,boolean等,释放旧值(假设非GC环境)?

And if setting non-object values like ints, boolean, etc, I do not have to release the old value right (assuming non-GC environment)?

是一条消息。您只能向Cocoa(或在某些情况下,Core Foundation)对象发送消息。

release is a message. You can only send a message to a Cocoa (or, in some cases, Core Foundation) object.


结构包含对象:

The struct contains objects:

typedef struct track {
    NSString* theId;
    NSString* title;


更精确地说,它包含指向对象的指针。

More precisely, it contains pointers to objects.

你不能有一个对象直接存储在变量中;您只能通过向类发送 alloc 消息来动态分配它,并接收指向所分配实例的指针。类似地,您只能将消息发送到指向对象的指针;您不能也不应该取消引用指向某个对象的指针。

You can't ever have an object stored directly in a variable; you can only allocate it dynamically by sending an alloc message to a class, and receive the pointer to the allocated instance. Similarly, you can only send a message to a pointer to an object; you cannot and should not dereference a pointer to an object.

由于这些原因,我们几乎总是删除指针。我们说的指针,如果他们是对象,但在精确的真相,他们不是。

For these reasons, we almost always elide the "a pointer to". We speak of the pointers as if they are the objects, but, in precise truth, they are not.


} *track;


这是正确的,如果你想声明 track 键入 struct track 的指针。

That's correct if you want to declare the track type as being a pointer to a struct track. Generally, this will confuse people.


最后,

Lastly, I access the struct like this:

[currentTrack.title ...];
currentTrack.theId = @"asdf"; //LINE 1


所以上一行是第0行? ;)

So the previous line is line 0? ;)


我也像下面这样手动管理内存(从setter):

I'm also manually managing the memory (from a setter) for the struct like this:

[currentTrack.title autorelease];
currentTrack.title = [newTitle retain];

如果我正确理解垃圾回收,我应该可以沟通,像上面的LINE 1?

If I'm understanding the garbage collection correctly, I should be able to ditch that and just set it like LINE 1 (above)?

如果你使用垃圾收集,那么 autorelease retain 消息不会做任何操作,所以是的,普通赋值和赋值(无效)释放和保留消息是等价的。

If you're using garbage collection, then the autorelease and retain messages will do nothing, so yes, the plain assignment and the assignment with (ineffectual) release and retain messages are equivalent.

我问你为什么要将这些信息放在结构中,而不是模型对象

I do question why you're putting this information in a structure and not a model object, though.


需要一个dealloc方法吗?如果我使用垃圾回收这意味着它只能运行在OS 10.5+?

Also with garbage collection, I don't need a dealloc method right? If I use garbage collection does this mean it only runs on OS 10.5+? And any other thing I should know before I switch to garbage collected code?

是的:读取垃圾收集编程指南

对于指针,您可能需要阅读我的指针教程。标题说C,但C中的一切在Objective-C中也是如此。

As for pointers, you may want to read my pointers tutorial. The title says C, but everything in C is also true in Objective-C.

这篇关于指针,我正确使用它们吗?目标/可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆