-1

I have an NSmutable array and I am adding some strings present in the C array to it. By using this method

if (!self.arrayOfVariableNames) {
        self.arrayOfVariableNames = [[NSMutableArray alloc] init];
        for (int i = 0; i< cols; i++) {
            [self.arrayOfVariableNames addObject:[NSString stringWithCString:cArrayOfVariableNames[i] encoding:NSUTF8StringEncoding ]];
        }
    }
    else{
        [self.arrayOfVariableNames removeAllObjects];
        for (int i = 0; i< cols; i++) {
        [self.arrayOfVariableNames addObject:[NSString stringWithCString:cArrayOfVariableNames[i] encoding:NSUTF8StringEncoding ]];
        }
    }   

Does this method ensure that the objects in the NSmutableArray won't be deallocated when the C array is taken out of memory?

9
  • YES ..Are you facing any problems ? Commented Sep 12, 2014 at 2:33
  • Actually a lot of problems. I am a self taught programmer and I am almost about to give up as I cant figure out certain things. I have to use this Array in a table view but by the time the tableview datasource methods are implemented, this array becomes null. Commented Sep 12, 2014 at 2:38
  • If your NSArray is becoming nil your not keeping a strong reference to it. Make it a property of your controller. Commented Sep 12, 2014 at 2:51
  • All arrays have strong reference. There is an NSTableView in a seperate ViewController which uses this this tableview for data and when I log the contents, I am getting an error Commented Sep 12, 2014 at 2:58
  • How are you passing the array to the second view controller? Commented Sep 12, 2014 at 3:15

3 Answers 3

1

if this array arrayOfVariableNames is becoming Null, then the problem is with the initialisation of the array. Please try to use Lazy loading by doing this:

- (NSArray*)arrayOfVariableNames {
    if (!_arrayOfVariableNames) {
        _arrayOfVariableNames = [[NSMutableArray alloc] init];  //initialise the array if needed
    }
    return _arrayOfVariableNames;  //else return the already initialized array
}

and please comment out this line in your code: self.arrayOfVariableNames = [[NSMutableArray alloc] init];

****EDIT****
Please find the update code in https://docs.google.com/file/d/0BybTW7Dwp2_vdHhQN1p1UzExdTA/edit?pli=1. Have a look at it.

13
0

Yes. NSArray retains anything in it. But you should stop chaining your NSString creation and instead creat a string a line before adding it to the array. Then check for nil. Only add it to the array if it is not nil. Do code defensively.

3
  • The NSStrings copy in data from the C-strings so they won't be modified when the C-strings get deallocated, and the NSArray will retain copies of the NSStrings so the NSStrings will not be released.. Commented Sep 12, 2014 at 2:42
  • The point is that you may not guarantee that you will get a NSString back from the C string. It can fail for many reasons. You cannot add nil to an NSArray or NSMutableArray. Commented Sep 12, 2014 at 2:49
  • I didn't see a string returning with nil in the docs, but you make a really good point. I was trying to clarify that the C-strings get copied into the NSStrings and hence persist after the C-strings are deallocated. Commented Sep 12, 2014 at 2:53
0

arrayOfVariableNames will not change when the C array get deallocated.

Make sure that your arrayOfVariableNames variable is strong.

@property (nonatomic, strong) NSMutableArray *arrayOfVariableNames;

    if (!self.arrayOfVariableNames) 
    {
      self.arrayOfVariableNames = [[NSMutableArray alloc] init];
    }
    else
    {
      [self.arrayOfVariableNames removeAllObjects];
    }

   for (int i = 0; i< cols; i++) 
   {
       NSString *tempString = [NSString stringWithCString:cArrayOfVariableNames[i] encoding:NSUTF8StringEncoding];

       if([tempString length] > 0)
       {
         [self.arrayOfVariableNames addObject:tempString];
       }
       else
       {
         NSLog(@"string is empty");
       }
   }

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.