Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 19, 2011, 06:20 AM   #1
thedollarhunter
macrumors member
 
Join Date: May 2011
Location: UK
NSDate may not respond to +dateWithString

Having just discovered NSDate I am trying to return a time interval (a typedef of long as a positive or negative number of seconds if I understand it correctly)
using the follow line of code

Code:
NSTimeInterval dateDiff = [[NSDate dateWithString:@"2011-11-17 00:00:00 +0000"] timeIntervalSinceNow];
It seems to work but when I build I see a Warning message that says:
NSDate may not respond to +dateWithString

What am I doing wrong?

Basically I want a check a given date to see if it is in the past or future.
__________________
Energy Storm preview on YouTube

Last edited by thedollarhunter; Nov 19, 2011 at 06:40 PM.
thedollarhunter is offline   0 Reply With Quote
Old Nov 19, 2011, 06:55 AM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
There is no mention of dateWithString: in the iOS NSDate Class Reference. It is is the the Mac OS X NSDate Class Reference, but it's use is discouraged. If it does in fact work in iOS, it's private API, and should be avoided.

You should send dateFromString: to an NSDateFormatter object instead, if your dates are in fact in a string.

If you dates aren't in a string but instead you have them as separate components, check out the NSDateComponents class.

Last edited by jiminaus; Nov 19, 2011 at 07:03 AM.
jiminaus is offline   0 Reply With Quote
Old Nov 19, 2011, 10:41 AM   #3
thedollarhunter
Thread Starter
macrumors member
 
Join Date: May 2011
Location: UK
Thanks for pointing that out, I will use dateFromString instead.

Another rookie question though, if I use NSDateFormatter to parse my string will it still work on devices with different regional settings?

Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm:ss ZZ"];

NSDate *dateToCheck = [dateFormatter dateFromString:@"24 Dec 2011 00:00:00 -0000"];

NSTimeInterval dateDiff = [dateToCheck timeIntervalSinceNow];
__________________
Energy Storm preview on YouTube
thedollarhunter is offline   0 Reply With Quote
Old Nov 19, 2011, 03:05 PM   #4
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by thedollarhunter View Post
Thanks for pointing that out, I will use dateFromString instead.

Another rookie question though, if I use NSDateFormatter to parse my string will it still work on devices with different regional settings?

Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm:ss ZZ"];

NSDate *dateToCheck = [dateFormatter dateFromString:@"24 Dec 2011 00:00:00 -0000"];

NSTimeInterval dateDiff = [dateToCheck timeIntervalSinceNow];
The user's locale and calendar are taken into consideration, so you want to set the locale before you set the format.

See Parsing Date String. In particular take heed of the advice to use the en_US_POSIX locale, not the en_US locale.
jiminaus is offline   0 Reply With Quote
Old Nov 19, 2011, 06:28 PM   #5
thedollarhunter
Thread Starter
macrumors member
 
Join Date: May 2011
Location: UK
Thanks for the pointer, that link is just what I was looking for.

I've added the en_US_POSIX locale and tested it out by picking random International settings on the device (never knew iOS offered a Buddhist calendar) and it seems to work just fine.

Again, thank you for taking the time to help others, it is really appreciated.
__________________
Energy Storm preview on YouTube
thedollarhunter is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
NSArray may not respond to '-length' Macman1993 iPhone/iPad Programming 2 Feb 23, 2011 09:48 PM
warning: 'NSOpenGLView' may not respond to dbrayford Mac Programming 1 Jan 28, 2011 03:11 PM
Warning: UIView may not respond to '-view' AbhishekApple iPhone/iPad Programming 1 Dec 8, 2010 12:57 PM
`NSString may not respond to '-getItem'` -- How can I avoid this warning? (appscript) Warhaven Mac Programming 0 Mar 5, 2010 01:14 PM
warning balloons- warning: 'XXXViewController' may not respond to XXX mlecho iPhone/iPad Programming 8 Dec 24, 2009 05:32 PM


All times are GMT -5. The time now is 06:12 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC