problem while getting arrays from one class to another class - iphone

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).

Related

Initialize 2 dim array NSMutableArray

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)

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];

How to add an integer to an array?

This must be quite basic, but I was wondering how to add an integer to an array?
I know I can add strings like this:
NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:#"0"];
[trArray addObject:#"1"];
[trArray addObject:#"2"];
[trArray addObject:#"3"];
But I guess I can't simply add integers like so:
NSMutableArray *trArray = [[NSMutableArray alloc] init];
[trArray addObject:0];
[trArray addObject:1];
[trArray addObject:2];
[trArray addObject:3];
At least the compiler isn't happy with that and tells me that I'm doing a cast without having told it so.
Any explanations would be very much appreciated.
Yes that's right. The compiler won't accept your code like this. The difference is the following:
If you write #"a String", it's the same as if you created a string and autoreleased it. So you create an object by using #"a String".
But an array can only store objects (more precise: pointers to object). So you have to create objects which store your integer.
NSNumber *anumber = [NSNumber numberWithInteger:4];
[yourArray addObject:anumber];
To retrive the integer again, do it like this
NSNumber anumber = [yourArray objectAtIndex:6];
int yourInteger = [anumber intValue];
I hope my answer helps you to understand why it doesn't work. You can't cast an integer to a pointer. And that is the warning you get from Xcode.
EDIT:
It is now also possible to write the following
[yourArray addObject:#3];
which is a shortcut to create a NSNumber. The same syntax is available for arrays
#[#1, #2];
will give you an NSArray containing 2 NSNumber objects with the values 1 and 2.
You have to use NSNumbers I think, try adding these objects to your array: [NSNumber numberWithInteger:myInt];
NSMutableArray *trArray = [[NSMutableArray alloc] init];
NSNumber *yourNumber = [[NSNumber alloc] numberWithInt:5];
[trArray addObject: yourNumber];
You can also use this if you want to use strings:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat:#"%d",1]];
[[array objectAtIndex:0] intValue];

how it affects one array to another

-(IBAction)Done :(id) sender {
NSMutableArray *addinfo = [[NSMutableArray alloc]init];
AddinfoCarteV *addinfoCarteV =[[AddinfoCarteV alloc]initWithNibName:#"AddinfoCarteV" bundle:nil];
[addinfo addObject:textfieldToAdd.text];
NSLog(#"%#",addinfo);
addinfoCarteV.informationMan = addinfo ;
textfieldToAdd.hidden= NO ;
}
Please try to ask questions clearly..I think you need to allocate and initialize your addinfoCarteV.informationMan array.or if you want to copy your addinfo array to informationMan array try this code
addinfoCarteV.informationMan = [[NSMutableArray alloc] init];
[addinfoCarteV.informationMan addObjectsFromArray:addinfo];

Passing NSMutableArray from delegate

I'm doing an Iphone aplication and in the delegate class i call a method from another class which return a NSMutableArray filled with the information i need:
NSMutableArray *array = [[NSMutableArray initWithObjects:nil] retain];
array = [xml loadXML:#"info.xml"];
Now I want to pass this array into the viewController class so i can do things with my mutable array. I do the following:
...
[self.window addSubview:viewController.view];
[self.viewController loadLocations:array];
[self.window makeKeyAndVisible];
In delegate, the array is ok, it has the data i want, however, in the viewController class (which is a UIViewController) the array is messed up.
-(void)loadLocations:(NSMutableArray*)_array{
NSLog(#"%f", [[_array objectAtIndex:0] lat]); // This sould be 42.000 but it is 0.00000 and all of the other indexes
You're in trouble right from the beginning:
NSMutableArray *array = [[array initWithObjects:nil] retain];
You're calling "initWithObjects" on "array", but you haven't allocated "array" yet.
You want something like:
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:nil];
or just :
NSMutableArray *array = [[NSMutableArray alloc] init];
This part is invalid:
NSMutableArray *array = [[array initWithObjects:nil] retain];
array = [xml loadXML:#"info.xml"];
The first line is not used because the second line is setting the array pointer to the result of [xml loadXML:]
I think this should suffice:
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[xml loadXML:#"info.xml"]];