0

It's more a general question because I'm just starting iphone programming.

in all my views I have now multiple lines just for the path to the database that is stored in the document folder:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
         NSString *docsPath = [paths objectAtIndex:0];
         NSString *path1 = [docsPath stringByAppendingPathComponent:@"database.sqlite"];
         FMDatabase *db1 = [FMDatabase databaseWithPath:path1];

What is the used way of storing that path once as a 'global' variable, accessible in the whole project? Like the web.config in .net solutions.

4

1 回答 1

0

在某个类中创建一个静态方法,该方法返回上面定义的 FMDataBase 对象。

Class Aclass;

+(FMDataBase)database{
//do your stuff here, make sure to return the database object.
}

现在可以通过以下方式访问数据库:

[Aclass database];

不要忘记#import "Aclass.h"

您还可以在 Aclass.m 中创建一个类变量并首先对其进行初始化,然后使用类方法对其进行访问。像这个例子:

Objective C 中的全局属性

抱歉格式化。

于 2013-07-24T20:30:19.500 回答