Retaining NSString value to be used in other methods
In the .h i've declared:
// in database.h
@interface database : UIViewController {
NSString* databaseName;
NSString* databasePath;
}
On execution, methodA is called first , which I get the databasePath to point to my database.db file:
//methodA: in database.m
- (void)methodA {
databaseName = @"database.db";
NSArray* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDir = [documentsPath objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
NSFileManager* filemagar = [NSFileManager defaultManager];
NSString* databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[filemagar copyItemAtPath:开发者_JS百科databasePathFromApp toPath:databasePath error:nil];
NSLog (@"databasePath is...%@", databasePath);
[filemagar release];
}
The databasePath will return a string of folder path name "/Users/....". So this is what I need.
Now subsequently, when I call method B...:
//methodB: in database.m
- (void)methodB {
NSLog (@"databasePath is now...%@", databasePath);
}
The program terminates as databasePath that is pointing to previous method strings, is no longer the same.
How do I retain the databasePath info , so that I can use in the other methods later?
This line:
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
Should be:
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] copy];
Another way to do this would be to use properties:
@interface database : UIViewController {
NSString* databaseName;
NSString* databasePath;
}
@property (nonatomic, copy) NSString *databaseName;
@property (nonatomic, copy) NSString* databasePath;
Then in your @implementation
:
@synthesize databaseName, databasePath;
- (void) dealloc {
[self setDatabaseName:nil];
[self setDatabasePath:nil];
[super dealloc];
}
//rest of your implementation...
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] retain];
Or better, learn about properties.
You can do this in two ways:
First, you can retain yourself the string:
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] retain];
Cocoa Touch uses reference count for memory management, you can read about it here.
Second, you can make properties. In your .h file:
@property(nonatomic, copy) NSString *databasePath
And use it like this when you set it:
[self setDatabasePath:stringByAppendingPathComponent:databaseName];
You can read about properties here.
databasePath
is nil
because it is being assigned to an autorelease
object, which is [documentsDir stringByAppendingPathComponent:databaseName]
. That is why databasePath
is not available outside the method.
As other answers have given, you can either retain
it or copy
it in the databasePath
variable so that it is available until you release
it.
The issue is that you're not retaining (or copying) the string reference. Its current scope is limited to that of the method. Once the method exits, the reference to the string is lost thanks to the autorelease nature of the string returned by the stringByAppendingPathComponent:
method.
The easiest approach here would probably be to declare databasePath
as a (nonatomic, copy) @property
. This way, when you assign the value of the database path to the variable, it is automatically copied for you.
@property(nonatomic, copy) NSString *databasePath;
Then update your code to use the dot syntax.
self.databasePath = ...;
Don't forget to release
databasePath in your dealloc
method.
精彩评论