Parse PFUser CurrentUser Returning Null - ios

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"]);

Related

Why is Parse PFObject response borked?

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"?

Retrieve string from Parse.com

I want to retrieve the current users string value on a specific column from the user class in Parse.com
I have tried this code:
NSString *columnString = [[[PFUser currentUser] objectForKey:#"columnKey"] stringValue];
This code logged an error saying "unrecognized selector sent to instance.."
What is the correct way to do this?
If your column datatype is String then try below snippet.
NSString *str_ColumnString = [NSString stringWithFormat:#"%#",[[PFUser currentUser]valueForKey:#"columnKey"]];

Insert NSMutableDictionary into 1 cell of SQLite database

I'm hoping someone here might be able to help me out.
I need to insert a NSMutableDictionary into one cell of a SQLite database. I am able to insert strings etc into the database, but when I try and insert a Dictionary I get a syntax error:
**Can't run query 'BEGIN TRANSACTION; UPDATE Database SET Column1 = {
Bad = "";
"End_Time" = 4;
Good = "";
Moderate = "";
Note = "";
} WHERE Title = Name; COMMIT TRANSACTION;' error message: unrecognized token: "{"**
To do this I am using the following code:
NSString *sql2 = [NSString stringWithFormat:#"UPDATE Database SET Column%# = %# WHERE Title = %#",previousQuestion,adictionary,Name];
Can anyone help or suggest a different approach,? I need to be able to store the key/values in 1 cell, as there will be 79 more cells with similar data and I need to reference each specific key from a specific column of the database.
I've tried turning the Dictionary into a string (and then I'd turn the string back to a dictionary on retrieval) But this causes the same issue.
Any suggestions?
Many thanks,
Andrew
Their is two way you can do this.
1st way :-
Archive your NSMutableDictionary convert it into NSData and store it in your Sqlite column which datatype should have be blob type. Archiving something like this,
NSData *theDictionaryData = [NSKeyedArchiver archivedDataWithRootObject:yourDictionary];
Bind this data in sqlite,
sqlite3_bind_blob(addStmt, 5, theDictionaryData, -1, SQLITE_TRANSIENT);
Now retrieving time from sqlite,
NSData *retrieveData = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstmt, 4) length:sqlite3_column_bytes(selectstmt, 4)];
and finally convert it into NSDictionary,
NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:retrieveData];
2nd way :-
Create numbers of column those are equal to numbers of keys of NSDictionary. Bring data from every key and save it respective table column.
You can't just insert an Objective-C object into the database. It must be a string, binary data, or a number. In converting it to a string, you also need to sanitize the input (so that it doesn't contain any invalid characters that SQL will interpret in another way). For example, if you tried to insert a string that was some SQL code, you wouldn't want it to try to execute that code. It looks like here it's getting hung up on a { in the string. You could instead convert the dictionary or the string to NSData and insert it into the database as binary data.

iOS/Parse query not finding string match in array

I am trying to query a PFObject that contains an array. I am looking to match a given NSString against any object within the PFObject's array of strings. According to http://blog.parse.com/2012/03/28/parse-supports-many-to-many-relations/, I can use whereKey:equalTo to look for matches within an array.
I have replaced the variable I would usually use with the string that I know to be in the array whose object I am querying. It is a character-for-character match. Yet the query returns no matches.
My code:
PFQuery *convosQuery = [PFQuery queryWithClassName:#"convo"];
PFObject *currentUserFacebookID = [NSString stringWithFormat:#"11808098"];
[convosQuery whereKey:#"nonUserFacebookIDs" equalTo:currentUserFacebookID];
[convosQuery findObjectsInBackgroundWithBlock:^(NSArray *convos, NSError *error) {
for (PFObject *convo in convos) {
NSLog(#"user/convo match found");
As I said, this returns no matches even though a PFObject of class "convo" contains for key "nonUserFacebookIDs" the value "[["11808098"]]".
What could be going on?
Does this object have any ACLs that might be restricting reads to a subset of users?
You mentioned "[["11808098"]]". Does this mean an array of array containing a string? If so, it would not match the string you specified.
And BTW, though not related to your question, why did use the below line? Why should you assign a string to a PFObject?
PFObject *currentUserFacebookID = [NSString stringWithFormat:#"11808098"];
~ Sunil Phani Manne

Integer from .plist

I'm new to plists, and I really need to use one. What I have is a plist where different numbers are stored, under two dictionaries. I need to get that number which is stored, and make it an integer. This process will be run from a method called 'readPlist.
The plist is called 'properties.plist'. The first dictionary is called 'Enemies'. It contains various other dictionaries, which will have the name stored in the NSMutableString called 'SpriteType'. The name of the number will have the format 'L - %d', with the %d being an integer called 'LevelNumber'.
If possible, can someone give me the code on how to get that integer using the information, and the names of dictionaries above.
I have looked around at how to access plists, but the code that people have shown doesn't work.
Thanks in advance.
EDIT: Too make it more understandable, this is my plist. What i want in an integer, called 'SpriteNumber' to be equal to the value of 'L - %d'
If you read the contents of your plist into a dictionary (I won't tell you how to do it, but this is the tutorial I refer to often), then it's a matter of getting the string out of the key for the level with [[myDictionary objectForKey:#"key"]stringValue];. Then, using of NSString's extremely helpful -stringByReplacingOccurencesOfString:withString: to get rid of the "L -" part and only get a numerical value. Finally, get an integer from the string with [myString intValue].
well, the easiest way would be something like :
-(int) getMosquitoCountForLevel:(int) level {
int mosquitoCount=0;
NSString *gsFile = #"whateverFullyQualifiedFileNameYourPlistIs";
NSDictionary* definitions = [NSDictionary dictionaryWithContentsOfFile:gsFile];
NSDictionary* mosquitos = [definitions objectForKey:#"Mosquito"];
if(mosquitos) {
NSString *levelKey = [NSString stringWithFormat:#"L - %d",level];
NSNumber *mosquitoCountAsNumber = [mosquitos objectForKey:levelKey];
if(mosquitoCountAsNumber) {
mosquitoCount=[mosquitoCountAsNumber intValue];
} else {
CCLOGERROR(#"%# - Mosquito definitions in %# does not contain a en entry for level %#.",self.class,gsFile,levelKey);
}
} else {
CCLOGERROR(#"%# - file %# does not contain a Mosquito dictionary.",self.class,gsFile);
}
return mosquitoCount;
}
this compiles but not tested with actual data.

Resources