1

So here is a partial sample of the relevant code.

static NSMutableArray *radioInputArray;
static NSMutableArray *buttonsArray;


- (IBAction)lookForRadioButtons:(id)sender {
    //  NSLog(@"Testing");
    NSError *error;
    NSString *radiostr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"getRadios" ofType:@"txt"] encoding:NSASCIIStringEncoding error: &error] ;
    if (radiostr == nil)
    {
        NSLog (@"Error! %@", error);
    }
    else
    {
        NSLog(@"%@",radiostr);
        NSString *radiotxt=  [webView stringByEvaluatingJavaScriptFromString:radiostr];
        NSLog(@"%@", radiotxt);
        NSArray *myRadios = [radiotxt componentsSeparatedByString:@"::"];
        [radioInputArray addObjectsFromArray:myRadios];
        NSLog(@"%d", myRadios.count);
        NSLog(@"Number of buttons in global radio array %d", radioInputArray.count);
        NSLog(@"%d", scrollViewer.subviews.count);
    }
}

So it throws no exceptions and seems to work properly except after addObjectsFromArray:, my count in the global NSMutableArray is 0 (the count in the myRadios = 56). I am pretty sure they should be equal at this point but are not. I have declared my NSMutableArray up near the top so that it can be globally accessed. Am I missing something such as allocating and initializing this? Does it not do that automatically like in C#? Again, this is my first foray into the Objective-C world from Windows programming so please be gentle yet feel free to be critical.

1
  • It's a hell of a gotchya. Just as you say it APPARENTLY WORKS, there's no error etc, in that situation! Horrific. Commented Feb 11, 2014 at 18:52

3 Answers 3

2

Your two global arrays are not initialized.

The lines

static NSMutableArray *radioInputArray;
static NSMutableArray *buttonsArray;

just define the two variables as pointers to NSMutableArray, so you need to get them to point at an actual instance of the class NSMutableArray.

Somewhere in your initialization code, or through an accessor (best if a class method), you should set the variables to an empty, newly allocated NSMutableArray.

Here is a way to do it:

+ (NSMutableArray*)radioInputArray
{
    if (!radioInputArray) {
        radioInputArray = [[NSMutableArray alloc] init]; 
    }

    return radioInputArray;
}

Then use the accessor in your code instead of the global variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Right I keep getting an error that receiver type MutableArray does not declare a method with selector alloc.
How stupid of me! It should be [[NSMutableArray alloc] init] Code changed in answer.
0

It may happen if your radioInputArray is nil, you didn't initialize the array

you need to add

[[radioInputArray alloc] init]; 

before you do anything with radioInputArray

1 Comment

Nothin. Went so far as to do the following in my header. property (weak, nonatomic) NSMutableArray *radioInputArray; Then placed the synthesize radioInputArray; Inside my .m file and still no love although it shows no build errors when I still get a 56 for my original array and a 0 for my radioInputArray. I have removed the at symbol as it wishes to notify people
0

Good place for initialising object is "init" method in Global class

Ex.

-(id)init
{
    if (self=[super init]) {

        self.globalAllArtworkArray=[[NSMutableArray alloc] init];
        self.globalCollectionArray=[[NSMutableArray alloc] init];
        self.globalLookbookArray=[[NSMutableArray alloc] init];

    }
    return self;

}

+(ASNGlobalClass *)shareManager
{
    static ASNGlobalClass *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];

    });

    return sharedInstance;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.