NSDictionary initialization results in "EXC_BAD_ACCESS" - objective-c

I can not figure out why this code is bad. I thought it was ok to use static strings in dictionaryWithObjectsAndKeys however it fails at runtime with a EXC_BAD_ACCESS. I used the debugger to identify that it is in fact failing at the definition line. It never outputs "Made it here".
- (NSString *)polyName:(NSUInteger)vertices {
NSLog(#"looking for polyName with %d vertices.", vertices);
NSDictionary *polNameDict = [NSDictionary dictionaryWithObjectsAndKeys:
#"triangle", #"3",
#"square", #"4",
#"pentagon","5",
#"Hexagon", #"6",
#"Heptagon", #"7",
#"Octagon", #"8",
#"Nonagon", #"9",
#"Decagon", #"10",
#"Hendecagon", #"11",
#"Dodecagon", #"12",
nil];
NSLog(#"Made it here");
NSString *key = [NSString stringWithFormat: #"%d", vertices];
NSString *polName = [polNameDict objectForKey:key];
return (polName);
// Memory management: No need to release polNameDict, key, polName because they use
// convenience functions
}

The problem is here:
#"pentagon","5"
It expects an object (NSString) but instead there's a regular string.
Change it to:
#"pentagon", #"5"

What line does the EXC_BAD_ACCESS occur on? Utilize breakpoints and let us know.
For now, if I were you, I'd do something like this:
NSDictionary *polNameDict = [[NSDictionary alloc] initWithObjectsAndKeys:
#"triangle", #"3",
#"square", #"4",
#"pentagon",#"5",
#"Hexagon", #"6",
#"Heptagon", #"7",
#"Octagon", #"8",
#"Nonagon", #"9",
#"Decagon", #"10",
#"Hendecagon", #"11",
#"Dodecagon", #"12",
nil];
instead of how you have it now.

Related

Sorting an NSArray that contains int with particular int

I have some data like this :
1, 5, 2, 9, 7, 6, 3, 8, 0, 4
but I actually want this:
5, 6, 7, 8, 9
only start to 5 .
and I use this:
NSArray *ary = #[ #"1", #"5", #"2", #"9", #"7", #"6", #"3", #"8", #"0", #"4" ];
NSArray *myArray = [ary sortedArrayUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"doubleValue" ascending:YES]]];
NSString *numberExpr =#"^[5-9]";
NSArray *ary = #[ #"1", #"5", #"2", #"9", #"7", #"6", #"3", #"8", #"0", #"4" ];
NSArray *myArray= [ary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self MATCHES %#",numberExpr]];
This solution treats the contents as numeric, as suggested by the question's sample code.
double startingValue = 5;
NSArray *ary = #[ #"1", #"5", #"2", #"9", #"7", #"6", #"3", #"8", #"0", #"4" ];
NSArray *myArray= [ary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.doubleValue >= %#", #(startingValue)]];
myArray = [myArray sortedArrayUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"doubleValue" ascending:YES]]];

sorting array and objetct

How can I order these values ​​in the array in reverse order?
_gradientArrayToWhite = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", #"8", #"9", #"10", nil];
Using one of the sorting methods of NSArray
NSArray * sorted = [_gradientArrayToWhite sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 integerValue] < [obj2 integerValue];
}];
NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:8],[NSNumber numberWithInt:9],[NSNumber numberWithInt:10], nil];
Here You can use your array directly also like this
NSArray *arr1 = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", #"8", #"9", #"10", nil];
NSArray *revArr = [[arr reverseObjectEnumerator] allObjects];
Result = <__NSArrayM 0x71348e0>(
10,
9,
8,
7,
6,
5,
4,
3,
2,
1
)

TouchJSON serializing a structure of dictionaries and arrays

iPhone development question (ObjectiveC).
I'm trying to use TouchJSON library, and having some trouble serialising to JSON. I have ARC switched on so I'm using the ARC branch from github. I'm trying what I imagine to be a fairly a basic nested structure. Three dictionaries inside and array inside a dictionary.
//Make some dictionaries with simple string pairs
NSDictionary *dicA = [NSDictionary dictionaryWithObjectsAndKeys:#"x", #"1", #"y", #"2", nil];
NSDictionary *dicB = [NSDictionary dictionaryWithObjectsAndKeys:#"x", #"1", #"y", #"2", nil];
NSDictionary *dicC = [NSDictionary dictionaryWithObjectsAndKeys:#"x", #"1", #"y", #"2", nil];
//Make an array of dictionary objects
NSArray *saveArray = [NSArray arrayWithObjects:dicA, dicB, dicC, nil];
//Make dictionary which has that array as one of the values
NSDictionary *bigDic = [NSDictionary dictionaryWithObjectsAndKeys:#"arr", saveArray,
#"mmm", #"nnn", nil];
NSData *jsonData = [[CJSONSerializer serializer] serializeObject:saveArray error:NULL];
//This works '[{"1":"x","2":"y"},{"1":"x","2":"y"},{"1":"x","2":"y"}]'
NSData *jsonDataB = [[CJSONSerializer serializer] serializeObject:bigDic error:NULL];
//This fails
When I try to serialize bigDic it bombs out at runtime with the following:
'NSInvalidArgumentException', reason: '-[__NSArrayI UTF8String]:
unrecognized selector sent to instance
Serializing an array on the line above seems to work OK. What's wrong with my bigDic?
After carefully writing out this question I realised where I had gone wrong. Thought I'd post this anyway. Maybe it's a helpful example for others. So the answer is...
I have my dictionaries back to front!
The dictionaryWithObjectsAndKeys method expects the values and keys the other way around so the correct way to build this structure is:
//Make some dictionaries with simple string pairs
NSDictionary *dicA = [NSDictionary dictionaryWithObjectsAndKeys:#"1", #"x", #"2", #"y", nil];
NSDictionary *dicB = [NSDictionary dictionaryWithObjectsAndKeys:#"1", #"x", #"2", #"y", nil];
NSDictionary *dicC = [NSDictionary dictionaryWithObjectsAndKeys:#"1", #"x", #"2", #"y", nil];
//Make an array of dictionary objects
NSArray *saveArray = [NSArray arrayWithObjects:dicA, dicB, dicC, nil];
//Make dictionary which has that array as one of the values
NSDictionary *bigDic = [NSDictionary dictionaryWithObjectsAndKeys:saveArray, #"arr",
#"nnn", #"mmm", nil];
This makes sense when you look at the method name "dictionaryWithObjectsAndKeys", but why it isn't done as "dictionaryWithKeysAndObjects" I have no idea.

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.

Insert NSMutableDictionary with NSDictionaries

I tried to insert NSDictionary's in a NSMutableDictionary. There is no error but it won't work, it remains empty.
Here's my code:
NSMutableDictionary *einnahmen = [[NSMutableDictionary alloc] initWithCapacity:20];
NSArray *objects = [NSArray arrayWithObjects:
name,
[NSNumber numberWithInt: x],
[NSNumber numberWithInt: y],
[NSNumber numberWithInt: z],
nil];
NSArray *keys = [NSArray arrayWithObjects:
#"name",
#"startJahr",
#"zins",
#"entnahmefaehig",
nil];
NSDictionary *entry = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[einnahmen setObject:entry forKey:#"name"]; //seems not to work
After [einnahmen setObject:entry the Debugger shows this:
I have solved this problem. The following code was in the wrong init-method:
NSMutableDictionary *einnahmen =
[[NSMutableDictionary alloc] initWithCapacity:20];
The compiler should show a error.