how can I read only one value of my dictionary? - ios

I created my own dictionary by taking the values ​​from a json file in the dictionary I have a set of values​​, the other can take them and use them, instead of these start with a brace can not seem to get them:
weather = (
{
description = "broken clouds";
icon = 04d;
id = 803;
main = Clouds;
}
);
Use this command to take the values ​​in the Dictionary:
NSString *currweather = myDict[#"weather"][#"main"];
The application quits when the launch. How can I fix?

NSString *currweather = myDict[#"weather"][0][#"main"];
The weather key is referencing an array of dictionary.
You should have been given a clue from the error message about an unrecognized selector (objectForKey:) being called on an array class.

Related

Error while parsing NSMutablearray values

I have a NSMuttableArraywhich contains the structure like below,
28 = (
twenty
);
30 = (
thirty
);
32 = (
thirty
);
I am trying to store the values like 28,30,32 into a separate array. I tried loop as,
NSMutableArray *getvalueshere;
for(int i=0;i<getvalueshere.count;i++)
NSArray *getval = [getvalueshere objectAtindex:i];
}
On trying this, my app gets crash saying __NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance.
Your object is a NSDictionary as the error message reveals.
You can get the numbers – which are actually the keys of the dictionary – with
NSArray *getval = [getvalueshere allKeys];
It's saying getvalueshere is an NSDictionary which doesn't recognize objectAtIndex. When you declared getvalueshere, you gave it a NSDictionary like structure. It is considered an NSDictionary even though you explicitly stated it should be an NSMutableArray.

Get whole object from NSMutableDictionary for current cellForRowAtIndexPath and assign it to a CustomCell

I have the following type of a NSMutableDictionary
(
{
id = 1;
key = value;
key = value;
},
{
id = 2;
key = value;
key = value;
}
)
It contains multiple data of an own Object. Now, in cellForRowAtIndexPath. I created a CustomCell that has a field CustomCell.customObject that should get this object. What I'm trying to do is the following. I want to assign the current entry of the NSMutableDictionary to this field.
Alternatively I could do this (and am doing it right now)
I'm getting the ID like this
NSString *objectId = [[dict valueForKey:#"id"] objectAtIndex:indexPath.row];
And then I'm loading the object from the database. The problem I'm seeing in this, is the doubled request. I mean, I already have the data in my NSMutableDictionary, so why should I request it again?
I don't want to just assign a certain key-value pair, I want to assign the whole current object entry of the NSMutableDictionary. How would I do this?
If the above code (dictionary) is used to show information on tableview there is a mistake in it dictionary should always starts with "Key" not with index. So better make the dictionary to array and then you will have index to get complete information in indexes write this code
NSLog(#"%#",[[newArray objectAtIndex:0]objectForKey:#"id"]);
Hope this will help..

RestKit post with NSArray of NSDictionary numbering

I am using RestKit to Post an object to the server.
The object contains an NSArray, which I convert to NSDictionaries before adding it to the parameters list (an NSDictionary)
the NSDictionary queryParams looks like this (printed out using NSLog, and removing some fields for clarity)
requestParams = {
"app_version" = "v1.0(1.006)";
"extended_information" = {
"travel_locations" = (
{
"Aircard_or_Tablet" = 0;
Country = "United States";
"End_Date" = "04/12/2013";
"Start_Date" = "03/12/2013";
}
);
"using_voice" = 0;
};
}
I then send it:
[objectManager postObject:nil path:server_path parameters:requestParams success:… failure:…]
The problem is that when RestKit creates the query string, it seems to increment the counter for each key in the dictionary, rather than per item in the array. The following is what is received by the server. I added a line break after each parameter for clarity:
app_version=v1.0(1.006)
&extended_information[travel_locations][0][Aircard_or_Tablet]=0
&extended_information[travel_locations][1][Country]=United States
&extended_information[travel_locations][2][End_Date]=04/12/2013
&extended_information[travel_locations][3][Start_Date]=03/12/2013
&extended_information[using_data]=0
The server is expecting (and I would expect) travel_locations to remain at [0] for all the keys in that one object, like this:
app_version=v1.0(1.006)
&extended_information[travel_locations][0][Aircard_or_Tablet]=0
&extended_information[travel_locations][0][Country]=United States
&extended_information[travel_locations][0][End_Date]=04/12/2013
&extended_information[travel_locations][0][Start_Date]=03/12/2013
&extended_information[using_data]=0
And, of course, if a second travel_location were there it would have [1], the third would have [2], and so forth.
Is this a bug in RestKit? Is there a better way I can be sending this request so that it does work? I do not have the option of changing the server.
To question is:
How can I have RestKit convert the NSDictionary for parameters properly so that it does not increment the array index identifier for each key in the array object?
Thanks,
-Nico

Accessing NSDictionary retrieves an object instead of NSString

I have an NSDictionary that I fill with JSON. It all looks great, and it is partially working for me. I am setting a property of my class that is of type NSString equal to a value for a certain key, which oddly enough sets my property equal to an object with the object being the string value I need.
The dictionary (printed description):
miscInfo:
<__NSArrayI 0x7494ab0>(
{
Abbreviation = DR;
DateInactive = "";
Description = Drowsiness;
ForQueue = 2430;
IsAdditive = 0;
IsReserved = 1;
MiscCodeTypeStr = AVR;
PluralDescription = "";
ReservedDescription = Drowsiness;
}
I am setting it on my property like so:
self.ReactionName = [miscInfo valueForKeyPath:#"Description"];
Which produces:
I've tried casting it as NSString such as:
self.ReactionName = (NSString*)[allergenAdverseReaction valueForKeyPath:#"Description"];
but nothing gives. What am I doing wrong?
miscInfo is an array containing a dictionary, so
self.ReactionName = [[miscInfo objectAtIndex:0] objectForKey:#"Description"];
would give the result that you expect. Or, using the modern array and dictionary
subscripting syntax:
self.ReactionName = miscInfo[0][#"Description"];
Remark: In your code
self.ReactionName = [miscInfo valueForKeyPath:#"Description"];
valueForKeyPath is applied to each element of the array, and an array with all
the values is returned. That is what you see in the Xcode debugger window.
This "feature" can be very useful, but in general (as Hot Licks commented above)
objectForKey is the right method to get a value from a dictionary. And
self.ReactionName = [miscInfo objectForKey:#"Description"];
throws a descriptive runtime exception if miscInfo is not a dictionary as expected.
It looks to me like miscInfo is an array of dictionaries. In that case valueForKey will return an array. See if the code below works. If it does, then it is an array of dictionaries.
self.ReactionName = [[allergenAdverseReaction valueForKeyPath:#"Description"]lastObject];

Archiving NSArray that contains dictionaries

I am trying to save an NSMutableArray in CoreData. The Array contains objects NSDictionary
NSDictionary has following Structure
valueDict =
{
FloorId = F0001;
endCoordinates = "NSPoint: {541, 413}";
linePath = "<UIBezierPath: 0x1d0903c0>";
pointsOnLine = (
);
startCoordinates = "NSPoint: {418, 504}";
},
To write to the Core Data I use following code: parray is type BinaryData
points.parray = [NSKeyedArchiver archivedDataWithRootObject:self.locationsArray];
and to retrieve value I use
locationsArray = [NSKeyedUnarchiver unarchiveObjectWithData:points.parray];
When I try to retrieve it i get following error :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSDictionary initWithObjects:forKeys:]: count of objects (0) differs from count of keys (5)'
I have checked that NSArray and NSDictionary adopts NSCoding protocol. What am I doing wrong here ?
The best way to solve this is to create either a small test app, or a test method called when your app launches. Create a typical dictionary using the same keys and types, try to archive it, log the data size, then immediately try to unarchive the data. Assuming it fails comment out one or more keys til what's left works. Now you know the problem key and can better determine what is wrong.

Resources