Quote:
Originally Posted by eddietr
PM is private message.
NVM, seems you dont' have private messages enabled?
In any case, maybe you'll figure it out tomorrow or you can post more of the project. I don't see anything (yet) from what you've posted.
|
Obviously new to the forum. Can't seem to do anything on my CP. So I'll just paste into here. I've stripped out the irrelevant code so it's clearer and smaller. What's left should work on its own. The POI class is just a data structure, could be anything - a couple of strings in this example. I'm a Java programmer if you can't tell and this .m, .h stuff and the property and synthesizing is driving me nuts. OK, here's the code. Hope it makes sense.
OtherClass.h
// nothing in here related to this problem
OtherClass.m
#import "OtherClass.h"
#import "MyAppDelegateAppDelegate.h"
#import "POI.h"
@class MyAppDelegateAppDelegate;
@implementation OtherClass
- (void) viewDidLoad
{
[super viewDidLoad];
MyAppDelegateAppDelegate *appDelegate = (MyAppDelegateAppDelegate *)[[UIApplication sharedApplication] delegate];
// breakpoint here, appDelegate appears to be uninitialized (the pois array has 0 elements)
POI *poi = [appDelegate.pois objectAtIndex:0];
}
MyAppDelegate.h
#import <UIKit/UIKit.h>
@class OtherClass;
@interface MyAppDelegateAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
{
UIWindow *window;
UITabBarController *tabBarController;
NSFileManager *FileManager;
NSString *databaseName;
NSString *databasePath;
NSMutableArray *pois;
}
-(void) readPOIsFromDatabase;
-(void) checkAndCreateDatabase;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) NSMutableArray *pois;
@end
MyAppDelegate.m
#import "MyAppDelegateAppDelegate.h"
#import "sqlite3.h"
#import "POI.h"
@implementation MyAppDelegateAppDelegate
@synthesize window;
@synthesize tabBarController;
@synthesize pois;
- (void) applicationDidFinishLaunching

UIApplication *)application
{
databaseName = @"MyAppDelegate.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
[self readPOIsFromDatabase];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
-(void) readPOIsFromDatabase
{
sqlite3 *database;
pois = [[NSMutableArray alloc] init];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sqlStatement = "select name, type from pois";
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while (sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSString *aName = [NSString stringWithUTF8String

char *)sqlite3_column_text(compiledStatement, 0)];
NSString *aType = [NSString stringWithUTF8String

char *)sqlite3_column_text(compiledStatement, 1)];
POI *poi = [[POI alloc] initWithName:aName tp:aType];
[pois addObject

oi];
[poi release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
-(void) checkAndCreateDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if (success) return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
[fileManager release];
}
- (void) dealloc
{
[tabBarController release];
[window release];
[super dealloc];
}
@end