Deprecated method dateWithNaturalLanguageString

Ch. 20, the dateWithNaturalLanguageString: method has been deprecated in X.10. Xcode recommends NSDateFormatter instead. Which class method is appropriate to get the same result?



Found this fix:

[code]NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@“M, d, y ‘at’ hh:mm”];
NSDate *result = [formatter dateFromString:@“July, 18, 2014 at 08:00”];

    jonathan.heightInMeters = 1.8;
    jonathan.weightInKilos = 82;
    jonathan.employeeID = 12;
    jonathan.hireDate = result;
    float height = jonathan.heightInMeters;
    int weight = jonathan.weightInKilos;
    NSLog(@"Jonathan is %.2f meters tall and weighs %d kilos.", height, weight);
    NSLog(@"Employee %d was hired on %@", jonathan.employeeID, jonathan.hireDate);[/code]

It’s a bit more code, but I suppose offers more granular control of the date formatting.

It works for me thanks.
I was quite annoyed with these changes at first. But now we can learn better by discovering ways around it using documentation and internet resources.

Many of the methods have been deprecated. Another example is the following: NSGregorianCalendar and NSCalendarIdentifierGregorian. The question is why? In some cases it is not so obvious. … deprecated

Yes, the changes are frustrating, and I can’t figure out how to implement the documentation half the time. Luckily this time it was a bit easier. I was having some trouble with ‘setDateFormat’, but I did find ‘setDateStyle’ and ‘setTimeStyle’ that seemed to work pretty well.

[code]#import <Foundation/Foundation.h>
#import “BNREmployee.h”

int main(int argc, const char * argv[]) {
@autoreleasepool {

    //Create an instance of BNREmployee
    BNREmployee *mikey = [[BNREmployee alloc]init];
    //Give the instance vairable interesting values using setters
    mikey.weightInKilos = 96;
    mikey.heightInMeters = 1.8;
    mikey.employeeID = 12;
    //NSDate dateWithNaturalString was depreciated in OS X 10.10.  I used the NSDateFormatter object.
    //There may be an easier way to do this, but I'm still trying to figure out how to read Documentation
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    mikey.hireDate = [dateFormatter dateFromString:@"Aug 02, 2010"];
    //Log in the instance variables using the getters
    float height = mikey.heightInMeters;
    int weight = mikey.weightInKilos;
    NSLog(@"mikey is %.2f meters tall and weighs %d kilograms", height, weight);
    NSLog(@"Employee %u hired on %@", mikey.employeeID, mikey.hireDate);
    //Log some values using custom methods
    float bmi = [mikey bodyMassIndex];
    double years = [mikey yearsOfEmployment];
    //I thought this sentence was non-sensical and made some changes.
    NSLog(@"Employee %u has a BMI of %.2f, has worked with us for %.2f years", mikey.employeeID ,bmi, years);
return 0;


Thanks for posting the code above. I could not figure anything out. Xcode did not give me any kind of suggestion, so I did not know anything about dateFormatter.

I noticed that if I change the setTimeStyle to anything besides NSDateFormatterNoStyle, such as NSDateFormatterShortStyle, then mikey.hireDate prints out as (null). If I just comment out that line, mikey.hireDate is not (null).

Why is this happening? How would you ever use setTimeStyle besides for NSDateFormatterNoStyle?

This seems like a lot of work to be able to enter a “natural date”. What are the advantages of this change from what the book used (the deprecated dateWithNaturalLanguageString)? Or is there an easier way that I am missing?

It’s a shame that stringWithNaturalLanguageString has been deprecated; it was a convenient exercise for this topic.

When you set the .dateStyle and .timeStyle (or just the .dateFormat), you are telling the NSDateFormatter “This is how I have formatted the information in my date string.” If you set the .timeStyle to something other than NoStyle, then the NSDateFormatter will look in your date string for a time in that style. Since it can’t find one, it fails to build a new NSDate and returns nil. The reason that the NSDateFormatter still works with NoStyle is that the date information in the string is still correctly formatted for the NSDateFormatterShortStyle. If you use a different date format in the string, say, @“02 August 2010”, then even this will fail.

Also, the API is confusing, because there are three properties you can use here to teach the NSDateFormatter how to understand your string:

  • dateStyle - a constant that specifies which of several formats are used for the date-only information in the string
  • timeStyle - a constant that specifies which of several formats are used for the time-only information in the string
  • dateFormat - a string literal that specifies the exact representation of the date and time information in the string

While the dateStyle and timeStyle are usually used together, they can only represent a handful of date/time formats. For this reason I prefer to use the dateFormat (which can be used alone), but it takes a little bit to understand. The dateFormat should be a string literal of the format specified in Unicode Technical Standard #35 (see … t_Patterns).

That is, if my source string will have content that looks like @“1982-12-26 04:26”,
then I should use a dateFormat of @“yyyy-MM-dd HH:mm”.

Or, if my source string looks like @“Dec 26, 1982”,
then my dateFormat would be @“MMM dd, yyyy”.

I hope that helps.

That does help. Thanks for all of that information. I feel better just knowing that “it is a shame”.

One more question about Xcode. NSStudent mentioned that Xcode suggested to use NSDateFormatter. I have had suggestions by Xcode for various things, but I did not get a suggestion in this case. Is the set of things Xcode makes suggestions for change from version to version. For example, maybe that change is older now and so Xcode no longer makes this suggestion anymore. Is that what probably happened?

It’s pretty likely. Each point-release of iOS and Xcode each bring changes to the availability of classes and methods, as well as Xcode’s suggestions for dealing with different types of issues. In this case, stringWithNaturalLanguageString wasn’t deprecated until OS X 10.10 (Yosemite). Even then, it’s possible that there could be differences in the suggestions from Xcode 6.2 to Xcode 6.3.

The latest stable Xcode release is 6.3.2. I generally recommend always having the latest Xcode release installed.