我们不能把NSMutableAttributedString换成NSString吗?

我有两个NSMutableAttributedStrings,我将第二个字符串附加到第一个字符串上,如下所示:

[string1 appendAttributedString:string2];

因为我必须在标签上显示string1,所以我需要:

self.label1.text = (NSString *)string1;

我有"unrecognized selector sent to instance"个错误.

我做错什么了吗?这不是给标签的文本属性赋值NSMutableAttributedString的正确方法吗?

推荐答案

不能使用强制转换将对象从一种类型转换为另一种类型.使用提供的方法:

label1.text = [string1 string];

更好的方法是使用属性字符串:

label1.attributedText = string1

Objective-c相关问答推荐

iPhone 可达性判断

ARC下归零弱引用的集合

将长按手势和拖动手势结合在一起

是否需要 ARC 中的 NSNotificationCenter removeObserver?

setFont 已弃用?

Interface Builder 中的 IBOutletCollection 集排序

NSString:从字符串中删除 UTF-8 重音的简单方法?

如何切换回已经加载的 UIViewController?

如何为图层 shadowOpacity 设置动画?

UILabel 的角半径属性在 iOS 7.1 中不起作用

无法加载从笔尖引用的图像

iOS6:supportedInterfaceOrientations 不起作用(被调用但界面仍然旋转)

关闭一次后如何返回允许推送通知对话框?

UIKeyboardBoundsUserInfoKey 已弃用,改用什么?

如何判断数组是空还是空?

笔尖中的原型单元而不是故事板

为什么我们不能在当前队列上使用 dispatch_sync?

Apple 说 NSManagedObjectContext 归创建它的线程或队列所有时是什么意思?

Foo 类在 MyApp 和 MyAppTestCase 中都实现了.将使用两者之一.哪个是未定义的

UITableViewCell中的文本居中对齐问题