addObjectsFromArray: not copying into global NSMut

2019-08-04 00:29发布

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.

3条回答
成全新的幸福
2楼-- · 2019-08-04 00:57

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.

查看更多
Lonely孤独者°
3楼-- · 2019-08-04 00:57

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;
}
查看更多
Deceive 欺骗
4楼-- · 2019-08-04 01:11

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

查看更多
登录 后发表回答