在NSMutableAttributedString初始化期间代码崩溃

时间:2014-04-29 08:09:47

标签: ios objective-c

我需要使用字符串productDesc初始化NsmutableAttributedString,但代码崩溃

 attrStrInfoLabel= [[NSMutableAttributedString alloc] initWithString:productDesc]; 

错误[NSConcreteMutableAttributedString _encodingCantBeStoredInEightBitCFString]

请建议我的代码是

NSMutableAttributedString  *attrStrInfoLabel;
  NSMutableString *productDesc;
  productDesc = [NSMutableString stringWithFormat:@"PRODUCT DESCRIPTION:%@",         [productDescription  objectAtIndex:i]];
  attrStrInfoLabel= [[NSMutableAttributedString alloc] initWithString:productDesc];

3 个答案:

答案 0 :(得分:1)

尝试使用NSAttributedString而不是NSMutableString。 请看下面的代码示例。

 NSMutableAttributedString *attrStrInfoLabel = [[NSMutableAttributedString alloc] init];
 NSAttributedString *productDesc = [[NSAttributedString alloc] initWithString:[NSMutableString stringWithFormat:@"PRODUCT DESCRIPTION:%@",[productDescription  objectAtIndex:i]];
[attrStrInfoLabel appendAttributedString:productDesc];

同时检查productDescription中的数据。检查是否为!nil并且计数> 0。

答案 1 :(得分:0)

您发布的代码不会导致错误,但我将假设您在以下行中进行了NSLog()来电:

NSLog(@"%@", attrStrInfoLabel);

应更改为:

NSLog(@"%@", [attrStrInfoLabel string]);

答案 2 :(得分:0)

初始化然后追加。

NSMutableString *str = [[NSMutableString alloc] init];
[str appendString:YOUR_STRING];
相关问题