简体   繁体   English

Xcode中的未知错误

[英]Unknown errors in Xcode

I am following the instructions for a tutorial but I cannot figure out what is wrong. 我按照教程的说明进行操作,但我无法弄清楚出了什么问题。 I have double checked everything. 我仔细检查了一切。 I put the the compiler errors in the code's comments below. 我把编译器错误放在下面的代码注释中。 Sorry, this will probably show how much of a noob I am. 对不起,这可能会显示我有多少菜鸟。

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

你在for循环中声明iWeeksFromNow,这就是为什么编译器不认为它存在于外部声明它,并在里面为它赋值

当你调用-dateByAddingYears方法时,你有一个额外的结束}

First error : you declare iWeeksFromNew inside a for loop, thus it's unreachable from outside. 第一个错误:你在for循环中声明iWeeksFromNew,因此它无法从外部访问。 You have to declare before the beginning of the loop. 你必须在循环开始之前声明。

Second error : you have a bracket '}' after [array addObject:newEntry]; 第二个错误:在[array addObject:newEntry];之后你有一个括号'}' [array addObject:newEntry]; so the compiler thinks its the end of your method, remove it. 所以编译器认为它是你的方法的结束,删除它。

That should fix all other error you have 这应该可以解决你所有的其他错误

First, iWeeksFromNow is declared within the scope of a for loop, so it will be visible only within that loop. 首先, iWeeksFromNowfor循环的范围内声明,因此它只在该循环中可见。 Second, as pointed out by Black Frog , you have an extra closing parenthesis. 其次,正如Black Frog所指出的,你有一个额外的右括号。

Move the declaration out that loop block. 将声明移出循环块。 You've got a scope problem here: The iWeeksFromNew only exists within the loop 你在这里遇到了一个范围问题: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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM