无法在 xcode6 beta 6 osx swift 项目中打印对象(‘ po’) : (自动导入错误: 无法从 AST 上下文获取模块‘__ ObjecC’)

试图在 xcode 6 beta 6 OSX Swift 项目中打印对象(po命令)会导致以下错误消息:

(lldb) po managedObject
error: Error in auto-import:
failed to get module '__ObjC' from AST context

在这种情况下,有问题的对象是 NSManagedObject的一个实例。

关于如何帮助自动导入将 __ObjC module导入 LLVM 抽象语法树上下文,有什么建议吗?

9470 次浏览

As of Xcode 6.1 if you attempt the po command twice it will work on the second attempt. The first po command will always fail for each new debugging session but subsequent calls work.

I'd recommend double-checking that you have the

-D DEBUG

flag set under 'Other Swift Flags' for the scheme that you're using to debug. I experienced similar issues when I'd accidentally deleted it.

Have same issue in xcode 7.3.1:

error: Error in auto-import:
failed to get module 'Touch' from AST context:
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/libxml2/libxml/tree.h"
^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/libxml2/libxml/tree.h:17:10: error: 'libxml/xmlversion.h' file not found
#include <libxml/xmlversion.h>
^
could not build Objective-C module 'LibXML2'

But you can use fr v managedObject instead of po managedObject.

A clean of my project and deleting DerivedData worked for me.

I discussed a similar error message with an Apple engineer at WWDC2017. It seems like this issue can have many causes, and I am aware that mine is slightly different than the one described above.

My team spent weeks trying to figure this out, and it ended up being a bug on Apple's compiler, which we could never have figured out by ourselves. Also, it has a VERY easy workaround.

So, this is just me posting the fix here, in order to maximize the probability that someone else does a search for this confusing error message, and finds this answer.

So, here it is. In our case, we had an Objective-C project using a mix of Swift and Objective-C frameworks. This fix might apply in slightly different contexts, just try it.

There happens to be a bug with the way the compiling flags get aggregated from the frameworks and the project, and the "pure Objective-C" project "activates" it.

Solution: add one single, empty Swift file ("Whatever.swift", or whatever) in your Objective-C project, making it not-pure-objective-c any more (new->file->Swift file, don't create the bridging header. The file will only contain the import of Foundation).