0

I am following the code shown here : Convert hex string to long to read a long from a string. In their example, they have a hard-coded string, but in my own context have another value I am taking the string from and that value seems to change but I don't think it is supposed to. I'd like to keep the value being read at the start and as such pass a deep-copy of the string to the scanner instead.

I am aware that [string copy] and [string mutableCopy] provide a shallow copy and in NSString copy not copying? question we point out that NSString is supposed to be immutable and so the scenario where it is changing is also when its pointer changes. However I am wondering if using scanner causes issues with this premise? Could I be understanding the issue here entirely wrong? I don't think the original string is supposed to even change and yet in the debugger it is changing after a few lines.

1 NSString* pString = someIdentifier;
2
3 unsigned long long val;
4 NSScanner* scanner = [NSScanner scannerWithString: pString];
5 [scanner scanHexLongLong: &val];
6 
Debug breakpoints
1 pString = @"0x12345"
4 pString = @"9876543" (not exactly a hex string)
  val = "74576" (some decimal close to, but not the same as the original pString)
  scanner = 0x12b45 (same a pString with 1 bit different)
5 scanner = previous pString
6 pString = 0xFFFFF 
  val = 0xFFFFF ( in decimal) 
ahSatan
  • 1
  • 1
  • 1
    The answers to the linked question say: the string isn't copied if the string is not mutable. So either the string is copied or the string can't change. Why is your string immutable? Have you tried copying the string? – Willeke Jul 03 '22 at 08:54
  • 1
    How and when did you inpect the values of the variables? What should the values of the variables be? What does `[pScanner scanHexLongLong: &val]` return? Is `pScanner` a typo? – Willeke Jul 03 '22 at 09:07
  • 1
    Deep/shallow copy has no meaning for strings, these concept applies to container objects (arrays, objects holding pointers to other objects, etc). Is your problem related to the fact that the buffer that holds the string contents is or is not copied? Can you give us a [mcve] so we can better understand your problem? – Cristik Jul 03 '22 at 16:02
  • @Willeke pScanner was a typo in the question, I have edited. As for why my string is immutable, I assumed it would be due to it being an NSString. I inspected the value of the variables using the xcode debugger with breakpoints at essentially every line. Immediately after being passed to the scanner is when pString changes. [pScanner scanHexLongLong: &val] returns the new value of pString. val is still the original at this point, but in the next line becomes what it returns (I think that part makes sense) – ahSatan Jul 03 '22 at 16:21
  • @Cristik I think this is as close to the minimal example I can give. The only thing missing here is where I am getting the string from, but that would involve way too much code that I didn't even write. I suppose there are 2 issues I am trying to understand. 1. Why would the NSString change at all? 2. Is there any way for me to keep the original string before it changes? This problem does not exist if I use a string literal. – ahSatan Jul 03 '22 at 16:27
  • 2
    Seems you are conflating the `NSString` changing its contents, and the `NSString *` pointer changing its value. Those are two different behaviours, I assume you have issues with the latter. Also, I don't see in the code snippet any possible sources where the string can change, that's why I asked for some more context there. Maybe try and convert the debugger instructions to Objective-C lines of code? – Cristik Jul 03 '22 at 16:33

0 Answers0