我有一些代码来设置一个NSMutableAttributedString:间歇性崩溃
NSMutableParagraphStyle *centred = [[NSMutableParagraphStyle alloc]init];
[centred setAlignment:NSCenterTextAlignment];
NSDictionary *attributes = [[NSDictionary alloc]initWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica bold" size:12],NSFontAttributeName,
[NSColor whiteColor],NSForegroundColorAttributeName,
[NSColor clearColor], NSBackgroundColorAttributeName,
centred, NSParagraphStyleAttributeName,
nil];
NSMutableAttributedString* attribTitle = [[NSMutableAttributedString alloc]initWithString:@"Foo"];
[attribTitle setAttributes:attributes range:NSMakeRange(0, [[attribTitle string] length] - 1)];
它已经坐在我的应用程序的-awakeFromNib方法几乎从我开始,没有任何问题,但就在今天的项目,我已经开始让所有其他尝试在Xcode 4.3中运行应用程序崩溃与“线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x10)”。它打破了最终的'... setAttributes ...'一行。有一次,我尝试使用方法--initWithString:attributes:来初始化attribTitle,并确定崩溃(当它的时候)在alloc-init行上。
我已经尝试清除Xcode重新启动,但它仍然发生在每个其他构建...如果应用程序不崩溃然后attrbibuted字符串出现预期。
我想知道如果我有一个损坏的项目文件,但万一我做了一些傻事,任何人都可以指出一个错误 - 大概是'属性'NSDictionary?
谢谢!
托德。
如果你像这样初始化段落风格会发生什么:'NSMutableParagraphStyle *居中= [[NSParagraphStyle defaultParagraphStyle] mutableCopy];'? – Wienke
另一个问题可能是您命名字体的方式。试着打破这个代码并测试你的字体是否为零。 – Wienke
对于它的价值,我在一个工作项目中尝试了你的代码,到目前为止在OS X SDK 10.7上没有崩溃。按照Wienke的建议,我尝试记录字体,结果如下:'“Helvetica-Bold 12.00 pt。P [](0x7ff6734f3f40)fobj = 0x7ff6736d0190,spc = 3.33”' - 那里也没有问题。 – modocache