[英]Set values to an object of type NSObject after adding it to an NSMutableArray
I created a class named as Tutoral which is a sub class of NSObject
, and it has following synthesized properties Title and Url.Then in my viewcontroller I created an instance of the Tutorial class.I also Created an NSMutableArray
object and initialised. 我创建了一个名为Tutoral的类,它是
NSObject
的子类,它具有以下合成属性Title和Url。然后在我的viewcontroller中我创建了一个Tutorial类的实例。我还创建了一个NSMutableArray
对象并进行了初始化。 I added my tutorial object instance to the array. 我将我的教程对象实例添加到数组中。 Then I set the properties such as Title = "mytitle" and Url = "myurl".
然后我设置Title =“mytitle”和Url =“myurl”等属性。 In another place I fetched the tutorial object instance from the array and
NSLog
its Title and Url property values.It shows "mytitle" and "myurl" respectively. 在另一个地方,我从数组中获取了教程对象实例,并在
NSLog
了Title和Url属性值。它分别显示了“mytitle”和“myurl”。 My confusion is that why tutorial object instance which is inside the arry shows its property values. 我的困惑是为什么arry里面的教程对象实例显示了它的属性值。 The tutorial object properties are set after that object is added to the array.
在将该对象添加到数组之后设置教程对象属性。 Follwing are my tested code.
Follwing是我测试过的代码。
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
Tutorial *tutorial = [[Tutorial alloc] init];
[newTutorials addObject:tutorial];
tutorial.title = @"mytitle";
tutorial.url = @"myurl";
Tutorial *objNew = [newTutorials objectAtIndex:0];
NSLog(@"Title %@",objNew.title);
NSLog(@"Url %@",objNew.url);
When you add an object to an array, the array just keeps a reference to that object (a pointer). 将对象添加到数组时,该数组只保留对该对象的引用(指针)。 It doesn't create a copy of that object.
它不会创建该对象的副本。
So, in the code example above, you're always dealing with the same instance of Tutorial
: 所以,在上面的代码示例中,您总是处理相同的
Tutorial
实例:
Tutorial
with alloc
and init
, and store a reference to it with the tutorial
pointer. alloc
和init
创建一个新的Tutorial
,并使用tutorial
指针存储对它的引用。 title
and url
properties of your existing object. title
和url
属性。 objNew
. objNew
。 You get this reference by asking the array for a pointer to its first object. When the following line of code executed, 当执行以下代码行时,
[newTutorials addObject:tutorial];
What it does is , it add the address (reference) of tutorial object which you created in the previous line to the array newTutorials . 它的作用是, 它将您在上一行中创建的教程对象的地址(引用)添加到数组newTutorials中 。
Your confusion is this : " You haven't set the values for the tutorial object inside the array but why is it displaying mytitle and myurl when you NSLog
ed the propeties of tutorials?" 您的困惑是这样的:“您没有在数组中设置教程对象的值,但为什么在
NSLog
编辑教程的时候会显示mytitle和myurl ?” The answer is simple " You've not stored the tutorial object inside the array but you have stored a reference to the tutorial object " 答案很简单“ 你没有将教程对象存储在数组中,但是你已经存储了对教程对象的引用 ”
Since when you've stored the reference and you've done the following to tutorial object : 从您存储引用时起,您已完成以下教程对象:
tutorial.title = @"mytitle";
tutorial.url = @"myurl";
When you try to print the properties of the reference stored in the array, it prints mytitle and myurl because that is what you've assigned to the actual object's properties. 当您尝试打印存储在数组中的引用的属性时,它会打印mytitle和myurl,因为这是您分配给实际对象属性的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.