-3

I have an issue with NSMutablestring. When i use it without override my class init method it doesn't work.

my interface code:

@interface Topic : NSObject

@property (strong, nonatomic) NSMutableString *title;
@property (strong, nonatomic) NSMutableString *description;

@end

If I created an object with this class then tried to assign a string to the "title", its NSLog prints Null!

This is my code when I'm trying to use it:

Topic *myTopic = [[Topic alloc] init];
[myTopic.title appendString:@"Hello World"];

To avoid this problem I have to override my Topic class init method and add this line:

self.title = [[NSMutableString alloc]init];

Any idea?

EDIT: this line should initialize my properties and methods right (without overriding the init method)?

Topic *myTopic = [[Topic alloc] init];

Thanks,

bspider
  • 28
  • 4
  • 3
    what is the problem? – Bryan Chen Aug 28 '14 at 02:54
  • 2
    You have to override the `init` method and allocate your strings or allocate them elsewhere manually... They don't magically initialize themselves. – rebello95 Aug 28 '14 at 02:54
  • Please, please, please at least study some elementary Objective-C tutorials. – Hot Licks Aug 28 '14 at 02:56
  • i think its enough to initialize my methods and properties with this line (without overriding the init method): Topic *myTopic = [[Topic alloc] init]; – bspider Aug 28 '14 at 03:09
  • 1
    possible duplicate of [Properties don't get initialized in iOS 7](http://stackoverflow.com/questions/23439563/properties-dont-get-initialized-in-ios-7) – Paulw11 Aug 28 '14 at 03:20

1 Answers1

1

When you first create your object, the title object is nil. You must initialize the title object before you can append anything to it.

self.title = [[NSMutableString alloc] init];

You should set this in the appropriate place - e.g., viewDidLoad on a view controller.

See Tommy's comment below for why you wouldn't want to do this in the init method itself.

nzeltzer
  • 521
  • 4
  • 9
  • 1
    I'm not the down voter, and I doubt this is the reason for the down vote but: you should never call a setter within your `init`. You don't know whether a subclass has overridden it. – Tommy Aug 28 '14 at 02:59
  • This information is already contained in the original post. – jscs Aug 28 '14 at 03:01
  • I'm sorry, I should have specified. – nzeltzer Aug 28 '14 at 03:01
  • If i have to initialize every property in my Class, what is the idea behind writing: MyClass *class = [[MyClass alloc] init] – bspider Aug 28 '14 at 03:13
  • 1
    That creates an instance of the class. But it's still up to you to create and configure any objects that your class declares as properties. You may want to assign initial values, or to leave certain properties empty — neither the language nor the framework can accurately infer your intent just from the declarations in your interface. – nzeltzer Aug 28 '14 at 03:17
  • Ahmed, you might find it helpful to check out this basic overview of creating a class in ObjC: http://rypress.com/tutorials/objective-c/classes.html – nzeltzer Aug 28 '14 at 03:28