This probably sounds bare bones basic, but I need help. I'm a bit new to objective-c. So I have a void method that that sets a property. How would I use that same property inside of a public method? Here's the code:
.h
@property (strong, nonatomic) NSString *globalShowKey;
.m
+(NSString *)fileStructure{
NSString *mainDBPath = [PATH stringByAppendingPathComponent:CLIENT_KEY];
NSString *subDirectory = [mainDBPath stringByAppendingPathComponent:globalShowKey];
return subDirectory;
}
Could someone please give me an explanation what the best approach to achieve this? I am so close to achieving this! If I am unclear, please let me know.
You are using self
in class method as +
in method definition indicates its class method and in class method self
represent class
not instance of that class.This line is causing trouble
//self represent here class not instance but you need to access instance variable through property
NSString *subDirectory = [mainDBPath stringByAppendingPathComponent:self.globalShowKey];
use -
instead of +
in method as +
indicates class method and -
indicates instance method
-(NSString *)fileStructure{
NSString *mainDBPath = [PATH stringByAppendingPathComponent:CLIENT_KEY];
NSString *subDirectory = [mainDBPath stringByAppendingPathComponent:self.globalShowKey];
return subDirectory;
}
Now call this method on class instance for eg: if this method is defined in class Abc
than instead of using [Abc fileStructure];
make object of class Abc
Abc *abc = [[Abc alloc] init];
NSString *fileStructure = [abc fileStructure];
EDIT : You can also make self.globalShowKey
as constant string if it is not changing througout application like NSString *const GlobalShowKey = @"abc";
//write this after #import
statements
and than you can append this global key using your previous class method
+(NSString *)fileStructure{
NSString *mainDBPath = [PATH stringByAppendingPathComponent:CLIENT_KEY];
NSString *subDirectory = [mainDBPath stringByAppendingPathComponent:GlobalShowKey];
return subDirectory;
}
and call it by
[Abc fileStructure];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.