2011-04-06 70 views
1

我按照教程的说明进行操作,但我无法弄清楚什么是错误的。我已经加倍检查了一切。我将编译器错误放在下面的代码注释中。对不起,这可能会显示我是多少个noob。Xcode中的未知错误

// main.m 


#import <Foundation/Foundation.h> 
#import "LotteryEntry.h" 

int main (int argc, const char * argv[]) 
{ 

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    // Creates the date object 

    NSCalendarDate *now = [[NSCalendarDate alloc]init]; 

    //Seed the random number generator 

    srandom(time(NULL)); 
    NSMutableArray * array; 
    array = [[NSMutableArray alloc]init]; 

    int i; 
    for (i = 0; i < 10; i++) { 

     //create a date/time object that is 'i' weeks from now 

     NSCalendarDate *iWeeksFromNow; 
     iWeeksFromNow = [now dateByAddingYears:0 
             months:0 
              days:(i * 7) 
              hours:0 
             minutes:0 
             second:0]; 
    } 

    //create the LotteryEntry object 

    LotteryEntry *newEntry = [[LotteryEntry alloc]init]; 
    [newEntry prepareRandomNumbers]; 
    [newEntry setEntryDate: iWeeksFromNow]; 

//Error says "Use of undeclared identifier "iWeeksFromNow'. Did I not declare it above? 

    //add the lottery entry object to the array 

    [array addObject:newEntry]; 

    } 

    for (LotteryEntry *entryToPrint in array) { 

//Error says " Expected identifier or '(' 


     //Display it's contents 

     NSLog(@"%@", entryToPrint); 

    } 





[pool drain]; 
return 0;  
//Error says " Expected identifier or '(' 
}     
//Error says " Expected External declaration 

回答

2

您声明iWeeksFromNow里面一个for循环,这就是为什么编译器不会认为它外面 存在外部声明它,并赋值给它里面

+0

呀,黑青蛙是正确的,for循环剪短中存在,它应该去} [阵列ADDOBJECT:newEntry]下,而不是仅仅在iWeeksFromNow之后 – jcane86 2011-04-06 18:54:15

1

您拨打的-dateByAddingYears方法你有一个额外的收盘}

0

首先,iWeeksFromNow是一个范围内声明for循环,因此只有在该循环中才可见。其次,正如Black Frog所指出的,你有一个额外的右括号。

1

第一个错误:您在for循环中声明了iWeeksFromNew,因此它无法从外部访问。 你必须在循环开始之前声明。

第二个错误:在[array addObject:newEntry];之后有一个括号'}',因此编译器认为它是方法的结尾,将其删除。

这应该可以解决所有其他错误你有

0

移动的声明指出,循环块。你已经有了一个范围问题就在这里:在iWeeksFromNew只在循环

NSCalendarDate *iWeeksFromNow; 
int i; 
for (i = 0; i < 10; i++) { 

    //create a date/time object that is 'i' weeks from now 
    iWeeksFromNow = [now dateByAddingYears:0 
            months:0 
             days:(i * 7) 
             hours:0 
            minutes:0 
            second:0]; 
}