This is the original codes when I am getting this error
ClassA.h
+(ClassA*)methodA:(NSData*)data;
@property (nonatomic, strong) NSMutableArray *arr; @property (nonatomic, strong) RXMLElement *rxmlRoot;@end
ClassA.m
+(ClassA*)methodA:(NSData*)data {
//NSLog(@"class is %@",[name class]); ClassA *ClassA = [[Stores alloc] init]; arr = [NSMutableArray array]; rxmlRoot = [RXMLElement elementFromXMLData:data];}
I am reviewing my codes and now I have tow options to fix the code
OptionA
ClassA.h
+(ClassA*)methodA:(NSData*)data; @property (nonatomic, strong) NSMutableArray *arr; @property (nonatomic, strong) RXMLElement *rxmlRoot; @endClassA.m
+(ClassA*)methodA:(NSData*)data { //NSLog(@"class is %@",[name class]); ClassA *ClassA = [[Stores alloc] init]; ClassA.arr = [NSMutableArray array]; <----- this has been modified ClassA.rxmlRoot = [RXMLElement elementFromXMLData:data];<----- this has been modified }
OptionB
ClassA.h
+(ClassA*)methodA:(NSData*)data; @endClassA.m
static RXMLElement *rxlRoot; <----- this has been added as class variable static NSMutableArray *arr; <----- this has been added as class variable +(ClassA*)methodA:(NSData*)data { //NSLog(@"class is %@",[name class]); ClassA *ClassA = [[Stores alloc] init]; arr = [NSMutableArray array]; rxmlRoot = [RXMLElement elementFromXMLData:data];}
I try either optionA or optionB and the compiler does not complain anything and the codes work properly.
I see many posts following the optionB which create class variables.
Question: is optionA also a good way to follow when we are dealing with class method and class variable as well.
Please advice me on this issue and by the way if I have made mistakes somewhere, please correct me. Thanks
Create a designated initializer with a data parameter. Every class should have a designated initializer.
In your class method call the designated initializer passing in
data.Here is example code:
ARC is assumed in the example.