I’m beginning to explore programming in objective-c and I wrote a few lines of code for a command line tool. It works but now I’m trying to rewrite it in Cocoa application and I have some problems. This is the working code of the single line tool:
//Formattazione delle date preimpostate
NSDateFormatter *formatoData = [[NSDateFormatter alloc] init];
[formatoData setDateFormat:@"dd/MM/yyyy"];
//Definizione della data di inizio e della data di fine
NSDate * dataInizio = [formatoData dateFromString:datePicker];
//Risultato
NSString *risultato=[dataInizio descriptionWithCalendarFormat:@"%w" timeZone:nil locale:nil];
//Output
NSLog(@"Il giorno impostato è %@", risultato);
To create the Cocoa application, I have created an objective-C class I called VisualizzaClasse.
VisualizzaClasse.h:
#import <Foundation/Foundation.h>
@interface VisualizzaClasse : NSObject
{
IBOutlet NSDatePicker *datePicker;
IBOutlet NSTextView *textView;}
- (IBAction)mostraRisultato:(id)sender;
@end
VisualizzaClasse.m:
#import "VisualizzaClasse.h"
@implementation VisualizzaClasse
- (IBAction)mostraRisultato:(id)sender;
{
//Formattazione delle date preimpostate
NSDateFormatter *formatoData = [[NSDateFormatter alloc] init];
[formatoData setDateFormat:@"dd/MM/yyyy"];
//Definizione della data di inizio e della data di fine
NSDate * dataInizio = [formatoData dateFromString:datePicker];
//Risultato
NSString *risultato=[dataInizio descriptionWithCalendarFormat:@"%w" timeZone:nil locale:nil];
[textView insertText:[NSString stringWithFormat:@"%@ \n", risultato]];
}
@end
The interface is composed of a DatePicker, TextView and by a button.I can not make the DatePicker replace dateFromString.The error is: incompatible pointer type sending ‘NSDatePicker _strong *’ to parameter of type ‘NSString *’, at the line:
NSDate * dataInizio = [formatoData dateFromString:datePicker];
Can anyone help me?
I’m assuming you have the NSDatePicker correctly hooked up in InterfaceBuilder so it is connected to DatePicker.
If so, you should be able to assign as follows:
And then do your formatting as before, using
unformattedDateas the input to your formatter.