Reputation: 5089
I'm a newbie to Objective-C, so please bear with me.
I have a custom object called UpdateData:
@interface UpdateData : NSObject
-(NSString*) getPath;
-(void) setPath:(NSString*) path ;
@end
As I create this items from parsing a list:
NSMutableArray *serverData = [[NSMutableArray alloc]init];
for (i = 1; i < [chunks count]; i++) {//chunks contains infor for parsing
UpdateData *ud = [[UpdateData alloc] init];
NSString * element = [chunks objectAtIndex:i];
[ud setPath: element];
[serverData addObject:ud];
NSLog(@"Path: %@",[[serverData lastObject] getPath]);
}
NSLog(@"Done parsing! Elements: %lu", [serverData count]);
Within that loop, the UpdateData (read straight from the Array) have the values. I get the following output:
Path: path
.
.
.
Done parsing! Elements: x
Where x is greater than 0 and equal to the objects parsed and path is the correct path parsed.
In the same method I later try to go over the Array to read the values of the objects:
for (i = 0; i < [serverData count]; i++) {
UpdateData *ud = [serverData objectAtIndex:i];
NSLog(@"Path: %@",[ud getPath]);
}
There the output is:
Path:
Values are lost. The for loops are back to back, exactly as shown above.
Any ideas?
Update:
The UpdateData class:
#import "UpdateData.h"
@implementation UpdateData
NSString* path=@"";
-(NSString*) getPath{
return path;
}
-(void) setPath:(NSString*) newpath{
path = newpath;
}
@end
Does that initialization there matter? Should be overwritten right?
Upvotes: 0
Views: 108
Reputation: 11233
Try like this: (untested)
.h
file:
@interface UpdateData : NSObject
@property (nonatomic, readonly) NSString *path;
/*-(NSString*) getPath;*/
-(void) setPath:(NSString*) path ;
@end
.m
file:
@interface UpdateData ()
@property (nonatomic, readwrite) NSString *path;
@end
@implementation UpdateData
/*
-(NSString*) getPath{
return path;
}*/
-(void) setPath:(NSString*) newpath{
path = newpath;
}
@end
Upvotes: 0
Reputation: 1493
Declare the Path
property in the implementation like this:
#import "UpdateData.h"
@implementation UpdateData {
NSString *_path;
}
-(NSString*) getPath{
return _path;
}
-(void) setPath:(NSString*) newpath{
_path = newpath;
}
@end
Since Xcode handles automatically the getters and setters. If you don't need them for any other internal class purpose. I'd suggest you to use this line only in your .h
file and remove the getters and setters:
@property (strong, nonatomic) NSString *path;
Upvotes: 2