2016-09-01 4 views
11

읽기 전용 변수를 덮어 쓸 수있는 방법이 있습니까? 당신이이 lldb와 엑스 코드에 중단 점에서에서 다음을 수행 구조체읽기 전용 변수 오버라이드

struct Object { 
    let name: String 
} 

이 있다면

예를 들어

(lldb) expression object.name = "Tom" 

내가 완전히 이유를 이해

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

가 발생합니다 디버깅 중에이 문제를 해결할 수있는 쉬운 방법이 있는지 알고 싶습니다.

이 스위프트 &에 유의하시기 바랍니다 하지 당신은 메모리를 덮어 문자열 값을 변경 memory write {address} lldb 명령을 사용할 수 있습니다

+0

왜 읽기 전용 변수를 덮어 쓸 필요가 있습니까? 'Object'의 이름을 변경할 수 없다는 것을 알고 있기 때문에, 새로운'Object'를 초기화하지 않고 이름을 바꾸면 어떨까요? –

+1

@Joe 디버깅하고 lldb를 사용할 때 런타임에 하나의 변수를 변경하여 다른 동작을 테스트하는 것이 유용하기 때문에 종종 유용합니다. 종속성 주입이 필요한 복잡한 객체가있는 경우 간단히 새 객체를 할당하는 것이 콘솔에서 더 복잡해집니다. – sbarow

+0

그래서 let을 var로 변경하지 않으시겠습니까? – WMios

답변

4

목표 - C. 한 번에 하나의 주소 만 처리 할 수 ​​있었지만, 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 이해와 스위프트의 내부와 희망 사람)이다 :

Example using memory write

이 한 번에 메모리를 한 바이트를 덮어 도시 . po "Tom".dataUsingEncoding(NSUTF8StringEncoding)!은 object.name의 메모리를 단계적으로 덮어 쓰는 데 사용되는 16 진수 표현을 가져옵니다. 나는 그것을 (한 명령에서) 할 수있는 더 쉬운 방법이 있다고 확신하지만, 올바른 매개 변수 값을 이해할 수 없다.

+1

모든 바이트를 한번에 대체하는 구문은'메모리 쓰기

... '입니다. 예를 들어'메모리 쓰기 0x1000022c0 -s 1 66 6F 6F 62 61 72' ... –

+0

흥미로운 해결책이지만, 이상적이지는 않지만 고마워요. – sbarow

+1

@ l' L' l 그 답변을 주셔서 감사합니다. 작동하지 않는'memory write 0x1000022c0 -s 6 666F6F626172'와 같은 것을 시도했습니다. @sbarow 수락 해 주셔서 감사합니다. 매우 귀찮은 솔루션입니다. – Austin