Initialize 2 dim array NSMutableArray - iphone

For C I would init an array like this:
NSInteger x[3][10]; That works.
Below I have a one dim array that works. Would like to move all of this to a 2 dim array, How do I init it? So in other words take the code below and make it work with 2 dimensions.
NSMutableArray *SRData;
SRData = [[NSMutableArray alloc] init];
NSMutableDictionary *SRRow;
SRRow = [[NSMutableDictionary alloc] init];
[SRRow setObject:#"Read" forKey:#"Descr"];
[SRRow setObject:#"Read2.png" forKey:#"Img"];
[SRRow setObject:#"Read the codes" forKey:#"Det"];
[SRData addObject:SRRow] ;
[SRRow release];

In Objective-C, you just have to have an array of arrays to get the second dimension. To my knowledge, there is no shorthand, so you're stuck doing something like the following:
NSMutableArray *firstDimension = [[NSMutableArray alloc] init];
for (int i = 0; i < rows; i++)
{
NSMutableArray *secondDimension = [[NSMutableArray alloc] init];
[firstDimension addObject:secondDimension];
}
So all you would do is add your other objects (in your case, the NSMutableDictionarys) to the secondDimension array. Usage would be like:
[[firstDimension objectAtIndex:0] objectAtIndex:0];
Edit
Full code example:
NSMutableArray *SRData = [[NSMutableArray alloc] init]; //first dimension
NSMutableArray *SRRow = [[NSMutableArray alloc] init]; //second dimension
[SRData addObject:SRRow]; //add row to data
[SRRow release];
NSMutableDictionary *SRField = [[NSMutableDictionary alloc] init]; //an element of the second dimension
[SRField setObject:#"Read" forKey:#"Descr"];
//Set the rest of your objects
[SRRow addObject:SRField]; //Add field to second dimension
[SRField release];
Now, to get at that "field" you would use code such as the following:
[[SRData objectAtIndex:0] objectAtIndex:0]; //Get the first element in the first array (the second dimension)

Related

Objective-C and ARC: Why value stored to during its initialization is never read?

I'm using this code with ARC:
NSMutableDictionary *datesDict = [[NSMutableDictionary alloc]init];
NSMutableArray *datesArray = [[NSMutableArray alloc]init];
for (NSString *key in programsArray) {
datesArray = [_onDemandDictionary objectForKey:key];
NSMutableArray *newDates = [[NSMutableArray alloc]init];
int count;
for (count = 0; count <datesArray.count; count++) {
NSMutableDictionary *programsDict = [[NSMutableDictionary alloc]init];
programsDict = [datesArray objectAtIndex:count];
[newDates addObject:[programsDict objectForKey:#"date"]];
}
[datesDict setObject:newDates forKey:key];
}
But when I run the analyzer tool I'm getting value stored to (datesArray and programsDict) during its initialization is never read on lines:
NSMutableArray *datesArray = [[NSMutableArray alloc]init];
programsDict = [datesArray objectAtIndex:count];
Why is this happening how do I get hid of the warning?
Thank you!
The issue is you create a new NSMutableArray and assign it to datesArray at the beginning
NSMutableArray *datesArray = [[NSMutableArray alloc]init];
Then almost immediately after you assign a completely different value to datesArray with
datesArray = [_onDemandDictionary objectForKey:key];
I would just start with
NSMutableArray *datesArray = nil;
It's the same concept for programsDict.
On line 2, you create a new array datesArray.
Then, on line 6 (first line of the for loop), you set a new value to datesArray.
The compiler is just warning you that the line 2 has no effect, and that the code is bugged (in the sense it does not do what you expect).
True, the programsArray could be an empty array, and in this case you want datesArray to just be initialized to use it after the snippet you showed us, but it would be better to make this explicit.
For programsDict, it is even easier: you initialize it with ... alloc] init] then set it to an object of datesArray, making the first operation useless.
You are not using datesArray in your loop, you are simply assigning it values, So either take it nil array like
NSMutableArray* datesArray = nil;
or like
NSMutableArray *datesArray;
to remove waring .

Adding values to two different NSMutableArray without increasing the retain count

I have two simple NSMutableArray that consists of few objects. Some of these objects can be common but need to be stored in both of the arrays as uses of both arrays are defined for totally different purpose.
However, the problem is that after adding same objects to both array, on changing the value of one of the common object, does not reflect in 2nd array.
For example,
Let's say we have two mutable NSArray:
NSMutableArray *mutableArrayOne;
NSMutableArray *mutableArrayTwo;
Now let's create the object definition that these array needs to contain.
#interface: DummyObject : NSObject
{
int objectValue;
}
#property (nonatomic) int objectValue;
-(void) printObjectValue;
#end
Now let's create the base class to store the arrays.
Base Class Definition
#interface: BaseClass : NSObject
{
NSMutableArray *mutableArrayOne;
NSMutableArray *mutableArrayTwo;
}
-(void) init;
-(void) printBothArrays;
#end
Base Class Implementation
#implementation BaseClass
-(void) init
{
// initialize the mutable array.
mutableArrayOne = [[NSMutableArray alloc] initWithCapicity:5];
mutableArrayTwo = [[NSMutableArray alloc] initWithCapicity:5];
DummyObject *dummyObject = [DummyObject alloc];
[dummyObject setObjectValue:5];
DummyObject *dummyObjectTwo = [DummyObject alloc];
[dummyObjectTwo setObjectValue:2];
[mutableArrayOne addObject:dummyObject];
[mutableArrayOne addObject:dummyObjectTwo];
[mutableArrayTwo addObject:dummyObjectTwo];
}
#end
Now let me the modify the DummyObject in array One:
for (DummyObject* dummyObject in mutableArrayOne)
{
[dummyObject setValue:100];
}
Problem
Now here starts the problem when I am printing the values for both array objects:-
Printing First Array
for (DummyObject* dummyObject in mutableArrayOne)
{
[dummyObject printObjectValue];
}
*Output Log (from first array) *
100
100
Printing second Array
for (DummyObject* dummyObject in mutableArrayTwo)
{
[dummyObject printObjectValue];
}
*Output Log (from second array) *
2
So here we can see that MutableArray is keeping the copy of the object, however, I want to store only the reference. That means, on changing the value of the object in 1st array should reflect in 2nd array.
How can we do that?
Do we any other alternative?
Thanks,
Paras Mendiratta
This will ideally work, seems like some issue with setting object.
For e.g. you can check like this -
NSMutableString *firstString = [[NSMutableString alloc] initWithString:#"first"];
NSMutableString *secondString = [[NSMutableString alloc] initWithString:#"second"];
NSMutableArray *originalArray = [[NSMutableArray alloc] initWithObjects: firstString, secondString, nil];
NSMutableArray *copyArray = [[NSMutableArray alloc] initWithObjects: firstString, secondString, nil];
[[copyArray objectAtIndex:0] appendString:#"add some text"];
for (int index = 0; index < [originalArray count]; index++) {
NSLog(#"Original:%# --- copy:%#", [originalArray objectAtIndex:index], [copyArray objectAtIndex:index]);
}
And output is -
2012-05-09 10:28:40.382 Demo[5237:f803] Original:firstadd some text --- copy:firstadd some text
2012-05-09 10:28:40.384 Demo[5237:f803] Original:second --- copy:second
EDIT - (Not adding object at the time of initialization)
NSMutableString *firstString = [[NSMutableString alloc] initWithString:#"first"];
NSMutableString *secondString = [[NSMutableString alloc] initWithString:#"second"];
NSMutableArray *originalArray = [[NSMutableArray alloc] init];
NSMutableArray *copyArray = [[NSMutableArray alloc] init];
[originalArray addObject:firstString];
[copyArray addObject:firstString];
[originalArray addObject:secondString];
[copyArray addObject:secondString];
[[copyArray objectAtIndex:0] appendString:#"add some text"];
for (int index = 0; index < [originalArray count]; index++) {
NSLog(#"Original:%# --- copy:%#", [originalArray objectAtIndex:index], [copyArray objectAtIndex:index]);
}
Output -
2012-05-09 11:11:18.275 Demo[5433:f803] Original:firstadd some text --- copy:firstadd some text
2012-05-09 11:11:18.277 Demo[5433:f803] Original:second --- copy:second

Getting out a undefined number of ViewController from an Array

In my app, I load UIViewController into an array from a .plist, and then, I need to get those VC's out. The problem is, since the number of VC's is not always the same, then I don't know how many I'm getting out each time. So I'm looking for a better engeneered solution - better iteration, rather than hard coding.
For example:
NSMutableArray *views = [[NSMutableArray alloc] init];
for (int i = [currentList count]; i > 0; i--) {
UIViewController *view = [[UIViewController alloc] init];
view.title = [NSString stringWithFormat:#"dgdg - %i", i];
[views addObject:view];
}
So there is my array of VC's, and now:
myIvar = [[CustomSubClass alloc] initWithViewControllers:**help** nil];
I tried:
myIvar = [[CustomSubClass alloc] initWithViewControllers:[views copy], nil];
and:
myIvar = [[CustomSubClass alloc] initWithViewControllers:[NSIndexSet..., nil];
I tried:
myIvar = [[CustomSubClass alloc] initWithViewControllers:[views objectAtIndex:0]... nil];
but none of it worked. Thanks in advance.
The syntax you want is like this:
[[CustomSubClass alloc] initWithViewControllers:[views objectAtIndex:0], [views objectAtIndex:1], [views objectAtIndex:2], nil];
Basically just repeat have all your arguments separated by commas, then always have the final argument be nil.
views is an array and you can pass this directly as a parameter like so:
 myIvar = [[CustomSubClass alloc] initWithViewControllers:views];

Removing Object in NSMutable Array using NSMutable Dictionary

i want to remove object in Mutable array using mutable dictionary, How?
My code :
Employees *Emp7 = [[Employees alloc] init];
Emp7.Number = 7;
Emp7.Name = #"Safa'";
[EmployeesArray addObject:Emp1];
[Dictionary setValue:Emp1 forKey:Emp1.Name];
in the Deleting button (IBAction)
Employees *objEmp2 = [Dic1 objectForKey:objEmp1.Name ];
[Dic1 removeObjectForKey:#"Safa'"];
Please be a little more clear with your question, is this what you are looking for ?
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:values forKeys:keys];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3", nil];
[array removeObject:[dict objectForKey:someKey]];
where 'values' and 'keys' are both arrays and 'somekey' is an object in the 'keys' array.
Please let me know if this helps.
TESTED CODE : 100 % WORKS
NSMutableArray *EmployeesArray = [[NSMutableArray alloc]init];
NSMutableArray *EmployeesArrayToDelete = [[NSMutableArray alloc]init];
Employees *Emp7 = [[Employees alloc] init];
Emp7.number = #"7"; Emp7.Name = #"Safa'";
[EmployeesArray addObject:Emp7];
Employees *Emp3 = [[Employees alloc] init];
Emp3.number = #"7"; Emp3.Name = #"AppleVijay#facebook.com";
[EmployeesArray addObject:Emp3];
Employees *EmployeToDelete = [[Employees alloc] init];
EmployeToDelete.number = #"7";
EmployeToDelete.Name = #"Safa'";
NSLog(#"before delete : %#",EmployeesArray);
for (Employees *eachEmployee in EmployeesArray) {
if ([eachEmployee.Name isEqual:EmployeToDelete.Name] && [eachEmployee.number isEqual: EmployeToDelete.number]) {
[EmployeesArrayToDelete addObject:eachEmployee];
}
}
if ([EmployeesArrayToDelete count]>0) {
[EmployeesArray removeObjectsInArray:EmployeesArrayToDelete];
}
NSLog(#" After delete Employeesarray : %#",EmployeesArray);

problem while getting arrays from one class to another class

i am have 4 arrays in myclass.m
i need to get those arrays into myclassviewcontroller.m
for that i write code in myclassviewcontroller.m like this.
- (void)resultarrays :(NSMutableArray *)Agentids loanofficerid:(NSMutableArray *)Loanofficerid agentname:(NSMutableArray *)agentname agentemail:(NSMutableArray *)agentemail agentphone:(NSMutableArray *)Agentphone {
agentids = [[NSMutableArray alloc] initWithObjects:Agentids,nil];
loanofficerid = [[NSMutableArray alloc] initWithObjects:Loanofficerid,nil];
agentnames = [[NSMutableArray alloc] initWithObjects:agentname,nil];
agentemails = [[NSMutableArray alloc] initWithObjects:agentemail,nil];
agentphone = [[NSMutableArray alloc] initWithObjects:Agentphone,nil];
NSLog(#"123 %#",agentids);
NSLog(#"123 %#",loanofficerid);
NSLog(#"123 %#",agentnames);
NSLog(#"123 %#",agentphone);
}
in myclass.m i write this
myclassviewcontroller *LOVobj = [[myclassviewcontroller alloc]init];
[LOVobj resultarrays:resultData_agent loanofficerid:array1 agentname:array2 agentemail:array3 agentphone:array4];
then it displays all the objects that i print in console.
After this, In the button click i print these arrays then it prints null.
even i assign setter and getter methods to it.
i did n't what's the problem can any one please help me.
Thank u in advance.
First of all, change the code to this:
- (void)resultarrays :(NSArray *)Agentids loanofficerid:(NSArray *)Loanofficerid agentname:(NSArray *)agentname agentemail:(NSArray *)agentemail agentphone:(NSArray *)Agentphone {
agentids = [[NSMutableArray alloc] initWithArray: Agentids];
loanofficerid = [[NSMutableArray alloc] initWithArray: Loanofficerid];
agentnames = [[NSMutableArray alloc] initWithArray: agentname];
agentemails = [[NSMutableArray alloc] initWithArray: agentemail];
agentphone = [[NSMutableArray alloc] initWithArray: Agentphone];
NSLog(#"123 %#",agentids);
NSLog(#"123 %#",loanofficerid);
NSLog(#"123 %#",agentnames);
NSLog(#"123 %#",agentphone);
}
Don't pass mutable array if you don't want it to change.
First of all, you're creating arrays containing references to arrays, not arrays of the objects in the parameter arrays. And since you're storing the references of the parameter arrays, if the contents of the parameter arrays changes, so will all the references.
You probably instead want something like this for each array:
agentids = [NSMutableArray arrayWithArray: Agentids];
(and [agentids retain] since arrayWithArray returns an auto-released object).