简体   繁体   English

将值添加到NSMutableArray后,将值设置为NSObject类型的对象

[英]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实例:

  • First, you create a new Tutorial with alloc and init , and store a reference to it with the tutorial pointer. 首先,使用allocinit创建一个新的Tutorial ,并使用tutorial指针存储对它的引用。
  • Then you add it to your array. 然后将其添加到阵列中。 Your array retains the object, which means it keeps a reference to it. 您的数组保留对象,这意味着它保留对它的引用。
  • Then you set the title and url properties of your existing object. 然后设置现有对象的titleurl属性。
  • Then you grab another reference to the same object, and call it objNew . 然后你获取对同一个对象的另一个引用,并将其objNew You get this reference by asking the array for a pointer to its first object. 通过向数组请求指向其第一个对象的指针来获得此引用。
  • You then print the properties of the 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编辑教程的时候会显示mytitlemyurl ?” 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. 当您尝试打印存储在数组中的引用的属性时,它会打印mytitlemyurl,因为这是您分配给实际对象属性的内容。

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

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