Quick question about Date from string

Joined
Feb 14, 2013
Messages
4
Reaction score
0
Points
1
In my learning book, objective-c for absolute beginners, is written the following example

Code:
    NSDate *today = [NSDate date];
    NSDate *saleDate = [NSDate dateWithString:@"2011-12-04 04:00:00 -700"];

The second line is giving me problems in that dateWithString doesn't seem to be a method of the NSDate class. Using google I learned that that is only available on the OS X version of the class.

So I resolved it as follows
Code:
    NSDate *saleDate = [[[NSDateFormatter alloc] init] dateFromString:@"2011-12-04 04:00:00 -700"];

Is this the best and fastest way to do so ?
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,734
Reaction score
2,059
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
That is indeed the right class to be using on iOS. You will find as you are learning Obj-C that the Cocoa (OS X) and Cocoa Touch (iOS) frameworks are similar but have subtle differences..
 
OP
S
Joined
Feb 14, 2013
Messages
4
Reaction score
0
Points
1
That is indeed the right class to be using on iOS. You will find as you are learning Obj-C that the Cocoa (OS X) and Cocoa Touch (iOS) frameworks are similar but have subtle differences..

Ok, thank you!

Seems a bit circuitous to me to have alloc and init a class to be able to do that. But it might be my little .net knowledge that's throwing me off...
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,734
Reaction score
2,059
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
The "[[object alloc] init]" is a common syntax in Objective-C. You first allocated an instance of the object and then send it the "init" message that is going to initialize all the internal variables.

All of the NSObjects define the init and dealloc methods that you would send to allocate/initialize the object and deallocate itself..
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top