2011-02-03 33 views
0

我开始学习Objective-C并帮助我这样做,我正在使用“Head First iPhone Development”。现在我正在学习SQLite数据库,并让它们起作用我听说SQLite文件需要位于应用程序的Documents文件夹中,因此我必须移动该文件。将SQLite从一个地方移动到另一个地方:应用程序崩溃和不兼容类型

我正在使用书中的例子,但似乎无法使其工作。每次我编译它,我的应用程序崩溃。我有以下警告:“不兼容的Objective-C类型初始化 '结构NSURL *',预期 '结构的NSString *'

有没有人有一个提示如何解决这一问题

编辑:

问题似乎是在applicationDocumentsDirectory返回一个NSURL的这两行中,但我告诉它返回一个NSString。我可以告诉它返回一个NSURL,但是在使用stringByAppendingPathComponent的下一行时会出现问题。方法来解决这个问题?

NSString *documentsDirectory = [self applicationDocumentsDirectory]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"iBountyHunter.sqlite"]; 

这就是调试器控制台,当应用程序崩溃输出:

2011-02-04 07:33:42.126 iBountyHunter[591:207] -[NSURL stringByAppendingPathComponent:]: unrecognized selector sent to instance 0x6043f80 
2011-02-04 07:33:42.128 iBountyHunter[591:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL stringByAppendingPathComponent:]: unrecognized selector sent to instance 0x6043f80' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00f87be9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x010dc5c2 objc_exception_throw + 47 
    2 CoreFoundation      0x00f896fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x00ef9366 ___forwarding___ + 966 
    4 CoreFoundation      0x00ef8f22 _CF_forwarding_prep_0 + 50 
    5 iBountyHunter      0x00001d8e -[iBountyHunterAppDelegate createEditableCopyOfDatabaseIfNeeded] + 107 
    6 iBountyHunter      0x00001f24 -[iBountyHunterAppDelegate application:didFinishLaunchingWithOptions:] + 37 
    7 UIKit        0x002ba1fa -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163 
    8 UIKit        0x002bc55e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439 
    9 UIKit        0x002c6db2 -[UIApplication handleEvent:withNewEvent:] + 1533 
    10 UIKit        0x002bf202 -[UIApplication sendEvent:] + 71 
    11 UIKit        0x002c4732 _UIApplicationHandleEvent + 7576 
    12 GraphicsServices     0x018bda36 PurpleEventCallback + 1550 
    13 CoreFoundation      0x00f69064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
    14 CoreFoundation      0x00ec96f7 __CFRunLoopDoSource1 + 215 
    15 CoreFoundation      0x00ec6983 __CFRunLoopRun + 979 
    16 CoreFoundation      0x00ec6240 CFRunLoopRunSpecific + 208 
    17 CoreFoundation      0x00ec6161 CFRunLoopRunInMode + 97 
    18 UIKit        0x002bbfa8 -[UIApplication _run] + 636 
    19 UIKit        0x002c842e UIApplicationMain + 1160 
    20 iBountyHunter      0x00001cf8 main + 102 
    21 iBountyHunter      0x00001c89 start + 53 
) 
terminate called after throwing an instance of 'NSException' 

回答

0

这将做到这一点。

// Check the existence of database 
     NSFileManager *mngr = [[NSFileManager alloc] init]; 

     // If the database doesn't exist in our Document folder we copy it to Documents (this will be executed only the first time we launch the app). 
     if (![mngr fileExistsAtPath:[NSString stringWithFormat:@"%@/Documents/db.sqlite", NSHomeDirectory()]]){ 
      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"db.sqlite" ofType:nil]; 
      [mngr copyItemAtPath:filePath toPath:[NSString stringWithFormat:@"%@/Documents/db.sqlite", NSHomeDirectory()] error:NULL]; 
     } 
     [mngr release]; 

这里的applicationDocumentsDirectory方法的另一个版本:

+ (NSString*)applicationDocumentsDirectory { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return documentsDirectory; 
} 
1

我使用从书 的例子,但我似乎无法得到它的工作。 每次我编译它我的应用程序崩溃。 我有以下警告: “不兼容的Objective-C类型 初始化‘结构NSURL *’, 预期‘结构的NSString *’

修复警告;你的东西,期望一个分配一个NSURL实例NSString的实例。

或发布警告&线,导致它的代码。

如果您的应用程序崩溃,后坠毁。


的问题似乎是在这两个 线, applicationDocumentsDirectory 返回一个NSURL,但我告诉它 返回一个NSString。我可以告诉它 返回一个NSURL,但是这给我一个 问题,我在下一行使用 stringByAppendingPathComponent。有 有办法解决这个问题吗?

你不能告诉一个方法返回一个NSURL来返回一个NSString。无论是铸造型的返回值,也不象下面的分配将工作:

NSString *documentsDirectory = [self applicationDocumentsDirectory]; 

编译器在该行警告,因为它是坏了;你不能像NSString那样对待NSURL。

像下面的东西应该工作:

NSString *documentsDirectory = [[self applicationDocumentsDirectory] path]; 
+0

当然是的。 NSURL和NSString不可互换。编辑您的问题以显示编译错误的位置。 – bbum 2011-02-04 06:30:34

+0

我已经更新了问题以显示调试器控制台中的输出。 – simonbs 2011-02-04 06:36:59

0

我有以下警告:“不兼容的Objective-C类型初始化 '结构NSURL *',预期 '结构的NSString *'

问题是,你正在调用苹果提供的applicationDocumentsDirectory方法,它返回一个NSURL。

+0

是否有可能使它返回一个NSString或使用带NSURL的stringByAppendingPathComponent? – simonbs 2011-02-04 06:31:50

1

如果应用程序仍然在你的模拟器doen't工作,千万记得要删除的应用程序从你的模拟器,然后生成并运行应用程序再次。

您可以像使用真实设备一样从模拟器中删除应用程序。

相关问题