Here is the code im having issues with:
if(DriveInfoDict) {
NSLog(#"%#", DriveInfoDict);
//PrevSpeedsDict = [DriveInfoDict objectForKey: #"speed"];
//NSLog(#"Previous Speed Dict:%#", PrevSpeedsDict);
}
DriveInfoDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble: CurrentLatitude], #"Lat",
[NSNumber numberWithDouble: CurrentLongitude], #"Long",
[NSNumber numberWithDouble:speedMPH], #"speed",
nil];
Here, I would like to set DriveInfoDict, so that the next time the function runs it will have the previous value. I have stripped the operators to simplify my problem.
The error I am getting is : EXC-BAD-ACCESS
I am new to Obj-C and I do not know how to make this object accessible here. Some code with explanation as to if it goes in the H or M file would be very helpful.
You need to retain the dictionary or use alloc/init (which returns a retained dictionary. So either:
DriveInfoDict = [[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble: CurrentLatitude], #"Lat",
[NSNumber numberWithDouble: CurrentLongitude], #"Long",
[NSNumber numberWithDouble:speedMPH], #"speed",
nil] retain];
or:
DriveInfoDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble: CurrentLatitude], #"Lat",
[NSNumber numberWithDouble: CurrentLongitude], #"Long",
[NSNumber numberWithDouble:speedMPH], #"speed",
nil];
If you replace the content of DriveInfoDict (that is: assign a new dictionary) don't forget to first release it.
Related
Which type I should use to save many objects with same key?
I should post data to server where one of parameter is suggestedTo and it contains userId.
This parameters should be more then one. So I'm confused which data type I should use to save them.
For example array or dictionary should looks like
{
#"suggestedTo" = 111,
#"suggestedTo" = 222,
#"suggestedTo" = 333,
etc.
}
This is typically handled with a dictionary of sets (or arrays if the data is ordered). So in this case, you'd have something like:
NSSet *suggestedTo = [NSSet setWithObjects:[NSNumber numberWithInt:111],
[NSNumber numberWithInt:222],
[NSNumber numberWithInt:333], nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:suggestedTo,
#"suggestedTo", nil];
You could use a dictionary of arrays
NSArray *suggestedTos = [[NSArray alloc] initWithObjects:
[NSNumber numberWithInt:111],
[NSNumber numberWithInt:222],
[NSNumber numberWithInt:333], nil];
NSDictionary *myDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
suggestedTos, #"suggestedTo", nil];
I want to convert characters into integers based on predetermined values, for example:
a = 0
b = 1
c = 2
d = 3
etc...
Right now I'm doing it with an If/Else If, I just want to know if there is a faster/better way I should be doing it because the list of conversions may get quite long.
Here's what I'm using now:
-(NSInteger)ConvertToInt:(NSString *)thestring {
NSInteger theint;
if([thestring isEqualToString:#"a"] == YES){
theint = 0;
} else if ([thestring isEqualToString:#"b"] == YES){
theint = 1;
} //etc...
return theint;
}
This works fine, but as I said, if it makes more sense can I create an array with all the key/values then just run through that to return the integers?
Please provide examples as I'm a beginner with Objective C/iOS. I come from Web languages.
Thanks!
EDIT: Thanks for the help everyone. I used taskinoors answer but I replaced the NSDictionary which was giving error messages with this:
NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0], #"a",
[NSNumber numberWithInt:1], #"b",
[NSNumber numberWithInt:2], #"c", nil];
unichar ch = [thestring characterAtIndex:0];
theint = ch - 'a';
Note that, 'a' with a single quote is character a, not string "a".
If the values are not regular like your example then you can store all predefined values into a dictionary. For example:
"a" = 5;
"b" = 1;
"c" = 102;
NSArray *values = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],
[NSNumber numberWithInt:1], [NSNumber numberWithInt:102], nil];
NSArray *keys = [NSArray arrayWithObjects:#"a", #"b", #"c", nil];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:values forKeys:keys];
theint = [[dic valueForKey:thestring] intValue];
If you wanted to keep some flexibility in what strings map to what integers, and your integers run from 0 to n-1 where you have n unique items in the array, you could do something like this:
-(NSInteger)ConvertToInt:(NSString *)thestring {
NSArray *arr = [NSArray arrayWithObjects:#"a", #"b", #"c", #"d", nil];
NSInteger theint = [arr indexOfObject:thestring];
return theint;
}
Now this will build the array each time, which would be very inefficient, the optimal way would be to build the array once in your class, and then just use a reference to that array with the indexOfObject method call.
Here is where i specify how many rows uipicker should have
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 15;
return numRows;
}
With that implementation pickers gives option for choose from 0 to 14. What to do so the picker to show the values from 2 to 14 (not to show 0, 1 and 2) ?
//EDIT
self.totalPlayers = [NSArray arrayWithObjects:
[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],
[NSNumber numberWithInt:11],
[NSNumber numberWithInt:12],
[NSNumber numberWithInt:13],
[NSNumber numberWithInt:14],
[NSNumber numberWithInt:15],
nil];
The ideal way to do this would be to use an Array.
instead of returning hardcoded length of 15, return array count
Make changes in the array to leave out 0,1,2 or any particular row for that matter.
I'm having some trouble with SBJsonWriter at the moment.
I need to send a request that contains a json object of name/value pairs. e.g.
[{%22uid%22:1,%22version%22:1}]
I can't figure out how to do this in Obj-C with the SBJson Writer framework.
For each pair I have tried to construct a dictionary then add the dictionary to an array. This results in an array containing many dictionaries, each containing one name/value pair.
Any idea on how a fix for this or is it possible?
Thanks in advance
To produce an Objective-C structure equivalent to the above JSON you should do this:
NSArray* json = [NSArray arrayWithObject: [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: 1], #"uid",
[NSNumber numberWithInt: 1], #"version",
nil]];
Check my answer to the '' SBJsonWriter Nested NSDictionary '' question. it illustrates how to properly use SBJsonWriter.
It includes error check and some pieces of advise about SBJsonWriter behaviour with NSDate, float, etc..
Excerpt:
NSDictionary* aNestedObject = [NSDictionary dictionaryWithObjectsAndKeys:
#"nestedStringValue", #"aStringInNestedObject",
[NSNumber numberWithInt:1], #"aNumberInNestedObject",
nil];
NSArray * aJSonArray = [[NSArray alloc] initWithObjects: #"arrayItem1", #"arrayItem2", #"arrayItem3", nil];
NSDictionary * jsonTestDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
#"stringValue", #"aString",
[NSNumber numberWithInt:1], #"aNumber",
[NSNumber numberWithFloat:1.2345f], #"aFloat",
[[NSDate date] description], #"aDate",
aNestedObject, #"nestedObject",
aJSonArray, #"aJSonArray",
nil];
how to define 2x2 or 3X.. array in ios?
like this
[name=john , age=21 , num=1]
[name=max , age=25 , num=2]
[name=petter , age=22 , num=3]
with columns
in NSMutableArray you can only add rows with objects;
i want this array[][]
Looking at your example, I wouldn't do it with arrays, or not just arrays. I'd have an array of dictionaries or an array of custom objects with the properties name, age and num. With dictionaries:
NSArray* theArray = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
#"john", #"name",
[NSNumber numberWithInt: 21], #"age",
[NSNumber numberWithInt: 1], #"num",
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
#"max", #"name",
[NSNumber numberWithInt: 25], #"age",
[NSNumber numberWithInt: 2], #"num",
nil],
[NSDictionary dictionaryWithObjectsAndKeys:
#"petter", #"name",
[NSNumber numberWithInt: 22], #"age",
[NSNumber numberWithInt: 3], #"num",
nil],
nil];
How to declare a two dimensional array of string type in Objective-C? might give you an idea
So many ways ...
NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableDictionary *person = [[[NSMutableDictionary alloc] init] autorelease];
[person setObject:#"john" forKey:#"name"];
[person setObject:[NSNumber numberWithInt:21] forKey:#"age"];
...
[array addObject:person];
... or create your custom class, which does hold all person data, or struct, or ... Depends on your goal.
It looks like you should be create a proper data storage class to store this in, rather than a dictionary or something like that.
e.g.
#interface Person : NSObject {
}
#property (nonatomic, copy) NSString* Name;
#property int age;
#property int num;
#end
Then create your person instances and store them in an array. You may wich to create some connivence methods first. e.g.
[[NSArray arrayWithObjects:[Person personWithName:#"Bob",Age:1 Num:3],
[Person personWithName:#"Bob",Age:1 Num:3],
[Person personWithName:#"Bob",Age:1 Num:3],nil];
Its much clearer.