objective-c - 客观c类(class)的简单问题

标签 objective-c xcode

我正在学习 iPhone 的 Objective 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;
}

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

这里发生了什么?

最佳答案

你写道:

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

这是一个错字吗?应该是:

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

关于objective-c - 客观c类(class)的简单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5798219/

相关文章:

ios - 由于未捕获的异常 'NSRangeException' 索引而终止应用程序

ios - 如何隐藏 tableview 特定单元格上的按钮?

ios - 使用 "Automatically manage signing"/allowProvisioningUpdates 时何时更新配置文件?

iphone - 从 Storyboard 文件/NIB 文件上的控件生成的 ID 值背后的逻辑

objective-c - 在 iOS 中更改 ViewControllers 和传递数据

xcode - 架构arm64的 undefined symbol : Azure mobile Services (o)

objective-c - 以编程方式发送 PayPal 付款 Objective C

iphone - 带有页面的 UIScrollView 内的 UIDatePicker

ios - 如何避免小时数被标记为日历链接?

iphone - 动态创建核心数据模型(实体、属性)