Converting NSDate to NSString causes unrecognized selector exception - cocoa

I am storing an NSDate in a plist as a string, and at launch I am trying to convert the string from the plist back to an NSDate to compare it later.
This is how I am storing the value in my plist:
[InfoDic setValue:[NSDate date] forKey:#"LastDate"];
In the log (When I convert the [NSDate date] to a proper string) it says this:
2013-04-13 22:47:57 +0000
This is how I am trying to convert the plist value back to an NSDate:
NSString *Checkdate= [InfoDic objectForKey:#"LastDate"];
NSDateFormatter *DateFormat=[[NSDateFormatter alloc]init];
[DateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *theday=[DateFormat dateFromString:Checkdate];
This is the error log on my iPhone 4S:
<Error>: -[__NSDate length]: unrecognized selector sent to instance 0x1d8715e0
<Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSDate length]: unrecognized selector sent to instance 0x1d8715e0'
*** First throw call stack:
(0x31e5c2a3 0x39b7997f 0x31e5fe07 0x31e5e531 0x31db5f68 0x327c213d 0x327c208d 0x327c237b 0x23fe1 0x33c83595 0x33cc3d79 0x33cbfaed 0x33d011e9 0x2385d 0x33cc4ad9 0x33cc4663 0x33cbc84b 0x33c64c39 0x33c646cd 0x33c6411b 0x3597a5a3 0x3597a1d3 0x31e31173 0x31e31117 0x31e2ff99
0x31da2ebd 0x31da2d49 0x33cbb485 0x33cb8301 0x235a1 0x23528)
Please note that when I install the app on my device it takes the first date and stores it in the plist. When I close the app and rerun it, it gives me the SIGABRT.
What should I do about this?

You are not storing a date as a string in the plist, you are storing it as a date.
The line:
[InfoDic setValue:[NSDate date] forKey:#"LastDate"];
stores the actual NSDate object.
All you need to get it back out is to call:
NSDate *theDay = InfoDic[#"LastDate"];
BTW - the line:
[InfoDic setValue:[NSDate date] forKey:#"LastDate"];
should be:
[InfoDic setObject:[NSDate date] forKey:#"LastDate"];
or just:
InfoDic[#"LastDate"] = [NSDate date];

You are overcomplicating things. What makes you think that storing a NSDate object you'll get back a NSString?
Just do
NSDate * checkDate = [InfoDic objectForKey:#"LastDate"];
Also, don't confuse KVC methods with NSDictionary methods.
You want to use setObject:forKey: instead of setValue:forKey if you don't want to face bad surprises.

Related

Error converting a NSString to a NSURL

The following snippet produces the debugger listing that follows it. Yet, if Line Two replaces Line One, there is no error. LineTwo was obtained by coping and pasting the debugger output from a previous run. Can you explain this to me?
//LINE ONE
NSString* aString = self.urlText;
NSLog(#"aString: %#",aString);
//LINE TWO
//NSString* aString = #"file:///Users/oldmancoyote1/Documents/A%20REFERENCE%20SYSTEM/C/Computers/iOS/C%20and%20Obj.%20C/C%20Language/1-4.6.html";
NSURL* testURL = [NSURL fileURLWithPath:aString];
2016-01-03 12:44:30.307 webScrol[1505:62452] aString: file:///Users/oldmancoyote1/Documents/A%20REFERENCE%20SYSTEM/C/Computers/iOS/C%20and%20Obj.%20C/C%20Language/1-4.6.html
2016-01-03 12:44:30.307 webScrol[1505:62452] -[NSURL length]: unrecognized selector sent to instance 0x6080000a4800
2016-01-03 12:44:30.307 webScrol[1505:62452] Failed to set (contentViewController) user defined inspected property on (NSWindow): -[NSURL length]: unrecognized selector sent to instance 0x6080000a4800
aString is actually an NSURL object despite being stored in an NSString variable. This must have happened in earlier code than what you have provided.
When it’s passed to [NSURL fileURLWithPath:], that function tries to treat it as an NSString by calling its length method and the program crashes.

How to get parse objects newer then a week

I need to get objects from parse that not older then a week.
i was trying it with
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE, MMMM d yyyy"];
NSDate *date = [object createdAt];
[dateFormatter stringFromDate:date]
Your code is a long way from working and shows some basic misunderstandings. I think you really need to go and learn about Objective-C and Cocoa/UIKit before you jump into Parse. However:
Parse has an automatic column called createdAt on all objects. You should use that.
I'm not sure about your definition of "a week ago" but to get a date exactly one week ago to the second use:
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:-60*60*24*7];
There are more complex ways of doing this if you mean "midnight one week ago" or something like that but I'll leave this as is for the sake of clarity.
Pass this date to a PFQuery using the built in createdAt column:
PFQuery* q = [PFQuery queryWithClassName:[MyClass parseClassName]];
[q whereKey:#"createdAt" greaterThan:date];
Then issue the find:
[q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// here objects will contain your PFObject subclasses from the server
}];

NSDatePicker Error

Ever time I try to make a string in xcode it won't work can someone point out what's wrong and help please
NSString *date = self.DatePicker.date;
This is the error "Incompatible pointer types initializing 'NSString * strong' with an expression of type 'NSDate *'"
self.DatePicker.date; returns date.
You are storing it in NSString.
Use:
NSDate *date = self.DatePicker.date;
Then if you want to store it in string then use:
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];

Date to String conversion (NSDateFormatter) doesn't work

I'm working on an iOS project, retrieving data from SQLite through JSON. The table has a datetime column called 'date'. When I retrieve the date in Xcode and printout the result, I see "2012-09-02T16:30:00Z". But when I try to convert it using NSDateFormatter, the result is null.
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:#"EdMMM" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *dateString = [dateFormatter stringFromDate:categoryAtIndex.date];
NSLog(#"%# => %#", categoryAtIndex.date, dateString);
The above outputs:
2012-09-02T16:30:00Z => (null)
in stead of a nicely formatted datestring.
Any thoughts?
Thanks to mask8 I found the issue was related to another part of my code. I found this post on stackoverflow to solve the way to change the format of the date that I retrieved through JSON. I also found another post on stackoverflow describing how to handle conversions from a string to NSDate and vice versa.

UIDatePicker Not storing time in core data

I am using a UIDatepicker with only "Time", I save the time in a NSDate object type, however when I try to store the Object in core data I get an error saying its not a NSDate type...
tt = [pickerTime date];
[myObject setValue:tt forKey:#"time"];
Thanks,
Hey, a see a mistake here:
tt = [pickerTime date];
tt's type is either an NSDate, or... You have to declare what tt is:
NSDate *tt = [pickerTime date];
Make sure that your using a date field in IB.

Resources