Why is this NSString null in a FireBase query block? - ios

I can't figure out why this string is null inside the FQuery block. My app keeps crashing when I build the dailyLog MutableDictionary at the user key;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM dd, YYYY"];
NSString *userID = [[self.userProfile objectForKey:#"userID"] copy];
self.logFirebase = [[Firebase alloc] initWithUrl:#"https://urlName.firebaseio.com/DailyLog"];
[[[self.logFirebase queryOrderedByPriority] queryEqualToValue:userID childKey:#"userID"] observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
if (![snapshot.value isEqual:[NSNull null]]) {
NSMutableArray *data = [CGCFirebaseDataExtractor extractKeysAndObjects:snapshot.value];
self.dailyLog = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"Name CONTAINS[cd] %#",[df stringFromDate:[NSDate date]]]].lastObject;
}else{
self.dailyLog = [[NSMutableDictionary alloc] init];
NSLog(#"users Profile:%#",self.userProfile);
NSLog(#"users ID :%#",[self.userProfile objectForKey:#"userID"]);
[self.dailyLog setObject:[df stringFromDate:[NSDate date]] forKey:#"date"];
[self.dailyLog setObject:[self.userProfile objectForKey:#"userID"] forKey:#"user"];
[self.dailyLog setObject:[NSNumber numberWithInt:450] forKey:#"calories"];
[[self.logFirebase childByAutoId] setValue:self.userProfile];
}
}];
EDIT:
When I log self.userProfile, I get the proper info I want. But when I log the userID itself from within the FQuery block, it's null
Here is my updated crash data:
2015-05-08 13:21:22.709 <appname>[4160:1321097] users Profile:{
"-JoirTqCXFFDY1psLTNn" = {
dateRegistered = "1431010405.81792";
userID = "304D92EF-CE77-4A10-A55F-9847153699F7";
userName = "User's Name";
};
}
2015-05-08 13:21:22.709 <appname>[4160:1321097] users ID :(null)
2015-05-08 13:21:22.714 <appname>[4160:1321097] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: user)'
*** First throw call stack:
(0x1852082d8 0x196a340e4 0x1850f1428 0x10007de00 0x1001454d0 0x100390fd4 0x100390f94 0x100395c28 0x1851bf7f8 0x1851bd8a0 0x1850e92d4 0x18e8ff6fc 0x189caefac 0x10007f14c 0x1970b2a08)
libc++abi.dylib: terminating with uncaught exception of type NSException

Sorry, I'm an idiot and didn't look closely at your log.
2015-05-08 13:21:22.709 <appname>[4160:1321097] users Profile:{
"-JoirTqCXFFDY1psLTNn" = {
dateRegistered = "1431010405.81792";
userID = "304D92EF-CE77-4A10-A55F-9847153699F7";
userName = "User's Name";
};
This object is evidently a dictionary in a dictionary. The first (outer) dictionary has key "-JoirTqCXFFDY1psLTNn" and that is its only key. That is why trying to get the key userID fails; that key is part of the second (inner) dictionary.

Related

NSPRedicate valueForUndefinedKey

I have a array of ProductData as follows, and I want to filter them based on their category and I applied the following Predicate, but it returns me error.
pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"category = %#", #"4"]]];
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[ valueForUndefinedKey:]: this class
is not key value coding-compliant for the key category.'
Here is overlook of the pElements
po self.pElements
<__NSArrayM 0x174241e30>(
<ProductData: 0x17427ce00>,
<ProductData: 0x17427cd80>,
<ProductData: 0x17427ce40>,
<ProductData: 0x17427ce80>,
)
ProductData.m
#import "ProductData.h"
#implementation ProductData
#synthesize pId, pImage, pPrice, pName, pCategory
-(id)initWithDictionary:(NSDictionary *)aDict{
self = [self init];
if (self){
self.pId = [aDict objectForKey:#"id"];
self.pPrice = [aDict objectForKey:#"price"];
self.pImage = [aDict objectForKey:#"imagePath"];
self.pCategory = [aDict objectForKey:#"category"];
self.pName = [aDict objectForKey:#"name"];
}
return self;
}
You should give the property name of the object by which you planning to filter the array. Here I guess it is pCategory. You should rewrite your code as below
pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"pCategory == %#", #"4"]]];

NSInvalidArgumentException, reason unrecognized selector sent to instance

When processing a json response I a get the below error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DADGList setDescription:]:
unrecognized selector sent to instance 0x7ae88880'
The class it crashed on:
#implementation DADGList
-(id)copy
{
DADGList *list = [[DADGList alloc] init];
list.identifier = self.identifier;
list.name = [self.name copy];
list.description = [self.description copy];
list.created = [self.created copy];
list.itemCount = self.itemCount;
list.shareLink = [self.shareLink copy];
return list;
}
+(DADGList *)listFromDictonary:(NSDictionary *)dictonary
{
DADGList *list = [[DADGList alloc] init];
NSLog( #"%#", dictonary );
list.identifier = [[dictonary objectForKey:#"list_id"] integerValue];
list.itemCount = [[dictonary objectForKey:#"list_items_count"] integerValue];
list.name = [NSString stringWithString:[dictonary objectForKey:#"list_name"]];
list.description = [NSString stringWithString:[dictonary objectForKey:#"list_description"]];
list.created = [[NSDate alloc] initWithTimeIntervalSince1970:[[dictonary objectForKey:#"list_created"] doubleValue]];
list.shareLink = [NSString stringWithString:[dictonary objectForKey:#"list_share_url"]];
return list;
}
and the dictonary that is past to listFromDictonary:
You should rename your description property for something else as this is a field already existing in the iOS echosystem (NSObject if I am not mistaken) and that creates all sort of weird crash like this.
If you are using core data maybe you need to implement the following instance:
Mall(entity: NSEntityDescription.entity(forEntityName: "Mall", in: context)!, insertInto: nil)
Where Mall is my entity and context is my view context.

Cant get the PFObject from this cloud code using iOS SDK

I have a cloud code on Parse.com searching a PFObject and returning it on the basis of date created the code is as follows
Parse.Cloud.define("getJournalEntry", function(request, response) {
var currDate = request.params.currDate;
var d1 = new Date(currDate + 1000 * 60 * 60 * 24 * 1); // gets 7 days ago
var query = new Parse.Query("JournalEntry");
query.greaterThan("createdAt",currDate);
query.lessThan("createdAt",d1);
query.find({
success: function(results) {
var entry = results[0];
response.success(entry);
},
error: function() {
response.error("no entry found");
}
});
});
I am trying to invoke this code from iOS app as follows
NSDate *dateOfMonth = .....;// some calculations
[PFCloud callFunctionInBackground:#"getJournalEntry"
withParameters:#{#"currDate": dateOfMonth}
block:^(PFObject *entry, NSError *error) {
if (!error) {
NSLog(entry[#"text"]);
}
else
{
NSLog(error.description);
}
}];
When checked this code from console if I supply wrong date it returns expected reply but in iOS it always crashes with following error
2014-07-16 18:18:44.343 Emojo[2845:60b] -[NSNull objectForKeyedSubscript:]: unrecognized
selector sent to instance 0x27f3068
2014-07-16 18:18:44.345 Emojo[2845:60b] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[NSNull objectForKeyedSubscript:]: unrecognized selector
sent to instance 0x27f3068'
What can be the issue ?
I this will happen even on sending correct date or wrong date.
Try this may be helpfull
Updated
NSDate *dateOfMonth = [NSDate date];
NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
[dateformat setDateFormat:#"dd-MM-YYYY"];
NSMutableDictionary *dateOfMonthDic = [[NSMutableDictionary alloc]init];
[dateOfMonthDic setValue: [dateformat stringFromDate:dateOfMonth ] forKey:#"currDate"];
[PFCloud callFunctionInBackground:#"getJournalEntry"
withParameters:dateOfMonthDic
block:^(PFObject *entry, NSError *error) {
if (!error) {
NSLog(entry[#"text"]);
}
else
{
NSLog(error.description);
}
}];
Thanks & Cheers ..

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber length]:

With these lines of code:
PayPalPayment *payment = [[PayPalPayment alloc] init];
NSLog(#"Price %#",[self.product objectForKey:#"Price"]);
NSString *string = [self.product objectForKey:#"Price"];
payment.amount = [[NSDecimalNumber alloc] initWithString:string];
my app is crashing with this log:
[NSDecimalNumber length]: unrecognized selector sent to instance
The NSLog is returning:
Price 96.67
Whay is it crashing???
I would guess that you have a type problem, most likely with the object at
[self.product objectForKey:#"Price"];
My guess is that this object is already an NSDecimalNumber, and when you treat it as a string and then call the initWithString method using it, bad things happen
put a breakpoint at:
payment.amount = [[NSDecimalNumber alloc] initWithString:string];
and then type po [string class] into the debugger.
This will show you the type of object stored at [self.product objectForKey:#"Price"];
It looks like your string object isn't an NSString. Looks like it's actually an NSDecimalNumber - set a breakpoint there and inspect the object. What kind of object is self.product?

adding Json to mutable array resolves in crash

Hello guys I am new to Xcode/iOS developing
I trying to add json data to the mutable array , and it results in app crash :(
so far here is my code:
if(! [defaults objectForKey:#"Person1"])
[defaults setObject:[PersonsFromSearch objectAtIndex:index] forKey:#"Person1"];
else
{
NSMutableArray *Array = [[NSMutableArray alloc]init];
id object = [defaults objectForKey:#"Person1"];
Array = [object isKindOfClass:[NSArray class]] ? object : #[object];
[Array addObject:[PersonsFromSearch objectAtIndex:index]];//crash here :((
[Array moveObjectFromIndex:[Array count] toIndex:0];
}
Crash Dump:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0xcb4d380'
* First throw call stack:
what is wrong here ? can you please help me to resolve this issue
Array contains this (Json?)
{
Address = "\U05d3\U05e8\U05da \U05d4\U05e9\U05dc\U05d5\U05dd 53";
CellPhone = "052-3275381";
EMail = "editor#pc.co.il";
EnglishPerson = "Yehuda Konfortes";
FaceBookLink = "";
Fax1 = "03-7330703";
Fax2 = "";
FileNAme = "100050.jpg";
HomeEMail = "";
HomeFax = "";
HomePhone1 = "";
HomePhone2 = "";
PersonID = 100050;
PersonName = "\U05d9\U05d4\U05d5\U05d3\U05d4 \U05e7\U05d5\U05e0\U05e4\U05d5\U05e8\U05d8\U05e1";
Phone1 = "03-7330733";
Phone2 = "";
ZipCode = "";
}
[defaults objectForKey:#"Person1"]; returns dictionary but not array.
So you can't use addObject method.
UPD
You may resolve this crash by creation array with a single object.
Here is updated code:
NSMutableArray *Array = [[NSMutableArray alloc]init];
id object = [defaults objectForKey:#"Person1"];
Array = [object isKindOfClass:[NSArray class]] ? [object mutableCopy] : [#[object] mutableCopy];
[Array addObject:[PersonsFromSearch objectAtIndex:index]];//crash here :((
[Array moveObjectFromIndex:[Array count] toIndex:0];
NSMutableArray *_arr = [[NSMutableArray alloc]init];
NSDictionary *results = PASS JSON Value;
[_arr add object:[results objectforkey:#“Address”]];
[_arr add object:[results objectforkey:#“CellPhone”]];

Resources