I have the following simple class definition:
//mycommon.h
@interface CurrentPath : NSObject
@property (nonatomic, strong) NSString* PathString;
@property (nonatomic, strong) NSMutableArray* PathArr;
- (void) addAddressToPath:(NSString*) address;
@end
//mycommon.m
@implementation CurrentPath : NSObject
@synthesize PathString;
@synthesize PathArr;
- (void) addAddressToPath:(NSString*) address{
NSLog(@"addAddressToPath...");
// Add to string
self.PathString = [self.PathString stringByAppendingString:address];
// Add to Arr
[self.PathArr addObject:address];
}
@end
In another class I do #import<mycommon.h> and declare the variable like this:
@interface myDetailViewController :
{
CurrentPath* currentPath;
}
- (void) mymethod;
@end
and in
@implementation myDetailViewController
- void mymethod{
self->currentPath = [[CurrentPath alloc] init];
NSString* stateSelected = @"simple";
[self->currentPath addAddressToPath:stateSelected];
}
@end
Problem is that the PathString and PathArr properties of self->currentPath are empty after this method call which I think should have “simple” in them. Please help!
You have to make sure that your
NSStringandNSMutableArrayproperties are initialized when yourCurrentPathobject is created. Otherwise, the call tostringByAppendingStringwill result innilbecause it is sent to anilobject.One feasible way would perhaps be
More elegant and robust would be to check for nil property in the
addAddressToPathmethod.Notice that am following the objective-c convention and use property names that start with lower case letters.