1

我有一个 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,这是发布问题吗?

4

1 回答 1

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;
于 2012-09-03T13:49:51.840 回答