gpt4 book ai didi

swift - 覆盖只读变量 lldb swift

转载 作者:IT王子 更新时间:2023-10-29 05:31:47 24 4
gpt4 key购买 nike

lldb 中有没有办法覆盖只读变量。

例如,如果你有一个结构

struct Object {
let name: String
}

使用 lldb 在 Xcode 的断点处执行以下操作

(lldb) expression object.name = "Tom"

将导致

error: <EXPR>:2:19: error: cannot assign to property: 'name' is a get-only property

我完全理解为什么会发生这种情况,只是想知道在调试过程中是否有一种简单的方法可以解决这个问题?

请注意这是在 Swift & NOT Objective-C 中

最佳答案

您可以使用 memory write {address} lldb 命令覆盖内存并更改字符串值。我设法一次完成一个地址,但似乎 memory write 能够一次性完成。

(lldb) help memory write
Write to the memory of the process being debugged.

Syntax: memory write <cmd-options> <address> <value> [<value> [...]]

Command Options Usage:
memory write [-f <format>] [-s <byte-size>] <address> <value> [<value> [...]]
memory write -i <filename> [-s <byte-size>] [-o <offset>] <address> <value> [<value> [...]]

-f <format> ( --format <format> )
Specify a format to be used for display.

-i <filename> ( --infile <filename> )
Write memory using the contents of a file.

-o <offset> ( --offset <offset> )
Start writing bytes from an offset within the input file.

-s <byte-size> ( --size <byte-size> )
The size in bytes to use when displaying with the selected format.

This command takes options and free-form arguments. If your arguments
resemble option specifiers (i.e., they start with a - or --), you must use
' -- ' between the end of the command options and the beginning of the
arguments.

举个例子(希望对lldb和Swift内部有更深入了解的人能提供更好的方法):

Example using memory write

这表明一次一个字节地覆盖内存。 po "Tom".dataUsingEncoding(NSUTF8StringEncoding)! 获取十六进制表示,用于逐步遍历和覆盖 object.name 的内存。我确信有一种更简单的方法可以做到这一点(在一个命令中),但我无法找出正确的参数值来实现它。

关于swift - 覆盖只读变量 lldb swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39260816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com