Essentially all relevant code listed below. Not much.
I wrote a class for an old project to handle accessing the "Application Support" folder; that old project had all kinds of busy-work it had to do there.
In my new project, I also want to access that folder, but it's vastly simpler-- I only need to write files there, to separate sub-folders. So I copied the source files and started whittling down until this point, where there's almost nothing left.
What is left is (at least) three lazy initializing properties. For the subfolders, the code for supportSubFolder01 and supportSubFolder02 is close to identical-- the only differences being 1) the name of the subfolder, which could be passed as a parameter, perhaps, or even an optional parameter, and 2) the name of the underlying property.
My gut tells me that a property must have a unique address, and yet... ObjC can sometimes do such queer and magic things that I can't allow myself to discount the possibility that... somehow...
It might be asking a lot, but might there be some way to have a function/whatever which can be used to create an arbitrary number of lazy properties, each of which is distinguishable just by a parameter string ( or other way of specifying string ) ?
Ermp... is it clear? Thx.
I wrote a class for an old project to handle accessing the "Application Support" folder; that old project had all kinds of busy-work it had to do there.
In my new project, I also want to access that folder, but it's vastly simpler-- I only need to write files there, to separate sub-folders. So I copied the source files and started whittling down until this point, where there's almost nothing left.
What is left is (at least) three lazy initializing properties. For the subfolders, the code for supportSubFolder01 and supportSubFolder02 is close to identical-- the only differences being 1) the name of the subfolder, which could be passed as a parameter, perhaps, or even an optional parameter, and 2) the name of the underlying property.
My gut tells me that a property must have a unique address, and yet... ObjC can sometimes do such queer and magic things that I can't allow myself to discount the possibility that... somehow...
It might be asking a lot, but might there be some way to have a function/whatever which can be used to create an arbitrary number of lazy properties, each of which is distinguishable just by a parameter string ( or other way of specifying string ) ?
Ermp... is it clear? Thx.
Code:
@interface AppSupportFolder : NSObject {
@private
// Guaranteed path props
NSString* _supportFolder;
NSString* _supportSubFolder01;
NSString* _supportSubFolder02;
}
// Guaranteed path props
@property ( readonly , nonatomic , copy ) NSString* supportFolder;
@property ( readonly , nonatomic , copy ) NSString* supportSubFolder01;
@property ( readonly , nonatomic , copy ) NSString* supportSubFolder02;
@end
///
///////////////////////////// h above, m below
///
@implementation AppSupportFolder
// Guaranteed path props
//
@synthesize supportFolder = _supportFolder;
@synthesize supportSubFolder01 = _supportSubFolder01;
@synthesize supportSubFolder02 = _supportSubFolder02;
// MARK: guaranteed path props
//
// These props not only promise the string path value, they create the underlying finder item when needed.
//
- (NSString*) supportFolder {
if ( _supportFolder )
return _supportFolder;
NSArray* pathsArray = NSSearchPathForDirectoriesInDomains( NSApplicationSupportDirectory, NSUserDomainMask, true );
XXErr_logIf( ! [pathsArray count] );
if ( ! [pathsArray count] )
return 0;
return _supportFolder = [[pathsArray objectAtIndex:0] retain];
}
- (NSString*) supportSubFolder01 {
if ( _supportSubFolder01 )
return _supportSubFolder01;
if ( ! self.supportFolder )
return 0;
_supportSubFolder01 = [[NSString stringWithFormat:@"%@/%@" , self.supportFolder , @"solved"] retain];
if ( [[NSFileManager defaultManager] fileExistsAtPath:_supportSubFolder01] )
return _supportSubFolder01;
XXErr_messageWithFmtStr( @"supportSubFolder01: creation failure" );
RELEASE_AND_ZERO( _supportSubFolder01 );
return 0;
}
- (NSString*) supportSubFolder02 {
if ( _supportSubFolder02 )
return _supportSubFolder02;
if ( ! self.supportFolder )
return 0;
_supportSubFolder02 = [[NSString stringWithFormat:@"%@/%@" , self.supportSubFolder02 , @"aborted"] retain];
if ( [[NSFileManager defaultManager] fileExistsAtPath:_supportSubFolder02] )
return _supportSubFolder02;
XXErr_messageWithFmtStr( @"supportSubFolder02: creation failure" );
RELEASE_AND_ZERO( _supportSubFolder02 );
return 0;
}