Why is Parse PFObject response borked? - ios

I am starting a new project on Parse.
I am trying to retrieve my own properties on a custom class PFObject being returned from a query. I'm using [object objectForKey:#"key"] and it isn't working. I logged the description of the object I was getting back from the query and it's format is:
<Horse: 0x7f93e9c7e240, objectId: gLqeyEfOr6, localId: (null)> {
hidden = 0;
visibleName = "Horsey";
}
Notice that "hidden" and "visibleName" have been encapsulated by { }'s . This seems to be my issue. Why is this format happening to my custom properties? How do I get the data for keys "hidden" and "visibleName" out of my PFObject?
Thanks

You can access a PFObject directly as a dictionary - the {} is just added by the description method (which is the method that gets invoked when you NSLog the object as a string) to indicate a dictionary. What you have should work. You can also use the more compact notation -
NSString *visibleName=myPFObject[#"visibleName"];
NSNumber *hidden=myPFObject[#"hidden"];
What results are you getting when you say "it doesn't work"?

Related

NSString format issue - {( String )}

I am fetching data from core data and trying to print the name of the object at valueForKey. This has never caused me any trouble however when getting a direct object but the object I am getting is from an NSSet relationship.
When I print out the value I want, it displays in this format.
{(
Bob Marley
)}
{(
Jack Daniels
)}
etc...
I have used NSString stringWithFormat, I have tried componentsSeperatedByString, however neither of these work. If another question has been asked, I cannot find it on here. The issue is identical in a UILabel as well. Makes no difference where I print it.
Hopefully it's a simple issue.
My code for getting the value is.
//Company * company
//Employee is related in a many-many relationship
NSString * name = (NSString *)[self.company.contractor valueForKey:#"contractorName"];
NSLog(#"Name: %#", name);
Thanks.
self.company.contractor is NSArray.
So you need to do
NSString * name = (NSString *)[[[self.company.contractor allObjects] firstObject] valueForKey:#"contractorName"];
Also refer to my previous answer

Parse PFUser CurrentUser Returning Null

I save a few objects in the _User class in parse, such as displayname, gender, and bio. A simple user profile. I'm having difficulty retrieving them. I tried with many types of queries with no luck. So I figured if you could get the username by doing this:
self.username.text = [NSString stringWithFormat:#"#""%#",[[PFUsercurrentUser]valueForKey:#"username"]];
You should be able to get other objects that are stored in _User right? Well I tryed this to get the display name:
self.name.text = [NSString stringWithFormat:#"%#",[[PFUser currentUser]valueForKey:#"name"]];
And I get (null) printed out on the label, except in the data browser there is a value there. I'm really in need of some assistance this has been a problem that's taking me a while to figure it out. Thanks!
You have mentioned two lines in your code:-
//Have modified this line
In this line valueForKey you are passing #"username"
self.username.text = [NSString stringWithFormat:#"%#",[[PFUser currentUser]valueForKey:#"username"]];
In this line valueForKey you are passing #"name"
self.name.text = [NSString stringWithFormat:#"%#",[[PFUser currentUser]valueForKey:#"name"]];
Now in above two line both are having two separate keys username and name. So just check the valid key like that below if you are not sure which is valid, if below any of line prints value then use that key for setting the value in your label:-
NSLog(#"%#",[[PFUser currentUser]valueForKey:#"username"]);
NSLog(#"%#",[[PFUser currentUser]valueForKey:#"name"]);

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..

How to check if NSDictionay key has a value efficiently when creating XML

I am creating some XML in objective C, I know how to do it however there is the possibility that there could be 800+ values I might be putting into XML, which I am getting from a NSArray of NSDictionaries.
So I was wondering if there is an efficient way of checking for nill or null in a keyvalue that's of type NSString.
Currently this is what my code looks like:
NSMutableArray *xmlItems = [coreDataController readInstallForXML:selectedInstallID];
for (int i = 0; i < [xmlItems count]; i++) {
NSDictionary *currentXMLItem = [xmlItems objectAtIndex:i];
[xmlWriter writeStartElement:#"Items"];
[xmlWriter writeAttribute:#"insID" value:[currentXMLItem valueForKey:#"insID"]];
// there are about another 20 attributes I have to add here.
}
// then write end elemtent etc.
In the code above I have no added any checking but I was hoping someone might have something better for me than adding a bunch of if statements for each attribute.
You can use [NSDictionary allKeysForObject:] to get all keys for the 'nil' values, so you have a list of keys to ignore.
Generating 800 items is not necessarily 'much' or 'slow'. You don't want to do that on the main thread anyway, so just make sure you perform it as a background operation.
use the allKeys method on the NSDictionary to return an NSArray of keys; then iterate through that array and for each key retrieve the value from the dictionary and use one if statement to check the string before writing out the xml element

How do I get properties out of NSDictionary?

I have a webservice that returns data to my client application in JSON. I am using TouchJson to then deserialize this data into a NSDictionary. Great. The dictionary contains a key, "results" and results contains an NSArray of objects.
These objects look like this when printed to log i.e
NSLog(#"Results Contents: %#",[resultsArray objectAtIndex:0 ]);
Outputs:
Results Contents: {
creationdate = "2011-06-29 22:03:24";
id = 1;
notes = "This is a test item";
title = "Test Item";
"users_id" = 1;
}
I can't determine what type this object is. How do I get the properties and values from this object?
Thanks!
To get the content of a NSDictionary you have to supply the key for the value that you want to retrieve. I.e:
NSString *notes = [[resultsArray objectAtIndex:0] valueForKey:#"notes"];
To test if object is an instance of class a, use one of these:
[yourObject isKindOfClass:[a class]]
[yourObject isMemberOfClass:[a class]]
To get object's class name you can use one of these:
const char* className = class_getName([yourObject class]);
NSString *className = NSStringFromClass([yourObject class]);
For an NSDictionary, you can use -allKeys to return an NSArray of dictionary keys. This will also let you know how many there are (by taking the count of the array). Once you know the type, you can call
[[resultsArray objectAtIndex:0] objectForKey:keyString];
where keyString is one of #"creationdate", #"notes", etc. However, if the class is not a subclass of NSObject, then instead use:
[[resultsArray objectAtIndex:0] valueForKey:keyString];
for example, you probably need to do this for keystring equal to #"id".

Resources