为什么我们总是分配Objective-C原语?

时间:2022-09-07 11:20:24

It's common knowledge that we should use the attribute (assign) on primitive properties in Objective-C, yet I've never had a clear explanation as to why we do this.

众所周知,我们应该在Objective-C中对原始属性使用属性(assign),但我从来没有清楚地解释为什么我们这样做。

consider

考虑

[myObject setIntProperty:22];

for class myObject that has a @property (nonatomic, assign) int intProperty;

对于具有@property(非原子,赋值)int intProperty的类myObject;

My imagination says we would be assigning intProperty a pointer to stack memory that holds the value 22. So since that pointer has to live on for our property to have any value, does this mean that every primitive has an unlimited lifetime in stack memory?

我的想象力说我们将为intProperty分配一个指向堆栈内存的指针,该指针保存值为22.因为该指针必须依赖于我们的属性才能拥有任何值,这是否意味着每个原语在堆栈内存中具有无限的生命周期?

Some more code to illustrate what I don't understand:

更多代码来说明我不理解的内容:

MyObject.h ----

MyObject.h ----

@interface MyObject : NSObject
@property (nonatomic, assign) int myInt;
@property (nonatomic, assign) NSString* myString;
@end

OtherFile.m ----

OtherFile.m ----

-(void) doSomething
{
   int x = 22;
   NSString* str = @"something";

   MyObject* obj = [[MyObject alloc] init];

   obj.myInt = x;
   obj.myString = str;

   [self doSomethingInOtherFunctionWith:obj];
}

Now once the doSomething function has completed and moved on, the assigned string myString now points to nothing, since it shared a pointer with the function's local str, which is destroyed after the function runs. But myInt remains as 22.

现在一旦doSomething函数完成并继续运行,分配的字符串myString现在指向什么,因为它与函数的本地str共享一个指针,该函数在函数运行后被销毁。但是myInt仍然是22岁。

I understand that this is the way it works, being passed by value, and I do not question HOW it works, just WHY. Doesn't an int have to be a pointer at some level?

我知道这是它的工作方式,通过价值传递,我不会质疑它是如何工作的,只是为什么。 int不是必须在某个级别的指针?

1 个解决方案

#1


1  

Why do we always assign Objective-C primitives?

为什么我们总是分配Objective-C原语?

Because we can't retain or copy them - that's for Objective-C objects only.

因为我们无法保留或复制它们 - 仅适用于Objective-C对象。

My imagination says we would be assigning intProperty a pointer to stack memory

我的想象力说我们将为intProperty分配一个指向堆栈内存的指针

No, not really. We are assigning an int to it. Of course, internally, the instance variable is implemented as a pointer-to-int into the myObject object, with some offset added (described by the instance variable layout of the class).

不,不是真的。我们正在为它分配一个int。当然,在内部,实例变量实现为指向myObject对象的指针,并添加了一些偏移量(由类的实例变量布局描述)。

But if by "stack variable", you mean "objects [that's the C terminology for variables] with automatic storage duration", then no. Objective-C objects are usually allocated dynamically (maybe "on the heap", but that's really irrelevant), and live as long as they are not deallocated, i. e. until their reference count reaches zero. Then they are deallocated, destroyed, and all of their instance variables (which back the corresponding @propertyes) are invalidated as well.

但是,如果通过“堆栈变量”,你的意思是“对象[这是变量的C术语]具有自动存储持续时间”,那么没有。 Objective-C对象通常是动态分配的(可能是“在堆上”,但这确实是无关紧要的),并且只要它们没有被释放,就可以生存,i。即直到他们的参考计数达到零。然后它们被解除分配,销毁,并且它们的所有实例变量(支持相应的@propertyes)也被无效。

#1


1  

Why do we always assign Objective-C primitives?

为什么我们总是分配Objective-C原语?

Because we can't retain or copy them - that's for Objective-C objects only.

因为我们无法保留或复制它们 - 仅适用于Objective-C对象。

My imagination says we would be assigning intProperty a pointer to stack memory

我的想象力说我们将为intProperty分配一个指向堆栈内存的指针

No, not really. We are assigning an int to it. Of course, internally, the instance variable is implemented as a pointer-to-int into the myObject object, with some offset added (described by the instance variable layout of the class).

不,不是真的。我们正在为它分配一个int。当然,在内部,实例变量实现为指向myObject对象的指针,并添加了一些偏移量(由类的实例变量布局描述)。

But if by "stack variable", you mean "objects [that's the C terminology for variables] with automatic storage duration", then no. Objective-C objects are usually allocated dynamically (maybe "on the heap", but that's really irrelevant), and live as long as they are not deallocated, i. e. until their reference count reaches zero. Then they are deallocated, destroyed, and all of their instance variables (which back the corresponding @propertyes) are invalidated as well.

但是,如果通过“堆栈变量”,你的意思是“对象[这是变量的C术语]具有自动存储持续时间”,那么没有。 Objective-C对象通常是动态分配的(可能是“在堆上”,但这确实是无关紧要的),并且只要它们没有被释放,就可以生存,i。即直到他们的参考计数达到零。然后它们被解除分配,销毁,并且它们的所有实例变量(支持相应的@propertyes)也被无效。