2013-08-07 28 views
2

我做了一個非常基本的iOS應用程序,它在viewDidLoad上的標籤上顯示時間。 我在這行代碼得到一個期望的標識符錯誤:iOS應用程序中的「預期標識符」錯誤

NSString *myTime = [myDateFormatter stringFromDate:[*myDate]]; 

Here'e的ViewController.m文件:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize timeLabel; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init]; 
    [myDateFormatter setDateFormat:@"HH:mm a"]; 
    NSDate *myDate = [[NSDate alloc] init]; 
    NSString *myTime = [myDateFormatter stringFromDate:myDate]; 
    [timeLabel setText : myTime]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 

提出了一些補充。代碼編譯,但現在我得到一個線程1:SIGABRT錯誤。 該應用程序無法加載模擬器,並且出現錯誤。 這裏的代碼是給錯誤在main()行:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 

是啊,我也沒聯繫起來。它的工作現在。謝謝你們!

+1

不要只是刪除*,還要刪除[]周圍的myDate。 – amb

+0

我只是測試你的代碼與NSString * myTime = [myDateFormatter stringFromDate:myDate];它給了我時間。 –

+0

你在界面生成器中鏈接了timeLabel嗎?這段代碼也適用於我。 – AMAN77

回答

7

刪除*這樣

NSString *myTime = [myDateFormatter stringFromDate:mydate]; 
+0

我認爲最好使用''[NSDate date]''代替''[[NSDate alloc] init]'',因爲不需要在提供的代碼中保留日期。 – amb

0

試試這個NSString *myTime = [myDateFormatter stringFromDate:[NSDate date]];

0

試試這個:當同時運行應用程序時遇到異常發生NSString *myTime = [myDateFormatter stringFromDate:myDate];

0

SIGABRT錯誤。 這可能是由於內存異常或任何其他運行時錯誤。

你可以嘗試清理和建立你的項目。如果它仍然存在,請檢查目標調試器控制檯。

0

我與SKM17 NSString *myTime = [myDateFormatter stringFromDate:mydate]; 但只要確保有一些MyDate中與

NSLog("mydate = %@",mydate); 

,如果它仍然沒有出現。日誌記錄始終是一個好主意,以檢查值是否應該在哪裏:D