客观c课程的简单问题

时间:2011-04-27 01:07:10

标签: objective-c xcode

我正在为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;
}

我认为程序应该输出零,一,二,三。

这里发生了什么?

2 个答案:

答案 0 :(得分:3)

您写道:

for(int i=0 <[items count];i++;)
那是一个错字吗?它应该是:

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

答案 1 :(得分:2)

for (NSString item in items) {
    NSLog(@"%@", item);
}