1

I have a string value I set with a -void method I need to retrieve that string in a class method +

I can't change + to - method so I need to figure out another way to retrieve that string.

@synthesize folderPathName=_folderPathName;

-(void)loadFolderPathName:(NSString *)folder{

    _folderPathName=[[NSString alloc] initWithString:folder];

}

+(NSString *)getDocumentsDirectory
{

   // NSString *pathwithFoldername=[NSString stringWithFormat:@"Documents/%@",_folderPathName];
    NSString *documentsDirectory = [NSHomeDirectory() 
                                    stringByAppendingPathComponent:pathwithFoldername];
    return documentsDirectory;
}

I have tried something like

-(NSString *) getFolderPathName{

    return _folderPathName;
}

but neither this

NSString *pathwithFoldername=[FileUtils getFolderPathName];

or this allowed function calls

NSString *pathwithFoldername=[self getFolderPathName];

How can I get that string and use it in a + method?

Mord Fustang
  • 1,523
  • 4
  • 40
  • 71

2 Answers2

2

You need to declare a static NSString variable in the .m file.

static NSString *Path;

Then add a method like:

+(NSString *)valueOfPath
{
  return Path;
}

You need to set this variable where you set the folderPathName.

You can call this method by:

NSString *pathwithFoldername=[FileUtils valueOfPath];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
2

You need to understand the fundamentals of Object Orientation to understand how this might not work as you expect, and why it shouldn't be done like this.

+ (void)... creates a class method, there can only be one of these throughout your application. This means if I call a class method throughout my application, they will all go to the same location.

- (void)... creates an instance method. There can be many instances throughout your application, and when you call an instance method, it will only go to one of your instances (The others won't be affected)

This includes calling an instance method when you have class methods available also; their memory locations are separate from each other. This means if you set some data via an instance method, that data won't be available to a class method. (This can work the other way around, if you set some data via a class method, an instance can get that data by referring to its class).

Now, after all that, the point I'm trying to get across is setting data via a class method isn't very object orientated. Personally, I use class methods as helpers on instances, and instances contain all the data I need. If you need to ensure you only have one instance throughout your application, use the Singleton pattern (See how here).

Now, in your situation, it means you can do:

+(id)sharedInstance
{
    static dispatch_once_t pred;
    static MyClass *sharedInstance = nil;
    dispatch_once(&pred, ^{
        sharedInstance = [[MyClass alloc] init];
    });
    return sharedInstance;
}

-(void)loadFolderPathName:(NSString *)folder{

    _folderPathName=[[NSString alloc] initWithString:folder];

}

-(NSString *)getDocumentsDirectory
{
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *pathwithFoldername = [documentsDirectory stringByAppendingPathComponent:_folderPathName];

    return pathwithFoldername;
}

And access your single instance via:

[MyClass sharedInstance]
Community
  • 1
  • 1
WDUK
  • 18,870
  • 3
  • 64
  • 72
  • thank you for detail explanation, If I had to use a value in different classes I would go with Singleton Design Pattern but in my case I find it too much work to do :) – Mord Fustang Nov 07 '12 at 17:28
  • Good info. One serious issue though - the code for getting the Documents directory is incorrect. It should be `NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];`. Then append whatever path you need to that. – rmaddy Nov 07 '12 at 17:33
  • I'll update that, thanks for the heads up! The focus of the answer was on OO, and I used the code they gave in the question to show it applied. – WDUK Nov 07 '12 at 17:36
  • @MordFustang "If I had to use a value in different classes I would go with Singleton Design Pattern but in my case I find it too much work to do". I'm sorry, but in fairness, that's not a good excuse for bad design. You may need to maintain that code later on, and it'll cause more hassle than good if you do it the 'quick and dirty' way. This isn't just for your case, this is a good principle to follow in programming as a whole. – WDUK Nov 07 '12 at 17:43
  • @rmaddy why it is `lastobject` ? Shouldn't it be `objectatindex:0` ? – Mord Fustang Nov 07 '12 at 17:58
  • @MordFustang It's the same since there will only be one value in the array. – rmaddy Nov 07 '12 at 18:07
  • "There can be many instances throughout your application" There is always just one copy of the code of any function or method. – newacct Nov 07 '12 at 19:58
  • Well yeah, naturally, but when using the class, the many instances refers to separate memory areas when run. – WDUK Nov 07 '12 at 21:01