addObject to NSMutableArray not working for iPhone App - iphone

There have been a few threads on this topic but none have been able to solve my problem. Essentially I am trying to add a custom object to an NSMutableArray and it doesn't seem to be adding. I don't get any errors but I get a warning saying that my array is an "unused variable" so it looks like it is not getting used. See code below. Any help is appreciated!
Here is the initialization in the app delegate (on run time it says this array is not being used):
NSMutableArray *organArray = [[NSMutableArray alloc] init];
Here is my object class organ.m (I am importing the app delegate, the rootviewcontroller and the organ.h file)
Organ *organObj = [[Organ alloc] initWithPrimaryKey:primaryKey];
organObj.organName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)];
organObj.isDirty = NO;
[appDelegate.organArray addObject: organObj];
[organObj release];
I know the organObj.organName is getting the correct values from my sqlite db because I can output them to the console. They just don't seem to be getting added to the array and the fact that it says the array is not being used means something is wrong.
Thanks in advance

Just a guess but if organArray is intended to be a member of your app delegate, you are creating a new organArray when prefixing it with "NSMutableArray" so if I understand your code, change your app delegate to:
organArray = [[NSMutableArray alloc] init];
instead of:
NSMutableArray *organArray = [[NSMutableArray alloc] init];

Related

Why I can't access NSMutable Array in my method?

