I have an application that uses a few classes found only in 10.7 Lion. (For example, NSFileCoordinator.)
When my application launches, it can use NSFileCoordinator to coordinate read access to the data store. (I save to XML.)
The application launches fine under 10.7 Lion, but when launching under 10.6 Snow Leopard, the application crashes with the error shown below.
My base SDK is set to 10.7, and the deployment target is 10.6. As far as I can tell, all my code is properly wrapped, like:
if (NSClassFromString(@"NSFileCoordinator") != nil) {
// Do something the 10.7 way
} else {
// Do something the 10.6 way
}
But for some reason, I'm still seeing the crash. Since the Base SDK is 10.7, I can't run the application under Xcode in 10.6, making debugging difficult. Additional messages are being logged to the console:
9/1/11 9:48:03 AM [0x0-0x18018].com.me.myapp[173] dyld: Symbol not found: _OBJC_CLASS_$_NSFileCoordinator
9/1/11 9:48:03 AM [0x0-0x18018].com.me.myapp[173] Referenced from: /Volumes/Macintosh HD/Users/Me/Library/Developer/Xcode/DerivedData/Mac-ebdxgdvcqfnptlftkreamrwdxetd/Build/Products/Debug/myapp.app/Contents/MacOS/myapp
9/1/11 9:48:03 AM [0x0-0x18018].com.me.myapp[173] Expected in: /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
9/1/11 9:48:03 AM [0x0-0x18018].com.me.myapp[173] in /Volumes/Macintosh HD/Users/Me/Library/Developer/Xcode/DerivedData/Mac-ebdxgdvcqfnptlftkreamrwdxetd/Build/Products/Debug/myapp.app/Contents/MacOS/myapp
And the crash report:
Process: myapp [137]
Path: /Volumes/Macintosh HD/Users/Me/Library/Developer/Xcode/DerivedData/Mac-ebdxgdvcqfnptlftkreamrwdxetd/Build/Products/Debug/myapp.app/Contents/MacOS/myapp
Identifier: com.me.myapp
Version: 1.5.0 (150)
Code Type: X86-64 (Native)
Parent Process: launchd [92]
Date/Time: 2011-09-01 09:40:32.591 -0400
OS Version: Mac OS X 10.6.8 (10K549)
Report Version: 6
Inte开发者_运维问答rval Since Last Report: 2009 sec
Crashes Since Last Report: 5
Per-App Crashes Since Last Report: 5
Anonymous UUID: 32784C79-0821-471E-BCBF-BE5874774075
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000
Crashed Thread: 0
Dyld Error Message:
Symbol not found: _OBJC_CLASS_$_NSFileCoordinator
Referenced from: /Volumes/Macintosh HD/Users/Me/Library/Developer/Xcode/DerivedData/Mac-ebdxgdvcqfnptlftkreamrwdxetd/Build/Products/Debug/myapp.app/Contents/MacOS/myapp
Expected in: /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
in /Volumes/Macintosh HD/Users/Me/Library/Developer/Xcode/DerivedData/Mac-ebdxgdvcqfnptlftkreamrwdxetd/Build/Products/Debug/myapp.app/Contents/MacOS/myapp
@Anomie has the right answer but let me expand on it some more. Here is the code of a command line tool that is similar to what you are doing:
int main (int argc, const char * argv[])
{
@autoreleasepool {
if (NSClassFromString(@"NSFileCoordinator")) {
NSLog(@"Version >= 10.7");
} else {
NSLog(@"Version < 10.7");
}
}
return 0;
}
The only framework that this program links to is Foundation. But in order to weakly link to this framework you must do the following:
- Click on the project in the project browser
- Go to "Build Phases"
- Go to "Link Binary With Libraries" and remove "Foundation.framework"
Go to "Build Settings" and add the following "Other Linker Flags":
-weak_framework Foundation
Rebuild your project. I've tested on OS X 10.6 and 10.7 and neither crashed and both produced the expected output. Hope this helps.
You need to weakly link the framework, so these unresolved symbols won't be an error at load time.
精彩评论