TUTORIAL : NSDate to NSTextField - Uses simple formatter function

This is a very brief tutorial on how to easily convert NSDate objects so they can be used with NSTextField(s).

Includes a formatter function, that allows easy splitting of date and time into separate fields.

This does not require the use of Bindings.

There are only 10 screen captures, take a look at it and see if it is useful.

Anyway this works very well for me, so I thought I’d share it.

http://www.journey-of-flight.com/

Xcode (item 4)

Let me know if there is a better way, I’m sure there is…

Bill Hernandez
Plano, Texas

Hi Bill,

Nice tutorial.

A couple of notes:

You do not need to use @property and @synthesize for the text fields or window.
The instance variables are all you need.

When checking for a BOOL value use YES and NO instead of true or false.
Also, I don’t understand what you are checking for in the if statements inside the
awakeFromNib method when you are checking for true. What are you wanting to
accomplish there? What is supposed to be true?

Cheers,

Craig

Often when I am debugging and trying more than one thing, instead of commenting several lines, and uncommenting others, it is easier to use this technique. There’s no need to define any variables, it’s just for testing, although you can leave it in there,

if(true) // if I change the true to false, then the “second set of instructions” will get executed
{
do this // first set of instructions
and also this
}
else
{
do the other // second set of instructions
and this other
and some more
}

for example :

// ±--------±--------±--------±--------±--------±--------±--------±--------+
// If I wanted to test the first set of instructions, I could comment the third line :
// ±--------±--------±--------±--------±--------±--------±--------±--------+

NSString *timeStr = [self bhFormatDate:today withFormat:@"hh:mm a z"];
[timeField2 setStringValue:timeStr];

// [timeField2 setStringValue:[self bhFormatDate:today withFormat:@"hh:mm:ss a z"]];

// ±--------±--------±--------±--------±--------±--------±--------±--------+
// If I wanted to test the second set of instructions, I could comment the first two lines :
// ±--------±--------±--------±--------±--------±--------±--------±--------+

// NSString *timeStr = [self bhFormatDate:today withFormat:@"hh:mm a z"];
// [timeField2 setStringValue:timeStr];

[timeField2 setStringValue:[self bhFormatDate:today withFormat:@"hh:mm:ss a z"]];

But that is all a pain, especially if each set of instructions has multiple lines, INSTEAD I can just do :

if(true)
{ // putting in true will run this set of instructions
NSString *timeStr = [self bhFormatDate:today withFormat:@“hh:mm a z”];
[timeField2 setStringValue:timeStr];
}
else
{ // putting in false will run this set of instructions
[timeField2 setStringValue:[self bhFormatDate:today withFormat:@“hh:mm:ss a z”]];
}

I’ve used this little technique in many languages for over twenty years, it’s just for debugging, and I don’t need to comment out either set of instructions.

Whatever you test for within if ( … ) has to evaluate to either true or false anyway.
.

Gotcha!

I am glad you asked, I’ve been doing this for so long that I didn’t realize, I should have added a comment.

Anyway I hope you are doing well…