I try to add object to my NSMutable array in my method, but keep getting error. It works, if I add the object in init. It doesn't say that anything is wrong until I try to execute the code.
This is below the #import stuff where I declare two arrays:
NSMutableArray *actions1, *actions2;
This is in init:
actions1 = [NSMutableArray array];
Here I try to add 1 to the array:
- (void) storeAction:(int) action {
[actions1 addObject:#"1"];
}
The same code works in int as I said earlier.
I also would like it to store the int value declared "action", but this didn't seem to work either.
[addObject:#"%d", action];
[NSMutableArray array]; is returning an autoreleased object, by the time you try to access it, it is most likely deallocated already. Try [[NSMutableArray alloc] init]; instead. And than you should urgently check the memory management rules.
Try out this code
actions1 = [[NSMutableArray alloc] init];
Hope this helps.
Alternatively, in your header file:
#property(nonatomic, strong)NSMutableArray *actions1;
Then in the implantation file:
#synthesize actions1 = _actions1;
Then you can access your array as self.actions1.

Unable to add NSmutableDictionary in NSmutable Array

Unable to add NSmutableDictionary in NSmutableArray, activitiesFeedArray is a mutable array and initialized in header file.
NSMutableDictionary *dummyitem = [[NSMutableDictionary alloc]init];
NSMutableDictionary *dummyitem2 = [[NSMutableDictionary alloc]init];
NSMutableDictionary *dummyitem3 = [[NSMutableDictionary alloc]init];
[dummyitem setObject:#"No Data Found" forKey:#"text"];
[dummyitem2 setValue:dummyitem forKey:#"Title"];
[dummyitem3 setObject:dummyitem2 forKey:#"ItemInfo"];
NSLog(#"%#",dummyitem3);
//dummyitem3 logs correct value here
if ([activitiesFeedArray count] == 0)
{
NSLog(#"%#",dummyitem3);
//dummyitem3 logs correct value here
[activitiesFeedArray addObject:dummyitem3];
NSLog(#"%#",activitiesFeedArray);
//activitiesFeedArray logs null value here
}
Viewdid load
(void)viewDidLoad
{
[super viewDidLoad];
activitiesFeedArray = [[NSMutableArray alloc]init];
}
Header File
#interface SearchViewController : UIViewController <UISearchBarDelegate ,UITableViewDelegate , UITableViewDataSource>
{
NSMutableArray *activitiesFeedArray;
}
#end
You don't mention where that code is, but it's clearly running some time before viewDidLoad, so the array is stil nil.
If this is the only code you have, then it should work fine and NSLog of your NSMutableArray should not return nil. I copied and pasted your code on my XCode and it works perfectly. You must be doing something somewhere else in your code to alter NSMutableArray.
If you are curious what I did to prove that your code works, I created a simple UIViewController with one button in it. I then created a UIViewController class and literally copied your code in the header file and the implementation file. I declared the NSMutableArray in the header file and initialized it in viewdidload (I copied and pasted them as is). I also put the rest of your NSMutableDictionary code in the viewDidLoad and it produced sensible results. The NSMutableArray log shows that it contains the correct data.
Sometimes it helps to do a clean build but as far as I can tell you, your code is correct and should work.
By the way, I have IOS5 and XCode 4.2.
Replace [dummyitem2 setValue:dummyitem forKey:#"Title"]; with [dummyitem2 setObject:dummyitem forKey:#"Title"];

Objective-C crash issue: NSInvalidArgumentException

I am stuck in it for a long time, but can not find a solution. Here is my code:`
NSLog(#"[tempArray retainCount]: %d",[tempArray retainCount]);
tempArray = [[NSMutableArray alloc] initWithArray:[allRemainingProductsDictionary objectForKey:[[allRemainingProductsDictionary allKeys]objectAtIndex:counter]]];
NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] init];
[tempDictionary setObject:productName forKey:#"name"];
[tempArray release];
I am getting a NSException crash with this report. Please help.
The method getObjects:range: that is being sent to your NSDictionary instance is a NSArray method.
You're probably trying to to your initWithArray passing a NSDictionary instead of a NSArray.
Is the NSLog entry showing up? If not, it's because tempArray does not respond to retainCount. You don't need to worry about anything with release or retain if you're using Xcode 4.2 with ARC for iOS 5 (which you should, unless you have legacy code).
Otherwise, somewhere you're sending an object a message it doesn't respond to.

Objective C two-dimensional array memory issues

I'm trying to declare a two-dimensional array as an instance variable in Objective C. I've got the NSMutableArray in the header (data), along with the #property (nonatomic, retain). In viewDidLoad: I have:
data = [[NSMutableArray alloc] init];
[data addObject:[[NSArray alloc] initWithObjects:#"Cheese", #"Meat", #"Veggie", nil]];
[data addObject:[[NSArray alloc] initWithObjects:#"Sandwich", #"Soup", #"Stew", nil]];
I can NSLog the array within the method and it is correct, however when I try to Log it from a separate method I get nothing (just "#"), and if I try to access with
NSInteger num = [[data objectAtIndex:component] count];
it crashes with no error in the log. I'm sure this is something to do with not allocating memory properly, however I am new to Obj C and haven't worked with a C-style language in many years. FWIW, I have tried many variants of this that all fail, including using NSArray instead of mutable, [NSArray arrayWithObjects] instead of [[NSArray alloc] initWithObjects], and every combination in between.
try creating the outer array like this:
self.data = [NSMutableArray arrayWithCapacity:2]; // assuming you're only adding 2 inner arrays.
The following may be a right way.
self.data = [NSMutableArray array];
[data addObject:[NSArray arrayWithObjects:#"Cheese", #"Meat", #"Veggie", nil];
[data addObject:[NSArray arrayWithObjects:#"Sandwich", #"Soup", #"Stew", nil];
Note that, as #jamihash commented above, you need self.data to properly retain the array. And, there is no need to alloc the NSArray which you are adding to data.
As a side issue, you're retaining the child arrays twice. They get retained when you add them to your NSMutableArray, so you should probably autorelease them on creation or create them with one of the NSArray methods that returns an autoreleased array.
Your code by itself shouldn't crash. You should look into where and when you release and retain the NSMutableArray. You could post more of the code and I'm sure somebody will spot the problem.

Why Instruments report a leak?

I am developing an iphone app. Instruments reported a leaked object ServiceTypes. Below is the relevant code. Does anyone have any ideas? Thanks a lot for your help.
ServiceTypes *serviceTypes = [[ServiceTypes alloc] init];
if ([userConnection getServiceTypes:serviceTypes]) {
if ([serviceTypes.types length] > 0) {
NSArray *array = [[NSArray alloc] initWithArray:[serviceTypes.types componentsSeparatedByString: SERVICE_TYPE_DELIMITOR]];
serviceRequestTypes = [[NSMutableArray alloc] initWithArray:array];
[array release];
}
}
[[self typesTableView] reloadData];
[serviceTypes release];
It doesn't look like serviceTypes is being leaked. From the code you posted, serviceTypes is always released at the end of the method, and it doesn't appear to be retained anywhere in your sample. My question is: what happens inside getServiceTypes:. Does that method retain the serviceTypes parameter?
One more thing. If serviceRequestTypes is an instance variable (and it looks like it is), then you may be leaking memory by reassigning it without releasing the existing serviceRequestTypes object first. You should either rewrite serviceRequestTypes to be a property and use a synthesized accessor or make sure to release it every time before assigning. If its current value is nil, no big deal; the release message will simply be ignored. For example:
[serviceRequestTypes release];
serviceRequestTypes = [[NSMutableArray alloc] initWithArray:[serviceTypes.types componentsSeparatedByString:SERVICE_TYPE_DELIMITER]];