cocoa - NSString stringWithUTF8String 附加错误

标签 cocoa nsstring

我正在尝试将一个字符数组附加到一个NSString。当我使用下面的代码时,出现此错误:

********* initialization method -initWithCharactersNoCopy:length:freeWhenDone: cannot be sent to an abstract object of class NSCFString: Create a concrete instance!

这是什么意思以及如何解决?

NSString *str =  [[NSString new] initWithString:@"Name: "];
NSString *name = [[NSString new] stringWithUTF8String:user.name];
//NSString *name = [[NSString stringWithUTF8String:seg.segname] copy]; //this also failed
str = [str stringByAppendingString:name];

最佳答案

我认为您正在寻找这样的东西:

NSString *str = @"Name: ";
NSString *name = [NSString stringWithUTF8String:user.name];
str = [str stringByAppendingString:name];

关于cocoa - NSString stringWithUTF8String 附加错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3215343/

相关文章:

ios - 为什么nstring obj的内存地址和iOS dev中复制的不一样?

cocoa - stringWithFormat 或 stringFromDate

objective-c - 替换字符串不起作用

iphone - 整数 0000 cocos2d

iphone - iphone NSString 的问题

c++ - RootViewController 在其委托(delegate)外部访问时不会添加 subview

swift - 无法在 Swift/Cocoa/NextStep 中将 JS 注入(inject) WKWebView/将 WKWebView 中网页上的用户选择推送到 Swift/Cocoa

cocoa - 为什么 Split View中文本字段中的文本会拉伸(stretch)?

cocoa - nstableview + 无法显示部分文件名

xcode - 在方案选项中为应用程序语言添加更多选项