2

我在从数组中获取元素时遇到问题,

我已经全局声明了 NSArray *array,也尝试了通过 myfile.h 文件属性,但是我无法在我的视图控制器 setCode 方法中访问数组元素。

        NSArray *array; 
//this is global array also i tried in my.h file 
//@property(nonatomic, retain) NSArra *array; 
//and by accessing it @synthesize array; also not works for me.

-(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
4

5 回答 5

1

请尝试以下代码:

 NSArray *array; 
-(void)viewDidLoad
        {
            array = [NSArray arrayWithObjects:@"sssss", @"hjjjjj", @"kkkkkk"];
             [array  retain];
             [self setCode];          
        }

    -(void)setCode
    {
           NSString *code = [array objectAtIndex:0];
           NSLog(@"code %@",code);
           [array  release];
    }
@end
于 2013-08-05T11:25:03.703 回答
1

您正在从array内部声明另一个对象viewDidLoad,因此您的全局array对象将永远不会被分配,您需要删除以下声明:

    -(void)viewDidLoad
    {
        array = [NSArray arrayWithObjects:@"sssss", @"hjjjjj", @"kkkkkk"];
        int i;
        [self setCode];          
    }

顺便说一句,setCode除了打印之外什么都不做!,您可以将其名称更改为printCode.

于 2013-08-05T11:07:39.533 回答
0

添加

@interface PeopelListViewController : UIViewController 
{
       NSArray *array; 
}

//在 my.h 文件中
//@property(nonatomic, retain) NSArra *array;
//和@synthesize数组;

只是你需要放在nil你的 NSArray 的末尾。

像这样

self.array = [NSArray arrayWithObjects:@"sssss", @"hjjjjj", @"kkkkkk", nil];
于 2013-08-05T11:07:35.907 回答
0

您所做的是在此方法中创建一个参考 - 本地。

-(void)viewDidLoad

但是在您的-(void)setCode方法中,您正在访问全局变量。

全局变量尚未分配内存,也未初始化,因此出现错误。

因此,您需要了解scope of variables.本地范围先于全局范围。无论如何,您正在使用两个不同的数组对象。

在您的viewDidload实现中更改它以使其工作:

// No NSArray at front that will make it an another array object
    array = [NSArray arrayWithObjects:@"sssss", @"hjjjjj", @"kkkkkk"];
于 2013-08-05T11:08:13.503 回答
0

首先,您在实现(.m 文件)中声明一个数组,如下所示

@implementation : myviewcontroller {
   NSArray *array; 
}

然后像这样初始化它

 array = [NSArray arrayWithObjects:@"sssss", @"hjjjjj", @"kkkkkk", nil];

它必须工作

于 2013-08-05T11:10:29.537 回答