2012-04-09 31 views
1

我開始探索objective-c中的編程,並且我爲命令行工具編寫了幾行代碼。它的工作,但現在我試圖重寫它在可可應用程序,我有一些問題。這是單行工具的工作代碼:從命令行工具到可可應用程序

//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); 

要創建Cocoa應用程序,我已經創建了一個Objective-C類我叫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 

接口是由一個datepicker,TextView的,並通過一個button.I不能使的DatePicker替換dateFromString.The錯誤是:兼容的指針類型發送「NSDatePicker _strong *爲鍵入「的NSString *」的參數,在該行:

NSDate * dataInizio = [formatoData dateFromString:datePicker]; 

誰能幫助我?

回答

0

提到我假設你有NSDatePicker正確掛接在InterfaceBuilder中,因此連接到使用格式化日期選擇器。

如果是這樣,你應該能夠分配如下:

NSDate * unformattedDate = [ datePicker dateValue ]; 

然後做你的格式化和以前一樣,使用unformattedDate作爲輸入到你的格式。

+0

非常感謝! – Andrea 2012-04-09 17:08:12

0

dateFromString方法需要的NSString,你的參數類型是NSDatePicker

使用的NSDate * dateVariable = datePicker.date;而不是dateFromString如果你需要將其轉換爲NSDate的

一旦你的NSDate,你可以從https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

+0

事實上,如果用@「07/04/2012」替換dataPicker它可以工作,但我想用datePicker來設置所需的日期。 – Andrea 2012-04-09 09:45:32

+0

所以你可以使用datePicker.date而不是dateFromString調用 – Zuuum 2012-04-09 09:47:54

+0

我試過了:NSDate * dataInizio = [formatoData datePicker.date];但是不起作用。 – Andrea 2012-04-09 09:53:00

相關問題