NSMutable Array, custom object losing string values

Discussion in 'iOS Programming' started by AZWolf7, Oct 23, 2010.

  1. AZWolf7 macrumors newbie

    Joined:
    Oct 23, 2010
    #1
    I have an array of items (NSMutableArray) which I am populating with strings (and for testing I added some int values).


    In a method for testing, I loop through, and create 10 of these objects and place them in a NSMutable array as follows:

    Code:
    listOfItems = [[NSMutableArray alloc] init];
    	
    	for(int idx=0;idx <=9;idx++)
    	{
    		ScannedUserInfo *userInfo = [[ScannedUserInfo alloc] init];
    		userInfo.int1 = idx;
    		userInfo.int2 = idx;
    		userInfo.companyName = [NSString stringWithFormat:@"Company Number:%d", idx];
    		userInfo.fullUserName = [NSString stringWithFormat:@"Customer:%d", idx];
    		[userInfo retain];
    		[listOfItems addObject:userInfo];
    	}
    
    [listOfItems retain];
    

    When I use this object in anther routine (I am binding it to a UITableView) the int values are there, but the string values are not. I suspect it's how I have them defined in my ScannedUserInfo class, but I am at my wits end, being new to Objective C. Here's how I have my ScannedUserInfo class defined.

    Code:
    #import <Foundation/Foundation.h>
    
    @interface ScannedUserInfo : NSObject
    {
    	NSString* fullUserName;
    	NSString* companyName;
    	
    	NSInteger int1;
    	NSInteger int2;
    }
    
    
    @property (readwrite) NSString* fullUserName;
    @property (readwrite) NSString* companyName;
    @property (readwrite) NSInteger int1;
    @property (readwrite) NSInteger int2;
    
    @end
    
    Am I defining my properties wrong?

    Thanks!
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Yes. The properties that are objects need either the copy or retain attribute. If they don't have it, then autoreleased strings won't be owned by the ScannedUserInfo that stores them.

    You're also over-retaining userInfo and listOfItems. Think about the object ownership in both cases. You called alloc so you already own the object. What purpose does another retain serve? If anything, userInfo should be released, because adding it to the array causes the array to send a retain, since it needs an ownership claim as well.

    If any of that doesn't make sense, you should read (or re-read) the memory management guidelines:
    http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
     
  3. AZWolf7 thread starter macrumors newbie

    Joined:
    Oct 23, 2010
    #3
    Much thanks!

    That solved it, I knew it was somehow losing the reference but didn't know where/how to fix it.

    As for all the excess "retains", that was a desperation attempt to figure out how to retain the references. I added the copy attribute to the property, and it's working correctly. Now to go remove all the desperation code. :)

    Much thanks!!
     

Share This Page