Problem removing multiple objects from NSMutableArray iPhone SDK - iphone

I am having trouble removing objects from nsmutable array. Here it is.
NSURL *url = [NSURL URLWithString:#"http://www.lockerz.com/dailies"]];
NSData *datadata = [NSData dataWithContentsOfURL:url];
NSString *removeForArray = [[NSString alloc] initWithData:datadata encoding:NSASCIIStringEncoding];
NSArray *theArray = [removeForArray componentsSeparatedByString:#" "];
NSMutableArray *deArray = [[NSMutableArray array] initWithArray:theArray];
[deArray removeObjectsInRange:NSMakeRange(0, 40)];
NSLog(#"%#", deArray);

+[NSMutableArray array] already returns an initialized array. Don't use an initializer method on that, they are used on new instances that you allocd.
In this case you can either
alloc/init an instance
use -mutableCopy
use a suitable convenience constructor
The three following lines are equivalent:
NSMutableArray *a = [[theArray mutableCopy] autorelease];
NSMutableArray *b = [NSMutableArray arrayWithArray:theArray];
NSMutableArray *c = [[[NSMutableArray alloc] initWithArray:theArray] autorelease];

Related

Why does the following lines of code not adding anything to the array and the array just produces null

Why does the following lines of code not adding anything to the array and the array just produces null.
NSString *LIstringURL = [[NSString alloc] initWithFormat:#"http://farm%#.staticflickr.com/%#/%#_%#.jpg",flickrfarmID,flickrServer, flickrID,flickrSecret];
NSString *SIstringURL = [[NSString alloc] initWithFormat:#"http://farm%#.staticflickr.com/%#/%#_%#_t.jpg",flickrfarmID,flickrServer, flickrID,flickrSecret];
// NSURL *url = [[NSURL alloc] initWithString:stringURL];
NSArray *bothImagesArray = [NSArray arrayWithObjects:LIstringURL,SIstringURL, nil];
[self.urlArray addObject:bothImagesArray];
check the mutable array is initiated or not
self.urlArray = [[NSMutableArray alloc]init];

How to add GET parameters to an ASIHttpRequest?

How can I add GET parameters to an ASIHttpRequest?
I want to go from http://mysite.com/server to http://mysite.com/server?a=1&b=2 programmatically.
I have all my parameters as key-value pairs in an NSDictionary object.
Thanks
Use string format like
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:#"1",#"a",#"2",#"b", nil];
NSMutableString *prams = [[NSMutableString alloc] init];
for (id keys in dict) {
[prams appendFormat:#"%#=%#&",keys,[dict objectForKey:keys]];
}
NSString *removeLastChar = [prams substringWithRange:NSMakeRange(0, [prams length]-1)];
NSString *urlString = [NSString stringWithFormat:#"http://mysite.com/server?%#",removeLastChar];
NSLog(#"urlString %#",urlString);

Number of Objects (3) not equal to number of keys(8)

Ok, so the issue is coming from trying to pull data from a sqlite DB and place it in an array for a scroll view display. Im using FM Database library to connect to sql database
The code is as follows:
NSMutableArray *data = [[NSMutableArray alloc] init];
FMResultSet *result = [[[StorageTank sharedStorageTank] DB]
executeQuery:#"SELECT * FROM table"];
while([result next])
{
NSArray *values = [[NSArray alloc] initWithObjects:
[[NSNumber alloc] initWithInt:[result intForColumn:#"id"]],
[[NSNumber alloc] initWithInt:[result intForColumn:#"count"]],
[[NSNumber alloc] initWithInt:[result intForColumn:#"required"]],
[result stringForColumn:#"image_portrait"],
[result stringForColumn:#"image_landscape"],
[[NSNumber alloc] initWithInt:[result intForColumn:#"end_date"]],
[[NSNumber alloc] initWithInt:[result intForColumn:#"active"]],
[result stringForColumn:#"merchant"], nil];
NSLog(#"%#", values);
NSArray *keys = [[NSArray alloc] initWithObjects: #"id",#"count",#"required",
#"image_portrait",#"image_landscape",
#"end_date",#"active",#"merchant",nil];
NSLog(#"%#", keys);
NSDictionary *row = [[NSDictionary alloc] initWithObjects: values forKeys: keys];
[data addObject: row];
}
NSArray *resultArray = [[NSArray alloc] init];
resultArray = data;
So, obviously from the code i've tested to make sure the values count is equal to the keys count... yet Im still getting this error:
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithObjects:forKeys:]: number of objects (3) not equal to number of keys (8)'"
I can't understand for the life of me why the count would differ if when I print out the values array I see 8 values... which should match my 8 keys? and they are correct?
Any help/direction would be greatly appreciated!
Thanks,
Is the fourth item in your values array:
[result stringForColumn:#"image_portrait"]
returning nil? That's the value that tells -initWithObjects that the list is done.

Memory leaks when deleting row from a tableview

I am loading data from a plist into a tableview in my application. Data is stored in a mutable dictionary of mutable dictionaries.
Here is my viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Categories";
// load data from plist fle
self.categories = [[[NSMutableDictionary alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"InventoryItems" ofType:#"plist"]] autorelease];
// add buttons to navigation menu
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.leftBarButtonItem = self.addButton;
}
My tableview is editable, so user can delete categories. In my commitEditingStyle:forRowAtIndexPath: method I update my data model:
[self.categories removeObjectForKey: [[self.categories allKeys] objectAtIndex:indexPath.row]];
When I profile my app it leaks memory. I am not very proficient in using the profile tool, but it seems to find leaks in my categories dictionary every time I delete a row.
I wonder where I missed releasing something? Is it a problem that object I am removing is a dictionary as well and I need to remove its objects too?
This leaks (if the property is retain or copy):
self.categories = [[NSMutableDictionary alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"InventoryItems" ofType:#"plist"]];
Use this instead:
categories = [[NSMutableDictionary alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"InventoryItems" ofType:#"plist"]];
or this:
self.categories = [[[NSMutableDictionary alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"InventoryItems" ofType:#"plist"]] autorelease];
I wonder if this would give you anything different?
NSMutableDictionary *d = [[NSMutableDictionary alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:#"InventoryItems" ofType:#"plist"]];
self.categories = d;
[d release];

Values not appending to my NSMutableDictionary

I'm trying to add some vales to a NSMutableDictionary dynamically. However, using the following code, I'm adding values using the first letter as the key to a temporary dictionary and then finally adding it to my names dictionary but it just overwrites each value for it's corresponding key
NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
for (NSString *drugStr in listContents) {
NSString *substring = [drugStr substringToIndex:1];
[dictionary setValue:drugStr forKey:substring];
}
names = [[NSDictionary alloc] initWithDictionary:dictionary];
[dictionary release];
What am I doing wrong?
You should define your NSDictionary to use NSString as a key, and NSArray as a value.
Then you should just retrieve your value according to the given key. If the result is nil, then you need to create a new NSMutableArray, to which you add the value above. IF the result is not-nil, add the value to the array.
NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
for (NSString *drugStr in listContents) {
NSString *substring = [drugStr substringToIndex:1];
NSMutableArray *valueArray = (NSMutableArray*)[dictionary objectForKey:substring];
if(valueArray==nil){
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:5];
[newArray addObject:drugStr];
[dictionary setObject:newArray forKey:substring];
}else{
[valueArray addObject:drugStr];
}
}
names = [[NSDictionary alloc] initWithDictionary:dictionary];
[dictionary release];