• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Setting an NSDate with natural language string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting an NSDate with natural language string


  • Subject: Re: Setting an NSDate with natural language string
  • From: Ondra Cada <email@hidden>
  • Date: Sat, 19 Oct 2002 15:22:52 +0200

On Saturday, October 19, 2002, at 02:08 , Rick Anderson wrote:

Okay, I'm not new to programming but I am new to cocoa and obj-c. What I'
ve written below as part of a project I'm working on to learn cocoa will compile but will bail out when running.

In the interface, I have two text fields (dateBox and timebox). This chunk of code is attempting to pull out the date and time in those fields and set an NSDate object based on those values.

First, that's a bad GUI design. The Cocoa way of doing this is to use *one*
textfield, with NSDateFormatter. Besides being right, it's also infinitely easier for you and more convenient for the user ;)

Nevertheless, let's see the code:

NSDate *theDate = [[NSDate alloc] dateWithNaturalLanguageString:theDateStr];

Well, it's a class message, and you are sending it to an instance (made by alloc). ObjC basics (/Developer/Documentation/Cocoa/ObjectiveC/ObjC.pdf). Also, the instance is not initialized, which might raise another havoc even if it had the method.

I'm stumped. Why would the code compile if NSDate doesn't recognize dateWithNaturalLanguageString:?

The code must compile, since you can send *any* message to *any* object. The compiler can't know whether you, for example, did not add a category with an appropriate instance method runtime. This is no static crap like C+
+!

Most time, the compiler does an educated guess based on your declarations and warns you in such cases that the code, albeit right one, looks suspicious; alas here it is not possible, for +alloc needs to return id to be quite polymorphic.

I know it's a class method (I understand the distinction between a class and an instance method), but is that not the context in which I'm using it here?

Which is exactly the reason it does not work. What you wanted to was (to use the formatter, but if not)

theDate=[NSDate dateWithNaturalLanguageString:theDateStr];
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Setting an NSDate with natural language string (From: Rick Anderson <email@hidden>)

  • Prev by Date: Re: About NSString - uppercaseString
  • Next by Date: keyDown events and first responder
  • Previous by thread: Setting an NSDate with natural language string
  • Next by thread: Re: Setting an NSDate with natural language string
  • Index(es):
    • Date
    • Thread