2 ooomiao oOOMIAO 于 2013.08.06 16:21 提问

访问全局声明NSArray值报错

获取数组中的元素

将nsarray声明为全局。但是无法在视图控制器的setcode方法中访问数组元素。

NSArray *array; 
//在.h文件中的全局数组 
//@property(nonatomic, retain) NSArra *array; 
//访问通过@synthesize数组; 没实现 
-(void)viewDidLoad
        {
            array = [NSArray arrayWithObjects:@"sssss", @"hjjjjj", @"kkkkkk"];
             [self setCode];          
        }  
    -(void)setCode
    {
           NSString *code = [array objectAtIndex:0];
            NSLog(@"code %@",code);
    }
@end

报错信息:

2013-08-05 16:25:28.429 test_project_ios_31st_july[2409:c07] -[__NSMallocBlock__ objectAtIndex:]: unrecognized selector sent to instance 0x9c82350
2013-08-05 16:25:28.430 test_project_ios_31st_july[2409:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ objectAtIndex:]: unrecognized selector sent to instance 0x9c82350'
*** First throw call stack:
(0x23fb012 0x1c98e7e 0x24864bd 0x23eabbc 0x23ea94e 0x283f 0x17594f9 0x24550c5 0x23afefa 0x168dbb2 0x16a0e6b 0xdb9f 0x16d2417 0x16ebb24 0x16a0d60 0x174da8a 0x43ac2 0x16d2417 0x16ebb24 0x16a0d60 0x174da8a 0x464d5 0x17594f9 0x24550c5 0x23afefa 0x16a0a0c 0xa3d0e6 0x206353f 0x2075014 0x20657d5 0x23a1af5 0x23a0f44 0x23a0e1b 0x2ad87e3 0x2ad8668 0xbdcffc 0x2192 0x20c5)
libc++abi.dylib: terminate called throwing an exception

2个回答

fans1991
fans1991   2013.08.07 17:06

数组没有分配内存 [NSArray alloc],试试、、、

u011367651
u011367651   2013.08.08 20:42

起码一点,数组赋值后面,没有nil,nil是结束标志,没有能行?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!