I have a custom class below:
#import <Foundation/Foundation.h>
@interface NamesFix : NSObject
@property (nonatomic, copy) NSString *fixedName;
- (NSString *)fixName:(NSString *)name;
@end
Implementation file:
#import "NamesFix.h"
@implementation NamesFix
@synthesize fixedName;
- (NSString *)fixName:(NSString *)name
{
if ([name isEqualToString:@"Foo"])
{
self.fixedName = @"Bar";
}
else
{
self.fixedName = @"";
}
return self.fixedName;
}
When I access my custom class via:
NamesFix *namesFix = [NamesFix alloc] init];
NSString *someString = @"Foo";
[namesFix fixName:someString];
NSLog(@"fixedName: %@", namesFix.fixedName];
fixedName returns null. Where did I got it wrong?
Make your property copy which is the recommended memory management attribute for strings
(you could use retain instead, but copy protects you from others passing in the mutable
subclass NSMutableString and have it changed without you noticing) :
and in your implementation, USE the setter method that this property provides:
Instead of
edit
and also use
return self.fixedStringas a matter of habit.