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?