开发者

Losing NSString

I'm copying an NSString from an NSDictionary into a local NSString using [[NSString alloc] initWithString:], processing it (removing some chars) then sending it to an external object.

I've used two methods a [[MyObject alloc] initWithString:] and [开发者_开发百科myObject setString:]; neither work.

Both of them make my app crash; when I use a breakpoint on the given area I get an empty NSString; I use NSLogs from the start of the NSString until I send it to my object; they all show the string's correct value…

Thank you all for your valuable input :-)


Replace the [[NSString alloc] initWithString:stringBeingCopied] call with [[NSMutableString alloc] initWithString:stringBeingCopied], creating a mutable string, as opposed to an immutable ("normal") string will stop the setString: calls from crashing your app

Immutable strings, being immutable, don't respond to setString: calls - which are NSString mutators. Creating an NSMutableString instead, which implements setString:, will let you modify the string object.


I've used two methods a [[MyObject alloc] initWithString:] and [myObject setString:]; neither work.

Show your work! Can't help you without showing more code. In particular, how did you implement initWithString: and setString: on your MyObject class?


Strings are normally immutable. To modify a string, you need a NSMutableString. Either do [string mutableCopy], or do this from inside the initWithString method. Maybe you do this already, but please post your code if possible. I'm afraid it's hard to give any further help without it...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