ios - Add objects to NSMutableArray that don't reference each other -


first question: i'm attempting add nsmutabledictionary objects nsmutablearray using method additem.

-(void)additem:(id)obid name:(id)obname description:(id)obdesc menuname:(id)obmname menuid:(id)obmid mid:(id)obmid pod:(id)pod type:(id)obtype price:(id)price {     nsmutabledictionary *itemtoadd = [[nsmutabledictionary alloc]init];     [itemtoadd setvalue:obid forkey:@"id"];     [itemtoadd setvalue:obname forkey:@"name"];     [itemtoadd setvalue:obdesc forkey:@"description"];     [itemtoadd setvalue:obmname forkey:@"mname"];     [itemtoadd setvalue:obmid forkey:@"menu_id"];     [itemtoadd setvalue:obmid forkey:@"mid"];     [itemtoadd setvalue:pod forkey:@"pod"];     [itemtoadd setvalue:obtype forkey:@"type"];     [itemtoadd setvalue:price forkey:@"price"];     [itemtoadd setvalue:@"1" forkey:@"amount"];      [items addobject:itemtoadd]; } 

however when method called again later next object added overwrites value of object same keys. im aware because array retains memory address, hence when same object different type "obtype" added of values change.

how able add objects nsmutablearray without them referencing each other?

example:

output after adding 1 object:

        {     amount = 1;     pod = both;     type =         {         0 = vegetarian;         1 = aussie;     };     description = "online special - 2 large pizza's $25 (no half halves or toppings!) enjoy!";     id = 7596;     "menu_id" = 112;     mid = 112;     mname = "deal";     name = "hungry? 2 large pizzas!"; } 

)

output after adding object of same id of different type:

    {     amount = 1;     pod = both;     type =         {         0 = "plain";         1 = aussie;     };     description = "online special - 2 large pizza's $25 (no half halves or toppings!) enjoy!";     id = 7596;     "menu_id" = 112;     mid = 112;     mname = "deal";     name = "two large pizzas!"; },     {     amount = 1;     pod = both;     type =         {         0 = "plain";         1 = aussie;     };     description = "online special - 2 large pizza's $25 (no half halves or toppings!) enjoy!";     id = 7555;     "menu_id" = 112;     mid = 112;     mname = "deal";     name = 2 large pizzas!"; } 

)

as can seen both object types have changed.

ok solved problem, not 100% sure why. passing in type object nsmutabledictionary if item had 2 variations, 1 object in dictionary have key of 0 other 1 when passed in overwrite each other. passing values in nsarray fixes this.

done passing in:

nsarray * values = [selecteditemoptions allvalues]; 

thanks helped.

i can't give answer, there not enough information provided that, maybe can figure out yourself. write:

im aware because array retains memory address, hence when same object different type "obtype" added of values change.

that not happens in code, though there situations in array may appear change - , seeing. line:

nsmutabledictionary *itemtoadd = [[nsmutabledictionary alloc]init]; 

generates new, never before seen, mutable dictionary. can log address (effectively identity(1)) of object adding:

nslog(@"created itemtoadd: %p", itemtoadd); 

after above line. %p format produce address of newly created object, , every object has unique address. when line:

[items addobject:itemtoadd]; 

the new dictionary added whichever array referenced items - , very important. variable, such items , itemtoadd, references object, not object itself. array being referenced items change between calls additem:. should check , can adding(2):

nslog(@"additem called, items = %p containing %ld element", items, items.count); for(id in items)    nslog(@"    element: %p", something); 

to start of method. first tell address of array referenced items , how many items array contains. for loop output address of each element of array (if there any).

further note addobject: add item array, doesn't matter if same item in array - add twice , array appears have 2 elements, both of reference same item. when above code displays count should increasing unless removing elements array somewhere.

add statements , @ results. among possibilities produce results reporting:

  • the array referenced items changing
  • the dictionary add being removed
  • etc.

hth


(1) addresses can reused, after object no longer required , destroyed arc memory may reused new object. address not strictly identify unique object, need aware of this

(2) assuming 64-bit here, if not need change %ld format or add cast.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -