objective-c 将换行符附加到 NSString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1699678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append a newline to an NSString
提问by Spanky
I have this:
我有这个:
if (soapResults != nil) {
soapResults = [soapResults stringByAppendingString:@"\n"];
}
But I get a warning:
但我收到警告:
Assignment from distinct Objective-C type on build.
在构建时从不同的 Objective-C 类型分配。
When I run it on device I get:
当我在设备上运行它时,我得到:
Program received signal: "EXC_BAD_ACCESS".
from gdb.
来自 gdb。
Any ideas how to append a newline to an NSString without getting a warning or error?
任何想法如何在不收到警告或错误的情况下将换行符附加到 NSString?
Any help appreciated // :)
任何帮助表示赞赏 // :)
回答by BitDrink
An alternative is: [NSString stringWithFormat: @"%@\n", soapResults]even if the logic is the same!
另一种选择是:[NSString stringWithFormat: @"%@\n", soapResults]即使逻辑是相同的!
However, even in the case of stringByAppendingString, it returns a new string but it doesn't change the target, so you have to assign the new string to a new instance of the class NSString.
但是,即使在 stringByAppendingString 的情况下,它也会返回一个新字符串,但不会更改目标,因此您必须将新字符串分配给类 NSString 的新实例。
回答by Alok Srivastava
You can try this, it works for me:
你可以试试这个,它对我有用:
NSString *content1 = [messageObject valueForKey:@"content"];
NSString *seprator = @"\n----------------------\n";
NSString *myString = [NSString stringWithFormat:@"%@\n%@\n",seprator,content1];
content = [content stringByAppendingString:myString];
`
`
回答by Tim Windsor Brown
I've found \r works better than \n when using NSStrings.
我发现 \r 在使用 NSStrings 时比 \n 效果更好。
So:
所以:
NSString *stringWithNewLine = [soapResults stringByAppendingString:@"\r"];
回答by NSResponder
The object you get back from -stringByAppendingString: is autoreleased. You have to retain it if you want it to persist. If you don't retain it, you'll get a memory exception the next time you try to access it.
您从 -stringByAppendingString: 返回的对象是自动释放的。如果你想让它持续存在,你必须保留它。如果不保留它,下次尝试访问它时会出现内存异常。
An easy way to make sure this happens is to declare "soapResults" as a property, and synthesize accessors for it.
确保发生这种情况的一种简单方法是将“soapResults”声明为属性,并为其合成访问器。
回答by NSResponder
You are using forward slash , thats means it will be ignoring n, so basically you are appending nothing. I believe that may be a problem
您正在使用正斜杠,这意味着它将忽略 n,所以基本上您什么都不附加。我相信这可能是一个问题
回答by duncanwilcox
You are likely to have memory management issues, soapResults is probably a stray pointer to an object that has been deallocated. Hard to say without seeing more code, but checking where the object has been originally allocated is a good start.
您可能会遇到内存管理问题,soapResults 可能是一个指向已释放对象的杂散指针。如果没有看到更多代码,很难说,但是检查对象最初分配到哪里是一个好的开始。
For example if it has been allocated via a [NSString stringWith... ] call then it is autoreleased by default, i.e. it will be released at the end of the run loop and you have to retain it if you want to hold on to it.
例如,如果它是通过 [NSString stringWith...] 调用分配的,那么默认情况下它是自动释放的,即它将在运行循环结束时释放,如果你想保留它,你必须保留它.

