Objective-C: How do you append a string to an NSMutableString?

后端 未结 2 1221
南方客
南方客 2021-01-16 05:47

URL Download

http://code.google.com/p/mwiphonesdk/source/browse/#svn/trunk/iMADE/PrepTasks/08

I have code at the location at the

相关标签:
2条回答
  • 2021-01-16 06:32

    As for having an NSMutableString* and needing to return an NSString*, the former is a subclass of the latter so anywhere you see an NSString* an NSMutableString* will suffice as-is.

    Your code looks OK from what you've posted. The only thing I can think of is that perhaps there isn't any data to speak of when initializing the str variable. In such a case appending an empty string will do nothing to mutableString.

    You'll also want to make sure self.mutableString has been properly allocated and initialized. You can send messages to NSObject*s that are nil which may be misleading when [self.mutableString length] returns 0.

    0 讨论(0)
  • 2021-01-16 06:36

    I have fixed the problem. I simply was not initializing the NSMutableString value and it was transparently not doing anything.

    Before appending the string I put the following code.

    if (_mutableString == nil){
        _mutableString = [[NSMutableString alloc] init];
    }
    

    Thanks everyone for answering. And it is good to know that I can use NSMutableString in place of NSString. (that is too easy) :)

    0 讨论(0)
提交回复
热议问题