0

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?

14
  • 1
    What is getPath doing? By the way: Getters in Objecive-C don't begin with 'get'. Commented Feb 11, 2014 at 17:54
  • 4
    The bug seems to be related to your UpdateData class, not the NSMutableArray. Please provide the code of your UpdateData class Commented Feb 11, 2014 at 17:55
  • 1
    Returns a string or is supposed to return a string? Debug it! Find out what's going on! Learn how to set breakpoints and examine data values. Commented Feb 11, 2014 at 18:19
  • 1
    (If you don't know how to debug, ask some questions about that. Yes, you will get razzed a bit, but you will learn something.) Commented Feb 11, 2014 at 18:22
  • 1
    You didn't implement setPath()? Commented Feb 11, 2014 at 18:24

2 Answers 2

2

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;

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.