2013-12-16 68 views
0

我很新的iOS編程(你可以從我的代碼可以告訴),我有困難。我的應用程序是一個包含許多ViewControllers的調查問卷,每個ViewController都有一個問題。這些問題的答案存儲在一個NSMutableArray中。然後,我在最後一個視圖上呈現答案,一個UITableView。除了在tableview中顯示日期之外,一切都很好。我得到這個錯誤信息:轉換NSDate到NSString保存在NSMutableArray

2013-12-16 14:04:48.609 Questionnaire[4602:70b] -[__NSDate length]: unrecognized selector sent to instance 0x8ac5160 
2013-12-16 14:04:48.613 Questionnaire[4602:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate length]: unrecognized selector sent to instance 0x8ac5160' 

我的數組是這樣的:

2013-12-16 14:04:32.743 Questionnaire[4602:70b] (
    1234, 
    "2013-12-16 14:04:24 +0000", 
    Yes, 
    Internet, 
    "Not Applicable", 
    No 
) 

正如你可以看到,日期存儲在默認的方式,雖然我不能在TableView中顯示它除非它被轉換爲NSString。

這是我的tableview的圖片。注意:日期顯示爲「空」用於測試目的,請參閱代碼。 (我沒有足夠的代表來附加圖片)。

My tableview

我GOOGLE了字面上天,試過各種代碼,我仍然不能得到它保存到陣列之前轉換。我試圖從UIDatePicker在UITextField上顯示日期,然後我將從該文本字段中保存日期,但沒有運氣。如果任何人能夠指導我如何將日期轉換爲NSString,然後將其保存到數組中,或者如果有更好的方法解決我的問題,請幫助我!

感謝您的閱讀。

這裏是我的代碼:

- (IBAction)buttonPressed:(UIButton *)sender { 

    NSDate *dob = self.datePicker.date; 
    NSString *idNum = self.idTextField.text; 

    // If data is input to TextField 
    if (![self.idTextField.text isEqualToString:@""]) { 

     NSLog(@"Questionnaire started"); 

     // Allocate & initialise array 
     self.submission = [[NSMutableArray alloc] init]; 

     // Insert ID and DoB 
     [self.submission insertObject:idNum atIndex:0]; 
     [self.submission insertObject:dob atIndex:1]; 


     // Prepare array, insert Null values 

     //[self.submission insertObject:@"Null" atIndex:1]; // for testing 

     [self.submission insertObject:@"Null" atIndex:2]; 
     [self.submission insertObject:@"Null" atIndex:3]; 
     [self.submission insertObject:@"Null" atIndex:4]; 
     [self.submission insertObject:@"Null" atIndex:5]; 
     NSLog(@"%@", self.submission); 


     Q1ViewController *Q1 = [[Q1ViewController alloc]initWithNibName:@"Q1ViewController" bundle:nil]; 
     NSLog(@"Move to Q1"); 

     //Pass array to next viewcontroller 
     Q1.submission = self.submission; 

     [self.navigationController pushViewController:Q1 animated:YES]; 


    } else { //If TextField is empty 
     NSLog(@"TextField is empty"); 

     //Show error alert 
     UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"Incomplete Submission" 
                 message:@"Please enter a User ID and Date of Birth." 
                 delegate:self 
               cancelButtonTitle:@"Dismiss" 
               otherButtonTitles:nil]; 
     [myAlert show]; 
    } 
} 

編輯:

看起來這是一個簡單的解決方案,我完全錯過了,感謝的iOS學習「這個問題已經解決了短短的幾行的代碼!非常感激!!

NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] 
                  dateStyle:NSDateFormatterShortStyle 
                  timeStyle:NSDateFormatterFullStyle]; 
    NSLog(@"%@",dateString); 

回答

3
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] 
                   dateStyle:NSDateFormatterShortStyle 
                   timeStyle:NSDateFormatterFullStyle]; 
     NSLog(@"%@",dateString); 

檢查這個代碼...您的字符串傳遞給數組

+0

,或者你需要 –

+0

這工作完全修改!我不能相信它只有幾行代碼,你不知道我有這麼多麻煩:PI試圖投票,但我沒有足夠的代表,對不起:非常感謝雖然!!我會用你的解決方案更新帖子:) – rosshump

+1

@rosshump你應該接受答案。點擊答案評分下方的白色複選標記。 – rmaddy