sqlite麻烦阅读

时间:2012-09-03 11:46:15

标签: objective-c xcode4.3

我有一个从数据库中读取数据的favDatabase:

·H

@interface favDatabase : NSObject {

UIWindow *window;
UINavigationController *navigationController;

NSString *databaseName;
NSString *databasePath;

NSMutableArray *idSPBData;
}

的.m

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // Setup some globals
    databaseName = @"usualBike.sql";

    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    // Execute the "checkAndCreateDatabase" function
    [self checkAndCreateDatabase];

    // Query the database for all favorites
    [self getFavorites];

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

现在在我最喜欢的视图中,我尝试读取数据:

favDatabase *fav = (favDatabase *)[UIApplication sharedApplication];

return fav.idSPBData.count;

但是,它崩溃了:

 2012-09-03 13:44:13.692 usualBike[1857:11603] -[UIApplication idSPBData]: unrecognized selector sent to instance 0x6e2f5a0
 2012-09-03 13:44:13.693 usualBike[1857:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIApplication idSPBData]: unrecognized selector sent to instance 0x6e2f5a0'

我正在使用Xcode 4.3,这是一个发布问题吗?

1 个答案:

答案 0 :(得分:0)

保留一个属性变量,将数据库保存在委托类中,然后调用:

favDatabase *fav = (favDatabase *)((AppDelegate*)[UIApplication sharedApplication].myFavDatabase);

假设AppDelegate是您的主要代表。

另一种方法是使您的数据库实例成为单例:

+(favDatabase *)sharedManager{
    static dispatch_once_t pred;
    static favDatabase *shared = nil;

    dispatch_once(&pred, ^{
        shared = [[favDatabase alloc] init];
    });
    return shared;
}

使用:

@interface favDatabase : UIResponder <UIApplicationDelegate>

@property (nonatomic) favDatabase  *myFavDatabase;