问题背景
iOS开发中,使用Xcode
开发时,有时候原本运行好好的项目,突然调试时,发现 po 命令无法正常显示变量的值,无论是清空编译目录,还是重装Xcode,都无法解决问题。
问题原因
自从 Xcode 13.3 到 Xcode 14 测试版之前,当尝试使用静态框架/库与 LLDB 或使用 CocoaPods 并启用框架和静态链接时,会出现一个奇怪的 bug。
当你在框架代码中使用简单的 LLDB 命令(如 po )时,调试控制台会显示以下消息:
1 2
| error: expression failed to parse: error: Couldn't realize type of self.
|
这个错误消息表明在使用静态框架或库时,LLDB 无法正确地解析和显示对象的内容。这是由于 Xcode 在这一版本范围内存在一个已知的问题。
因为我们的项目是通过Pod集成依赖的,所以按照解决方案中针对pod的解决方式就行。
参考方案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| post_integrate do |installer| xcconfig_path = installer.sandbox.target_support_files_root.to_s + '/Pods-App/Pods-App.debug.xcconfig'
xcconfig_content = File.read xcconfig_path xcconfig_original_ld_flags = xcconfig_content.match(/OTHER_LDFLAGS = ([^\n]+)\n/)[1]
xcconfig_new_ld_flags = <<~CONTENT
// Xcode 13.3+ OTHER_LDFLAGS = #{xcconfig_original_ld_flags} -Wl,-add_ast_path,$(TARGET_BUILD_DIR)/InternalPod/InternalPod.framework/Modules/InternalPod.swiftmodule/$(NATIVE_ARCH_ACTUAL)-apple-$(SHALLOW_BUNDLE_TRIPLE).swiftmodule
// Xcode 13.2 OTHER_LDFLAGS[sdk=iphoneos15.2] = #{xcconfig_original_ld_flags} -Wl,-add_ast_path,$(TARGET_BUILD_DIR)/InternalPod/InternalPod.framework/Modules/InternalPod.swiftmodule/$(NATIVE_ARCH_ACTUAL)-apple-ios.swiftmodule OTHER_LDFLAGS[sdk=iphonesimulator15.2] = #{xcconfig_original_ld_flags} -Wl,-add_ast_path,$(TARGET_BUILD_DIR)/InternalPod/InternalPod.framework/Modules/InternalPod.swiftmodule/$(NATIVE_ARCH_ACTUAL)-apple-simulator.swiftmodule
CONTENT
xcconfig_content.gsub! /OTHER_LDFLAGS = ([^\n]+)\n/, xcconfig_new_ld_flags
File.open(xcconfig_path, 'w') do |f| f.puts xcconfig_content end
end
|
Pods-App
这个 Pods-App 指的是 target 的名称,我们直接去工程里找到 Pod 目录,找到 Targets Support Files 文件夹,打开我们想要进行调试的 target 目录,比如 Pods-DataTrack-Debug,这个 Pods-DataTrack-Debug 就是 上面的 Pods-App,但我们要填入的是 .xcconfig 文件的路径,它又去哪里了呢?
右键这个 Pods-DataTrack-Debug.debug 文件,选择 Show in finder,可以看到,它就是我们要的 .xcconfig 文件,我们将它的地址填入到刚才的路径里就可以了。
解决方案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| post_integrate do |installer| xcconfig_path = installer.sandbox.target_support_files_root.to_s + '/Pods-DataTrack-Debug/Pods-DataTrack-Debug.debug.xcconfig' xcconfig_content = File.read xcconfig_path xcconfig_original_ld_flags = xcconfig_content.match(/OTHER_LDFLAGS = ([^\n]+)\n/)[1] modules = %w[ YCPlatformManagers YCPlatformVideo YCPlatformVideoBusiness YCPlatformVideoPlugins YCPlatformDataTrack YCPlatformDataShared YCPlatformPreferenceManager YCPlatformDownload YCPlatformDownloadCache YCPlatformPush YCPlatformNetwork YCPlatformIAPManager YCPlatformTools YCPlatformShare YCPlatformBusinessInterfaces YCPlatformLog YCPlatformLogPlugin YCPlatformVersionCheck YCPlatformLogin YCPlatformDiagnosis YCPlatformRouter YCPlatformScreen YCPlatformBrowser YCPlatformUIKits YCPlatformBusinessRouter YCPlatformStatistics ] module_flags = modules.map do |module_name| "-Wl,-add_ast_path,$(TARGET_BUILD_DIR)/#{module_name}/#{module_name}.framework/Modules/#{module_name}.swiftmodule/$(NATIVE_ARCH_ACTUAL)-apple-$(SHALLOW_BUNDLE_TRIPLE).swiftmodule" end.join(" \\\n ") new_content = <<~CONTENT # Xcode 13.3+ # 添加所有需要 debug 的 Swift 模块路径 OTHER_LDFLAGS = #{xcconfig_original_ld_flags} \\ #{module_flags} CONTENT xcconfig_content.gsub!(/OTHER_LDFLAGS = ([^\n]+)(\n|$)/, new_content) File.write(xcconfig_path, xcconfig_content) puts "\e[38;5;141mRunning post_integrate for DataTrack-Debug target\e[0m" end
|
执行 pod install,当出现紫色的提示表示执行成功,如果弹出 xcode 弹窗,选择高亮选项即可