0

Strangeness with non-nil, non-empty NSStrings returning a zero length. Here is how I'm setting up my string.

// property is declared like so in .h
@property (strong, nonatomic) NSString *myStringValue;

// synthesized like so in .m
@synthesize myStringValue;

// most times property is set directly like so (.m)<br />
// ...
self.myStringValue = @"Some Value";
// ...

// property is (sometimes) set like so (.m)<br />
// copied from another vc
// ...
self.myStringValue = anotherObj.myStringValue;
// ...

Zero Length
When the property has been set by assigning it from another object (the latter case), and used later on at some point in the code, [self.myStringValue length] returns 0 even though mouseover using the debugger shows the summary text correctly as "Some Value".

NSString Optimization?
This blog talks about NSString optimizations that may happen when there are multiple NSString objects floating around in memory that contain the same string. I wonder if this could have anything to do with it. Since NSString is implemented as a class-cluster, and NSString objects may be transformed into any number of related private classes (*__NSCFString, __NSCFStringConstant*) behind the scenes for optimization purposes, perhaps this is what is causing my problem, if either of these classes happen to return zero for length messages...

Thoughts?

eric
  • 4,863
  • 11
  • 41
  • 55
  • i think it just one of the debugger's bug. try `p (int)[self.myStringValue length]` in debugger which should give you the actual value – Bryan Chen Mar 25 '13 at 20:11
  • The internal objects are designed to be identical in behaviour, so you never know which one you're using. None of them do silly things with the length. You'll have to give more details; can you provide a minimal complete example? Are the values you set special in any way (maybe they begin with a null character?) Have you tried logging the string's value and length (instead of simply using the debugger)? – Dave Mar 25 '13 at 20:11
  • please try a copying property http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain – vikingosegundo Mar 25 '13 at 20:45

1 Answers1

0

why don't you try using

 @property (copy, nonatomic) NSString *myStringValue;
nkongara
  • 1,233
  • 8
  • 14