my attempt to init an array with a number of bool values using:
[myArray initWithObjects:[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
[NSNumber numberWithBool:YES],
nil];
seems to fail since the debugger shows an empty array after this statement is carried out ... Any clues?
Make sure you are alloc-ing the object, as well, i.e.:
NSArray *myArray = [[NSArray alloc] initWithObjects:...];
...
[myArray release];
Or:
NSArray *myArray = [[[NSArray alloc] initWithObjects:...] autorelease];
Or:
NSArray *myArray = [NSArray arrayWithObjects:...];
Related
I am getting strange exception when calling the NSDictionary in the following example:
NSInteger userId = 1;
NSDictionary *postData = [NSDictionary dictionaryWithObjectsAndKeys:
#"3", #"a",
#"0", #"b",
userId, #"c",
nil];
Can someone see what's the problem above?
NSDictionary, as most collections, just accepts real Obj-C objects (id type), but you are passing a NSInteger which is a normal C typedef.
Try with NSNumber userId = [NSNumber numberWithInt:1];
// Configure the cell.
cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row] objectForKey:#"Name"];
return cell;
The above code is generating this exception. What could be causing this and how can I fix it?
My Understand from Your question
You have array of drinks like
Drink *d1 = [[Drink alloc] init]
d1.name = #"Drink1";
d1.price = [NSNumber numberWithFloat:25.0];
Drink *d2 = [[Drink alloc] init]
d2.name = #"Drink2";
d2.price = [NSNumber numberWithFloat:35.0];
OR
NSArray *values1 = [NSArray arrayWithObjects:#"Drink1",[NSNumber numberWithFloat:25.0], nil];
NSArray *keys1 = [NSArray arrayWithObjects:#"Name",#"price", nil];
NSDictionary *d1 = [[NSDictionary alloc] initWithObjects:values1 forKeys:keys1];
NSArray *values2 = [NSArray arrayWithObjects:#"Drink2",[NSNumber numberWithFloat:55.0], nil];
NSArray *keys2 = [NSArray arrayWithObjects:#"Name",#"price", nil];
NSDictionary *d2 = [[NSDictionary alloc] initWithObjects:values1 forKeys:keys1];
self.drinks = [NSArray arrayWithObjects:d1,d2, nil];
[d1 release];
[d2 release];
From the above case
cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row] objectForKey:#"Name"];
the above statement is correct
You are getting exception means, You are inserting the objects into drinks array, that supports Key Value Pairs data and that is String Data.
I hope you are having like this
self.drinks = [NSArray arrayWithObjects:#"Drink1",#"Drink2", nil];
In this above case you have to go with
cell.textLabel.text = [self.drinks objectAtIndex:indexPath.row];
first try to determine what object is self.drinks,
then you can go finer depending if is an array with dictionaries?
do a log
[self.drinks objectAtIndex:0]
what do you see?
I wanted to know how (if it is possible) to create an NSDictionary that holds an NSArray for every key.
Something like that:
#"FirstKey" - [0], [1], [2], [3]
#"SecondKey" - [0], [1], [2], [3]
#"ThirdKey" - [0], [1], [2], [3]
#"FourthKey" - [0], [1], [2], [3]
Hope you got the idea, thanks ahead!
You can add NSArray instances (or any other collection) as values of an NSDictionary:
NSDictionary *d = [[NSMutableDictionary alloc] init];
[d setValue:[NSArray arrayWithObjects:
[NSNumber numberWithInteger:0],
[NSNumber numberWithInteger:1],
...,
nil]
forKey:#"FirstKey"];
[d setValue:[NSArray arrayWithObjects:
[NSNumber numberWithInteger:0],
[NSNumber numberWithInteger:1],
...,
nil]
forKey:#"SecondKey"];
//etc.
Alternatively, if you don't have too many keys/values:
NSArray *values1 = [NSArray arrayWithObjects:
[NSNumber numberWithInteger:0],
[NSNumber numberWithInteger:1],
...,
nil];
NSArray *values2 = [NSArray arrayWithObjects:
[NSNumber numberWithInteger:0],
[NSNumber numberWithInteger:1],
...,
nil];
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
values1, #"FirstKey",
values2, #"SecondKey",
...,
nil];
That's totally possibly. Simplest way is to create your NSArray and then just add that as the object for the key to your NSDictionary
NSArray *myArray = [NSArray arrayWithObjects:#"One",#"Two", nil];
[myDictionary setObject:myArray forKey:#"FirstKey"];
Yes, you can store any object in a dictionary. For numeric values you will need to store them as NSValue or NSNumber objects.
How Can we Store An group OF NSString Objects to an Single Array.....
starting from index = 0;
NSArray *array = [NSArray arrayWithObjects:string1, string2, string3, nil];
or
NSMutableArray *array = [NSMutableArray array];
[array addObject:string1];
[array addObject:string2];
pick one, or improve your question if I've misunderstood you.
An example:
NSArray *myStringArray = [NSArray arrayWithObjects:#"String", #"Another String", #"Last string", nil];
NSArray *myStringArray = [NSArray arrayWithObjects: string1, string2, string3, ..., stringn, nil];
In the following code, the first log statement shows a decimal as expected, but the second logs NULL. What am I doing wrong?
NSDictionary *entry = [[NSDictionary alloc] initWithObjectsAndKeys:
#"x", [NSNumber numberWithDouble:acceleration.x],
#"y", [NSNumber numberWithDouble:acceleration.y],
#"z", [NSNumber numberWithDouble:acceleration.z],
#"date", [NSDate date],
nil];
NSLog([NSString stringWithFormat:#"%#", [NSNumber numberWithDouble:acceleration.x]]);
NSLog([NSString stringWithFormat:#"%#", [entry objectForKey:#"x"]]);
You are exchanging the order in which you insert objects and key: you need to insert first the object, then the key as shown in the following example.
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:#"value1", #"key1", #"value2", #"key2", nil];
new Objective-c supports this new syntax for static initialisation.
#{key:value}
For example:
NSDictionary* dict = #{#"x":#(acceleration.x), #"y":#(acceleration.y), #"z":#(acceleration.z), #"date":[NSDate date]};
NSDictionary Syntax:
NSDictionary *dictionaryName = [NSDictionary dictionaryWithObjectsAndKeys:#"value1",#"key1",#value2",#"key2", nil];
Example:
NSDictionary *importantCapitals = [NSDictionary dictionaryWithObjectsAndKeys:
#"NewDelhi",#"India",#"Tokyo",#"Japan",#"London",#"UnitedKingdom", nil];
NSLog(#"%#", importantCapitals);
Output looking like,
{India = NewDelhi; Japan = Tokyo; UnitedKingdom = London; }