跃迁引擎

空気を読んだ雨降らないでよ

iOS Research & Development


Xcode lldb控制台打印 po 命令失效问题解决记录

问题背景

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
# 修复 xcode 执行 po 命令失效问题
post_integrate do |installer|
# 注意在项目里面找到对应文件的正确路径,如果这个文件路径经有问题,pod install会报错
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]

# 需要添加 swiftmodule 路径的模块列表
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
]

# 构建新的 OTHER_LDFLAGS
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 ")

# 更新 xcconfig 文件
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 弹窗,选择高亮选项即可

最近的文章

AI 工具的使用艺术之 Prompt

什么是提示词和提示词工程1.1 提示词(Prompt) 提示词,英文名叫 Prompt。 是用户发送给大语言模型的问题、指令或请求,用来明确地告诉模型用户想要解决的问题或完成的任务,是大语言模型理解用户需求并据此生成相关、准确回答或内容的基础。 对于大语言模型来说,提示词就是用户输入给大语言 …

, , 开始阅读
更早的文章

WKWebView缓存协议验证

前言本次我们基于 URLRequest.CachePolicy 协议进行 WKWebView 缓存能力的验证,主要验证方向为网页页面及相应资源的内容缓存。 API 如下 1234567891011public struct URLRequest : ReferenceConvertible, E …

, , 开始阅读
comments powered by Disqus