How to create and use temp NSRange in lldb?
Asked Answered
T

2

10

NSRange is just a C struct. I want to create a temporary one in lldb in Xcode at a breakpoint.

Specifically for use in NSArray method objectAtIndex:inRange:

This does not work.

(lldb) expr NSRange $tmpRange = (NSRange){0,4}
(lldb) expr $tmpRange
(NSRange) $tmpRange = location=0, length=4
(lldb) expr -o -- [items indexOfObject:item4 inRange:$tmpRange]
error: no matching constructor for initialization of 'NSRange' (aka '_NSRange')
error: 1 errors parsing expression

My code has an NSRange var named badRange at the breakpoint, and passing that one in works. Thus:

(lldb) expr -o -- [items indexOfObject:item4 inRange:badRange]
0x7fffffffffffffff
(lldb) expr badRange
(NSRange) $1 = location=0, length=3

What is going on?

Thanks.

Tract answered 23/4, 2015 at 23:5 Comment(8)
Someone was talking about that specific error on the LLVM mailing list in 2013. Sadly, the answer seems to be "file a bug".Tract
Submitted bug 20684329 at bugreport.apple.comTract
It seems to work just fine with Xcode 6.3.1.Tartrazine
Interesting. I am trying it right now in Xcode 6.3.1 (6D1002) and I get the same "no matching constructor" error. Some setting difference we have?Tract
I created a project with a new application template and put a breakpoint at the startup of the app. Have you tried that?Tartrazine
I tried a new project in Xcode 6.3.1 and got the same result. Perhaps it is iOS specific? I get this same result on iOS Simulator or actual device. Perhaps you are running OS X app? Breakpoints in viewDidLoad and application:didFinishLaunchingWithOptions:.Tract
You are absolutely right. I tried creating an iOS project and I wasn't able to create a NSRange in the debugger. In fact I searched in the iOS frameworks and even though Foundation provides the header file in which the struct is declared, it doesn't expose any corresponding symbol. Basically, on iOS, NSRange is just a forward declaration and I do not know the real symbol for the implementation.Tartrazine
@Tartrazine Great work! Thank you! Can you turn that into an answer?Tract
T
4

Creating a NSRange in the debugger works fine when working in a OS X project but it doesn't for iOS projects. The reason it doesn't work on iOS is that even though Foundation provides the header file in which the struct is declared, it doesn't expose any corresponding symbol. Basically, on iOS, NSRange is just a forward declaration and I do not know the real symbol for the implementation.

Tartrazine answered 29/4, 2015 at 14:26 Comment(0)
S
4

I needed to create an NSRange recently whilst trying to debug some code and came across this thread. It is currently possible to do this for iOS projects using Xcode 8.3.2 with the following syntax.

po [@"test words here" stringByReplacingOccurrencesOfString:@"\\s" withString:@"" options:1024 range:(NSRange){0,15}]

This also works:

expr NSRange $tmpRange = (NSRange){0,15}
po [@"test words here" stringByReplacingOccurrencesOfString:@"\\s" withString:@"" options:1024 range:(NSRange)$tmpRange]

Not sure when this was fixed (or if it ever was, as leaving off (NSRange) on the second example results in the same error), but it works now.

Sextain answered 25/5, 2017 at 3:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.