testing
testing

Reputation: 229

Error in accessing global declared NSArray values in class methods

I have problem in fetching the element from array,

I have declared NSArray *array globally, also tried through myfile.h file property, but I'm not able to access array element in my view controller setCode method.

        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

It throwing some errors:

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

Upvotes: 2

Views: 375

Answers (5)

Swapnil Kotwal
Swapnil Kotwal

Reputation: 5720

Please try following code:

 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

Upvotes: 1

Puneet Sharma
Puneet Sharma

Reputation: 9484

What you did is to create a reference - local, here in this method.

-(void)viewDidLoad

But in your -(void)setCode method, you are accessing your global variable .

The global variable is not yet allocated a memory, neither initialized hence the error.

So you need to understand the scope of variables. Local scope preceeds global scope. Anyways you are working with two different array objects.

Change this in your viewDidload implementation to make it work:

// No NSArray at front that will make it an another array object
    array = [NSArray arrayWithObjects:@"sssss", @"hjjjjj", @"kkkkkk"];

Upvotes: 0

Tarek Hallak
Tarek Hallak

Reputation: 18470

You are declaring another object from the array inside your viewDidLoad, so your global array object will never get allocated, you need to remove the declaration as below:

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

And by the way the setCode doesn't do anything rather than printing!, you can change its name to printCode.

Upvotes: 1

Hemant_Negi
Hemant_Negi

Reputation: 2078

first you declare an array in implementation (.m file) like this

@implementation : myviewcontroller {
   NSArray *array; 
}

then initialize it like

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

it must work

Upvotes: 0

user1525369
user1525369

Reputation:

Add

@interface PeopelListViewController : UIViewController 
{
       NSArray *array; 
}

//in my.h file
//@property(nonatomic, retain) NSArra *array;
//and @synthesize array;

just you need to Put nil at the end of your NSArray.

such like

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

Upvotes: 0

Related Questions