Your web-browser is very outdated, and as such, this website may not display properly. Please consider upgrading to a modern, faster and more secure browser. Click here to do so.
Source:
Implementation:
AppDelegate.h
@property (nonatomic, retain) NSString *databaseName;
@property (nonatomic, retain) NSString *databasePath;
AppDelegate.m (To check and create db if does not exist on device)
@synthesize databaseName, databasePath;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.databaseName = @”dbfile.db”;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
self.databasePath = [documentDir stringByAppendingPathComponent:self.databaseName];
[self createAndCheckDatabase];
return YES;
}
-(void) createAndCheckDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
Connecting to DB and retrieving data, remember to import the headers!
NSString *databasePath = [(CAppDelegate *)[[UIApplication sharedApplication] delegate] databasePath];
FMDatabase *db = [FMDatabase databaseWithPath: databasePath];
[db open];
FMResultSet *results = [db executeQuery:@”SELECT * FROM profile”];
while([results next]) {
Profile *p = [[Profile alloc] init];
p.pID = (int) [results stringForColumn:@”id”];
p.originalName = [results stringForColumn:@”originalName”];
p.changedName = [results stringForColumn:@”changedName”];
[profileList addObject:p];
}
[db close];
Can only use NSArray and not NSMutableArray.
If you have an NSMutableArray and want to display the data in it, just convert it to a NSArray using this.
profileArrayList = (NSArray *) profileMutableList;
If added only an array, the plist will create Item0, Item1 …
With some coding, it’ll save in a format you desire.
Get Path of Plist
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@”CurrencyISO.plist”];
Setting Up the Array to store the Dic
[dataArray addObject: [[NSMutableDictionary alloc]
initWithObjectsAndKeys:
[countryISO objectAtIndex:i], @”Prefix”,
@”0”, @”Index”,
nil]];
Creating a Dictionary of your choice
[countryDic setValue: [dataArray objectAtIndex:i] forKey:mainKey];
Writing to the PLIST!
[countryDic writeToFile:plistPath atomically:YES];
Note: All these are inside a for loop.