2011-04-27 33 views
2

我學習目標C iPhone和運行到一些小錯誤的目標:簡單的問題用C教訓

控制檯輸出:

kill 
quit 

The Debugger has exited with status 0. 
[Session started at 2011-04-26 17:51:40 -0700.] 
GNU gdb 6.3.50-20050815 (Apple version gdb-1510) (Wed Sep 22 02:45:02 UTC 2010) 
Copyright 2004 Free Software Foundation, Inc. 
GDB is free software, covered by the GNU General Public License, and you are 
welcome to change it and/or distribute copies of it under certain conditions. 
Type "show copying" to see the conditions. 
There is absolutely no warranty for GDB. Type "show warranty" for details. 
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000 
Loading program into debugger… 
Program loaded. 
run 
[Switching to process 13334] 
2011-04-26 17:51:40.788 RandomPossessions[13334:a0f] Two 
2011-04-26 17:51:40.792 RandomPossessions[13334:a0f] Three 
2011-04-26 17:51:40.793 RandomPossessions[13334:a0f] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (4) beyond bounds (4)' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00007fff81d007b4 __exceptionPreprocess + 180 
    1 libobjc.A.dylib      0x00007fff856730f3 objc_exception_throw + 45 
    2 CoreFoundation      0x00007fff81d005d7 +[NSException raise:format:arguments:] + 103 
    3 CoreFoundation      0x00007fff81d00564 +[NSException raise:format:] + 148 
    4 Foundation       0x00007fff88ef6aa0 _NSArrayRaiseBoundException + 122 
    5 Foundation       0x00007fff88e59bc5 -[NSCFArray objectAtIndex:] + 75 
    6 RandomPossessions     0x0000000100000e8d main + 301 
    7 RandomPossessions     0x0000000100000d58 start + 52 
    8 ???         0x0000000000000001 0x0 + 1 
) 
terminate called after throwing an instance of 'NSException' 
Running… 
Program received signal: 「SIGABRT」. 
sharedlibrary apply-load-rules all 
(gdb) 

我的代碼:

#import <Foundation/Foundation.h> 

int main (int argc, const char * argv[]) { 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    // insert code here... 
    NSMutableArray *items = [[NSMutableArray alloc] init]; 

    [items addObject:@"One"]; 
    [items addObject:@"Two"]; 
    [items addObject:@"Three"]; 
    [items insertObject:@"Zero" atIndex:0]; 

    for(int i=0 <[items count];i++;) 
    { 
     NSLog(@"%@", [items objectAtIndex:i]); 
    } 

    [pool drain]; 
    return 0; 
} 

我想該程序應輸出零,一,二,三。

這裏發生了什麼?

回答

3

您寫道:

for(int i=0 <[items count];i++;) 

是一個錯字?它應該是:

for(int i=0; i<[items count]; i++) 
2
 
for (NSString item in items) { 
    NSLog(@"%@", item); 
}