I have a singleton initializer which is a class method. I want to set instance variable data from it. How can I do this?
4 Answers
You need an instance to call an instance method. So, either create an instance and call its methods or use class methods.
Should I change those instance methods to be class methods?
If the methods are not related to an instance, it might be a good idea to change them to class methods.
2 Comments
This init Method For Example. It's the way I do it...
-(id)initWithName:(NSString *)name {
if(self = [super init]) {
self.name = name;
}
return self;
}
In this case the instance variable needs also to be a property. Else you can't write self.name.
Sandro
1 Comment
Than I don't know what you mean. Do you want to set a instance variable? That's possible. (See example below) But if you want to access one, it's not possible. Because they don't exists in the class. They only exist in an object... Example:
+(YourClass *)YourClassWithName:(NSString *)name {
if(self = [super init]) {
self.name = name;
}
return [self autorelease];
}
If you didn't mean that, I don't know what you meant. ^^