arrayByAddingObjectsFromArray? - objective-c

I am not sure what I am doing wrong here? I have tried various combinations to try and copy an array into variable mmm. I am trying to learn how to create a 2D array and then run a loop to place init_array into 10 columns.
// NSMutableArray *mmm = [NSMutableArray arrayWithCapacity: 20];
NSMutableArray *kkk = [NSMutableArray arrayWithObjects: #"a", #"b", #"cat", #"dog", nil];
NSMutableArray *mmm; //= [NSMutableArray arrayWithObjects: #"1", #"2", #"3", #"4", nil];
[mmm arrayByAddingObjectsFromArray:kkk];
NSLog(#"Working: %#",[mmm objectAtIndex:3]);
thanks...
so this works from the given answer:
NSMutableArray *mmm = [NSMutableArray arrayWithCapacity: 20];
NSMutableArray *kkk = [NSMutableArray arrayWithObjects: #"a", #"b", #"cat", #"dog", nil];
[mmm addObjectsFromArray:kkk];
NSLog(#"Working: %#",[mmm objectAtIndex:3]);

arrayByAddingObjectsFromArray: returns a new (autoreleased) NSArray object. What you want is addObjectsFromArray:.

arrayByAddingObjectsFromArray: returns a new NSArray that includes the objects in the receiver followed by the objects in the argument. The code you posted there, with mmm unset, will probably just crash since mmm doesn't point to an NSArray object. If you had assigned an array to mmm, then it would return (#"1", #"2", #"3", #"4", #"a", #"b", #"cat", #"dog") — but you don't assign the result to any variable, so it just goes nowhere. You'd have to do something like NSArray *yetAnotherArray = [mmm arrayByAddingObjectsFromArray:kkk].
If you have an NSMutableArray and you want to add objects from another array, use addObjectsFromArray:.

Related

How to count the mutable array objects in iphone?

I am fresher to iOS. In my application i have 3 mutable arrays with objects like
NSMutableArray *MuteItem = [NSMutableArray alloc]initWithObjects:#"a", #"b", #"b", #"c", #"c", #"c", nil]];
NSMutableArray *MuteQuantity = [NSMutableArray alloc]initWithObjects:#"1", #"1", #"1", #"1", #"1", #"1", nil]];
NSMutableArray *MutePrice = [NSMutableArray alloc]initWithObjects:#"4", #"3", #"3", #"6", #"6", #"6", nil]];
Now i need to print that 3 mutable arrays values with counting the same item's quantity and calculate the price also like objects
MuteItem = { a, b, c }
MuteQuantity = { 1, 2, 3 } // counting of same item's quantity like {1, 1+1, 1+1+1}
MutePrice = { 4, 6, 18 } // here addition of same item's prices like {4, 3+3, 6+6+6}
So anybody, would you please help me in this problem. Thanks in advance.
This code will do exactly as you requested, and will even handle any keys in MuteItem, and will generate three new arrays with the aggregate information from each of the three original arrays.
NSMutableArray* muteItem = [[NSMutableArray alloc] initWithObjects: #"a", #"b", #"b", #"c", #"c", #"c", nil];
NSMutableArray* muteQuantity = [[NSMutableArray alloc] initWithObjects: #"1", #"1", #"1", #"1", #"1", #"1", nil];
NSMutableArray* mutePrice = [[NSMutableArray alloc] initWithObjects: #"4", #"3", #"3", #"6", #"6", #"6", nil];
NSMutableArray* setItem = [NSMutableArray array];
NSMutableArray* setQuantity = [NSMutableArray array];
NSMutableArray* setPrice = [NSMutableArray array];
NSSet* itemSet = [NSSet setWithArray: muteItem];
for (NSString* key in itemSet) {
NSIndexSet* indices = [muteItem indexesOfObjectsPassingTest: ^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [obj isEqualToString: key];
}];
__block NSInteger totalQuantity = 0;
__block NSInteger totalPrice = 0;
[indices enumerateIndexesUsingBlock: ^void(NSUInteger idx, BOOL *stop) {
totalQuantity += [[muteQuantity objectAtIndex: idx] integerValue];
totalPrice += [[mutePrice objectAtIndex: idx] integerValue];
}];
[setItem addObject: key];
[setQuantity addObject: [NSNumber numberWithInteger: totalQuantity]];
[setPrice addObject: [NSNumber numberWithInteger: totalPrice]];
}
NOTE: This code assumes you are using ARC. Also, in your original code you forgot to nil terminate your array constructors.
EDIT: I notice that your prices are integers, you may want to change them to floats if your currency uses decimal fractions. This would require changing the definition of totalPrice to float and you would want to change the end of the totalPrice += line from integerValue to floatValue.
EDIT2: Renamed all variables that started with a capital letter as this violates standard naming convention. Only class names should begin with a capital letter, variables should always begin with lowercase, or an _ for instance variables. :)

how to add object from one key NSDictionary in NSMutableArray

I have one NSDictionary and one NSMutableArray And I want store many object from NSDictionary into NSMutableArray from one key but I dont know about it.
this is my code :
//NSArray * a = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"7",#"9", nil];
//NSArray *b = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"0", nil];
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:a,#"number1",b,#"number2", nil];
NSMutableArray *array = [NSMutableArray alloc].... ?
I want store object in number1 key from NSDictionary in NSMutableArray
Since it appears that you store arrays as NSDictionary elements, you can do this:
NSMutableArray *array = [[NSMutableArray alloc] initiWithArray:dic[#"number1"]];
or
NSMutableArray *array = [NSMutableArray arrayWithArray:dic[#"number1"]];
To add object into NsDictionary Array do:
NSMutableDictionary * datos = [array objectAtIndex:i];
[datos setObject:#"hola" objectForKey#"newKey"];

Deleting Set of Indexs from NSMutableArray Efficiently

i have a set of Index setOFIndex = {0,1,3,4}.
now i want to remove this set of index from an NSMutableArray ?
what is the efficient method to do that ?
i have checked for any library method , but not able to find any.
i cant use removeObjectAtIndex directly as at each deletion array will be shifted by one.
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"one", #"a", #"two", #"b", #"three", #"four", nil];
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:1];
[indexes addIndex:3];
[array removeObjectsAtIndexes:indexes];
NSLog(#"array: %#", array);
// Output: array: (one, two, three, four)
read the documentation
You should use NSMUTABleArray's method removeObjectsAtIndexes:
This is the code from documentation
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"one", #"a", #"two", #"b", #"three", #"four", nil];
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:1];
[indexes addIndex:3];
[array removeObjectsAtIndexes:indexes];
NSLog(#"array: %#", array);
I simply don't understand why people don't read documentation.
Why don't you use
– removeObjectsInArray:
Convert your Indexset to array and then use above method.
EDIT:
Very straight forward example from documentation.
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"one", #"a", #"two", #"b", #"three", #"four", nil];
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:1];
[indexes addIndex:3];
[array removeObjectsAtIndexes:indexes];
NSLog(#"array: %#", array);

UITableView crash

Anyone got any ideas why this Table View code is crashing?
This, in my viewDidLoad:
itemArray = [NSArray arrayWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", nil];
Then, this in my cellForRowAtIndexPath method:
cell.textLabel.text = [NSString stringWithFormat:#"Item (%#)", [itemArray objectAtIndex:indexPath.row]];
When I scroll down (i.e. so that object 1 goes off screen) then scroll back to try and see object 1, it crashes at this previous line.
It's fine if I replace the offending line with something like this:
cell.textLabel.text = #"test";
UPDATE: Answer was that the array was not being retained.
This line fixed the problem:
itemArray = [[NSArray arrayWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", nil] retain];
OR
itemArray = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", nil];
Your itemArray seems not to be retained. arrayWithObjects: returns an autoreleased object. You need to retain or copy it, or use the syntactic sugar of a retaining property.

Can NSArray hold NSDictionary in Objective-C/

Can each index of array hold the NSDictionary?
Thank You.
Yes, the value of an NSArray can resolve to an object identifier for an NSDictionary. However, the array doesn't "hold" the NSDictionary, nor can the index of an NSArray be an NSDictionary. An index from an Array is always an integer value.
An NSArray can hold any type of object, so yes, putting an NSDictionary in an NSArray works just fine.
You sure can, here is an example:
NSDictionary *dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
#"value1", #"key1", #"value2", #"key2", nil];
NSDictionary *dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:
#"Billy", #"Goat", #"Rover", #"Dog", nil];
NSArray *arrayWithDictionaries = [[NSArray alloc] initWithObjects:
dict1, dict2, nil];
[dict1 release];
[dict2 release];