Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-09-2010, 04:10 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Poland
Age: 30
Posts: 6
krisix is on a distinguished road
Question addObject to NSMutableArray

Hello,

I have a problem with adding custom objects to NSMutableArray. I've defined my class Absent.

Absent.h:
Code:
#import <Foundation/Foundation.h>

@interface Absent : NSObject {

	NSString *Username;

	NSDate *From;
	NSDate *To;

	NSString *Type;

}

@property (nonatomic,retain,readonly) NSString *Username;
@property (nonatomic,retain,readonly) NSDate *From;
@property (nonatomic,retain,readonly) NSDate *To;
@property (nonatomic,retain,readonly) NSString *Type;

- (id) initWithUsername:(NSString *)username from:(NSDate *)from to:(NSDate *)to type:(NSString *)type;

@end
Absent.m:
Code:
#import "Absent.h"

@implementation Absent

@synthesize Username, From, To, Type;

- (id) init {
	self = [super init];
	if (self != nil) {
		Username = [[NSString alloc] init];
		Type = [[NSString alloc] init];
		From = [[NSDate alloc] init];
		To = [[NSDate alloc] init];
	}
	return self;
}

- (id) initWithUsername:(NSString *)username from:(NSDate *)from to:(NSDate *)to type:(NSString *)type {
	self = [super init];

	if (self != nil) {
		Username = username;
		From = from;
		To = to;
		Type = type;
	}
	
	return self;	
}

@end
In another part of code I am initializing NSMutableArray with this code:

Code:
Absents = [[[NSMutableArray alloc] init] retain];
Next I'm trying to add object to my array:

Code:
Absent *userAbsent = [[[Absent alloc] initWithUsername:_xmlUsername from:fromDate to:toDate type:_xmlType] retain]; 

[Absents addObject:userAbsent];

[userAbsent release];
In a debugger I can see that NSMutableArray Absents has 1 object but it is marked as "out of scope". I've just changed my code for testing to this simple code:

Code:
NSString *test = [[NSString alloc] initWithString:@"Nieobecność"];
[Absents addObject:test];
[test release];
Above code works ok. What's wrong with my code and adding custom object?
krisix is offline   Reply With Quote
Old 05-09-2010, 10:19 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Your code looks OK, except that you're calling "retain" where you don't need to. Objects that you init or copy start with a retainCount of 1, so you don't need to retain them right away.

Make sure it's a code problem and not a debugger problem. Try this to see if the object is really being saved:

Code:
Absent *userAbsent = [[Absent alloc] initWithUsername:_xmlUsername from:fromDate to:toDate type:_xmlType]; 

[Absents addObject:userAbsent];

[userAbsent release];

Absent *testUser = [Absents objectAtIndex:0];
NSLog (@" %@ ", testUser.Username);
I also see you're not retaining the inputs to your init method - you should, in case they get autoreleased or released by the original owner.

Code:
if (self != nil) {
	Username = [username retain];
	From = [from retain];
	To = [to retain];
	Type = [type retain];
}
Normally you would just say self.Username = username and let the property handle the retain for you, but Apple suggests not using the property in the init and dealloc methods.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 05-10-2010, 12:11 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Poland
Age: 30
Posts: 6
krisix is on a distinguished road
Default

Thanks for your help, smasher!

I will make some tests today.
krisix is offline   Reply With Quote
Old 05-10-2010, 03:00 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2009
Location: Poland
Age: 30
Posts: 6
krisix is on a distinguished road
Default

I've made some changes to code which you suggests. But then I've problems in another part of code which was working ok.

So I've decided to test my app on iPhone - not on iPhone simulator. And now it works ok! So all my problems was related with iPhone simulator. On iPhone everything works ok!

With iPhone Simulator I've problems with accessing to my objects and the last problem was initializing NSDate object. On iPhone everything is ok.

Maybe it is caused that I'm using iPhone OS 4.0 Beta 3.

Thanks again Smasher for your help!
krisix is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 468
14 members and 454 guests
7twenty7, AlanFloyd, David-T, imac74, Jaxen66, logan, lovoyl, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:08 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0