Objective-C类中的指针、原语和属性

时间:2022-09-07 09:38:24

I really need some clarification — I have a few questions and I'm all mixed up right now.

我真的需要澄清一下——我有几个问题,现在我都糊涂了。

Here is a simple class interface:

下面是一个简单的类接口:

#import <UIKit/UIKit.h>

@interface Car : NSObject{
    NSInteger carID;
    NSString *carName;
}

@property (nonatomic, assign) NSInteger carID;
@property (nonatomic, copy) NSString * carName;
@end
  1. Why is carID not declared as a pointer?
  2. 为什么carID不声明为指针?
  3. Why does it use "assign" for carID instead of "copy"?
  4. 为什么它用“分配”来代替“复制”?
  5. Why even declare class members as pointers in the first place? (In my main program, my Car object will be used as a pointer.)
  6. 为什么要首先声明类成员作为指针呢?(在我的主程序中,我的Car对象将用作指针。)

4 个解决方案

#1


5  

NSInteger is simply a typedef for a primitive type (int on 32-bit, long on 64-bit) — it is not an object, and can as such not be retained or copied.

NSInteger只是一个原始类型的类型定义(32位上的int, 64位上的long)——它不是一个对象,因此不能保留或复制。

Class members are always pointers; you never pass the "real" objects around; as that would be, at best, unmanageable.

类成员总是指针;你永远不会传递“真实”的物体;那样的话,充其量也就是无法控制。

Edit: To expand on the last paragraph: Objective-C class instances always exist on the heap, never on the stack; this is to facilitate things like reference counting and self-managed object life cycle.

编辑:扩展最后一段:Objective-C类实例总是存在于堆上,而不是堆栈上;这是为了方便诸如引用计数和自管理对象生命周期之类的事情。

This also means that it's very hard to accidentally copy an object; but on the flip side it can be somewhat easier to accidentally dispose of an object you still need. Still, the latter is more readily debugged (as it causes a nice, big crash (at best, anyway)) than the last (which at worst causes a slow leak).

这也意味着很难不小心复制一个对象;但另一方面,意外地处理仍然需要的对象可能更容易一些。尽管如此,后者比最后一个(最坏的情况是导致缓慢泄漏)更容易调试(因为它会导致一个漂亮的大崩溃(至少是最好的))。

#2


1  

The property for carID is not really correct. For types that are not pointers, the correct definition looks like:

carID的属性并不正确。对于非指针类型,正确的定义如下:

@property (nonatomic) NSInteger carID;

It's always going to be copying a value anyway, but "copy" has a very different meaning in properties - for objects it's going to call [object copy] when that property is used to set a new value.

无论如何,它总是复制一个值,但是“copy”在属性中有一个非常不同的含义——对于对象,当这个属性被用来设置一个新值时,它将调用[object copy]。

Or you could drop off the nonatomic, but then the property is more expensive to call (by some small amount). Just leave in the nonatomic unless you have a good reason not to.

或者您可以去掉非原子属性,但是调用该属性的代价会更大(只需要少量)。除非你有充分的理由不这么做,否则就不要用原子。

#3


0  

Thanks guys!

谢谢你们了!

So in Objective-C , you have int and Pointer Int.

在Objective-C中,有int和指针int。

How do you declare these in objective C

你如何在objective C中声明这些?

-int being a regular int.

-int是一个正则的int。

-Pointer Int being an object representation of an integer. Since it is an object, it can also point to pointers*. Right?

指针Int是整数的对象表示形式。因为它是一个对象,所以它也可以指向指针*。对吧?

And Pointer Int pointers can point to pointers of any type If I wanted to. Right? It will cause a crash if it doesn't point to a Pointer int. But it will compile successfully, Right?

指针Int指针可以指向任何类型的指针,如果我想的话。对吧?如果它不指向指针int,就会导致崩溃,但它会成功编译,对吧?

But in what scenarios would I prefer using a regular int to a Pointer Int?

但是在什么情况下,我更喜欢使用正则int而不是指针int ?

#4


0  

I would like to add some clarification why you would want to use:

我想补充一些说明,说明您为什么要使用:

@property (nonatomic, copy) NSString * carName;

instead of

而不是

@property (nonatomic, retain) NSString * carName;

