Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AZWolf7

macrumors newbie
Original poster
Oct 23, 2010
2
0
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!
 
Am I defining my properties wrong?

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
 
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!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.