Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C:需要从FinishedLaunching调用另一个类中的方法_Objective C - Fatal编程技术网

Objective-C:需要从FinishedLaunching调用另一个类中的方法

Objective-C:需要从FinishedLaunching调用另一个类中的方法,objective-c,Objective C,我走了这么远。。。这是我的密码: - (void)applicationDidFinishLaunching:(UIApplication *)application { // create the d/b or get the connection value SQLiteDB *dbInstance = [[SQLiteDB alloc] init]; } 现在,问题是:这段代码应该检查数据库是否存在,如果不存在,就创建它。我的问题是,我在弄清楚如何准确地编写被调用方法的

我走了这么远。。。这是我的密码:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    //  create the d/b or get the connection value
    SQLiteDB *dbInstance = [[SQLiteDB alloc] init];
}
现在,问题是:这段代码应该检查数据库是否存在,如果不存在,就创建它。我的问题是,我在弄清楚如何准确地编写被调用方法的第一行以及如何将其放置在SQLiteDB.m中时遇到了问题。这是实例方法还是类方法+


我很抱歉在这件事上做得这么差劲,但一旦我看到它,我就会掌握它的窍门。。。其余的代码是用C编写的,我可以处理到Obj_C的转换。

我假设通过查看数据库是否存在,您的意思是查看数据库文件是否存在于磁盘上。为此,可以使用NSFileManager类的fileExistsAtPath:方法。这是一个实例方法,但您可以使用[NSFileManager defaultIntance]


首先计算文件的路径,如何计算取决于您。检查文件是否存在。如果是,请打开该文件;如果不是,请使用该文件名创建一个新数据库。

我假设,通过查看数据库是否存在,您的意思是查看数据库文件是否存在于磁盘上。为此,可以使用NSFileManager类的fileExistsAtPath:方法。这是一个实例方法,但您可以使用[NSFileManager defaultIntance]


首先计算文件的路径,如何计算取决于您。检查文件是否存在。如果是,请打开该文件;如果不是,请使用该文件名创建一个新数据库。

以下是一种将现有数据库从捆绑包复制到文档目录的方法,但可以轻松地适应新数据库。只需使用下面的fileExistsAtPath:方法逻辑,并用自定义数据库创建代码替换要执行的操作

将其放入AppDelegate.m文件:

- (void)prepareDatabase
{
    //add Database Versioning check to see if the resources database is newer
    // generally as simple as naming your database with a version on the end

    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/YOURDATABASE.s3db"];
    if(![filemanager fileExistsAtPath:databasePath]) {
        //Database doesn't exist yet, so we copy it from our resources
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/YOURDATABASE.s3db"];
        if([filemanager copyItemAtPath:defaultDBPath toPath:databasePath error:nil]) {
            NSLog(@"Database Copied from resources");           
        } else {
            NSLog(@"Database copy FAILED from %@ to %@",defaultDBPath,databasePath);
        }
    }
}
然后在您的ApplicationIDFinishLaunching:方法中调用以下命令:

[self prepareDatabase];

以下是一种将现有数据库从捆绑包复制到Documents目录的方法,但可以轻松地适应新数据库。只需使用下面的fileExistsAtPath:方法逻辑,并用自定义数据库创建代码替换要执行的操作

将其放入AppDelegate.m文件:

- (void)prepareDatabase
{
    //add Database Versioning check to see if the resources database is newer
    // generally as simple as naming your database with a version on the end

    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/YOURDATABASE.s3db"];
    if(![filemanager fileExistsAtPath:databasePath]) {
        //Database doesn't exist yet, so we copy it from our resources
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/YOURDATABASE.s3db"];
        if([filemanager copyItemAtPath:defaultDBPath toPath:databasePath error:nil]) {
            NSLog(@"Database Copied from resources");           
        } else {
            NSLog(@"Database copy FAILED from %@ to %@",defaultDBPath,databasePath);
        }
    }
}
然后在您的ApplicationIDFinishLaunching:方法中调用以下命令:

[self prepareDatabase];

在IOS和Mac OS X V 10.5之后,您应该考虑使用[[NSFLimeManger-OLLC] init ],而不是SuntLon方法Debug管理器。当使用[[NSFileManager alloc]init]创建NSFileManager实例时,将其视为线程安全的。即使是线程安全的文件管理器也不会在检查/如果未找到,则创建方案中从竞争条件中保存。谢谢。。我已经有了逻辑,但仍然不知道这个方法是什么样的。。。我想调用这个方法,但不知道它是上面代码中的实例方法还是类方法。我认为我的调用是正确的,但是不知道目标代码是什么样的,我只需要第一行,即IOID inITQualDeDB……在iOS和Mac OS X V 10.5中,稍后你应该考虑使用[[NSfMeMaNanger-OLLC] init ],而不是SuntLon方法Debug管理器。当使用[[NSFileManager alloc]init]创建NSFileManager实例时,将其视为线程安全的。即使是线程安全的文件管理器也不会在检查/如果未找到,则创建方案中从竞争条件中保存。谢谢。。我已经有了逻辑,但仍然不知道这个方法是什么样的。。。我想调用这个方法,但不知道它是上面代码中的实例方法还是类方法。我想我的调用是正确的,但不知道目标代码看起来像什么,我只需要第一行,即-id initSQLiteDB…chriskent:这正是我要找的信息。。。非常感谢你!谢谢其他人的回复。克里斯:这正是我要找的信息。。。非常感谢你!感谢其他回复的人。