The copy keyword implies language semantics that you want to have a COPY of the NSString passed into your current object reference. So the pointer does not change (that is why you don't have to release the object ref).

copy关键字表示希望将NSString的副本传递到当前对象引用的语言语义。所以指针不会改变(这就是为什么你不需要释放对象ref)。

The retain keyword makes it so that you get the pointer which will be retained because the pointer reference changes for this data member (and the current one will be released). Copying a NSString might not be a considerably heavy operation, so copying NSString is used often. You have to be careful what type of property you declare as copy. There might be a considerable amount of effort to produce a copy of types like Dictionaries etc (see shallow, deep copy etc).

retain关键字使您获得将被保留的指针,因为该数据成员的指针引用发生了更改(当前的指针将被释放)。复制NSString可能不是一个非常繁重的操作,因此经常使用复制NSString。你必须小心你声明为拷贝的属性类型。可能需要付出相当大的努力来生成一个类型的副本,如字典等(参见浅、深副本等)。

Hope that helps!

希望会有帮助!

#1


5  

NSInteger is simply a typedef for a primitive type (int on 32-bit, long on 64-bit) — it is not an object, and can as such not be retained or copied.

NSInteger只是一个原始类型的类型定义(32位上的int, 64位上的long)——它不是一个对象,因此不能保留或复制。

Class members are always pointers; you never pass the "real" objects around; as that would be, at best, unmanageable.

类成员总是指针;你永远不会传递“真实”的物体;那样的话,充其量也就是无法控制。

Edit: To expand on the last paragraph: Objective-C class instances always exist on the heap, never on the stack; this is to facilitate things like reference counting and self-managed object life cycle.

编辑:扩展最后一段:Objective-C类实例总是存在于堆上,而不是堆栈上;这是为了方便诸如引用计数和自管理对象生命周期之类的事情。

This also means that it's very hard to accidentally copy an object; but on the flip side it can be somewhat easier to accidentally dispose of an object you still need. Still, the latter is more readily debugged (as it causes a nice, big crash (at best, anyway)) than the last (which at worst causes a slow leak).

这也意味着很难不小心复制一个对象;但另一方面,意外地处理仍然需要的对象可能更容易一些。尽管如此,后者比最后一个(最坏的情况是导致缓慢泄漏)更容易调试(因为它会导致一个漂亮的大崩溃(至少是最好的))。

#2


1  

The property for carID is not really correct. For types that are not pointers, the correct definition looks like:

carID的属性并不正确。对于非指针类型,正确的定义如下:

@property (nonatomic) NSInteger carID;

It's always going to be copying a value anyway, but "copy" has a very different meaning in properties - for objects it's going to call [object copy] when that property is used to set a new value.

无论如何,它总是复制一个值,但是“copy”在属性中有一个非常不同的含义——对于对象,当这个属性被用来设置一个新值时,它将调用[object copy]。

Or you could drop off the nonatomic, but then the property is more expensive to call (by some small amount). Just leave in the nonatomic unless you have a good reason not to.

或者您可以去掉非原子属性,但是调用该属性的代价会更大(只需要少量)。除非你有充分的理由不这么做,否则就不要用原子。

#3


0  

Thanks guys!

谢谢你们了!

So in Objective-C , you have int and Pointer Int.

在Objective-C中,有int和指针int。

How do you declare these in objective C

你如何在objective C中声明这些?

-int being a regular int.

-int是一个正则的int。

-Pointer Int being an object representation of an integer. Since it is an object, it can also point to pointers*. Right?

指针Int是整数的对象表示形式。因为它是一个对象,所以它也可以指向指针*。对吧?

And Pointer Int pointers can point to pointers of any type If I wanted to. Right? It will cause a crash if it doesn't point to a Pointer int. But it will compile successfully, Right?

指针Int指针可以指向任何类型的指针,如果我想的话。对吧?如果它不指向指针int,就会导致崩溃,但它会成功编译,对吧?

But in what scenarios would I prefer using a regular int to a Pointer Int?

但是在什么情况下,我更喜欢使用正则int而不是指针int ?

#4


0  

I would like to add some clarification why you would want to use:

我想补充一些说明,说明您为什么要使用:

@property (nonatomic, copy) NSString * carName;

instead of

而不是

@property (nonatomic, retain) NSString * carName;

The copy keyword implies language semantics that you want to have a COPY of the NSString passed into your current object reference. So the pointer does not change (that is why you don't have to release the object ref).

copy关键字表示希望将NSString的副本传递到当前对象引用的语言语义。所以指针不会改变(这就是为什么你不需要释放对象ref)。

The retain keyword makes it so that you get the pointer which will be retained because the pointer reference changes for this data member (and the current one will be released). Copying a NSString might not be a considerably heavy operation, so copying NSString is used often. You have to be careful what type of property you declare as copy. There might be a considerable amount of effort to produce a copy of types like Dictionaries etc (see shallow, deep copy etc).

retain关键字使您获得将被保留的指针,因为该数据成员的指针引用发生了更改(当前的指针将被释放)。复制NSString可能不是一个非常繁重的操作,因此经常使用复制NSString。你必须小心你声明为拷贝的属性类型。可能需要付出相当大的努力来生成一个类型的副本,如字典等(参见浅、深副本等)。

Hope that helps!

希望会有帮助!