为什么这个变量不是一个属性字符串? [英] Why isnt this variable an Attributed String?

查看:52
本文介绍了为什么这个变量不是一个属性字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中有这 3 行,它们旨在从 .txt 文件中获取文本并将其分配给 NSAttributedString

I have these 3 lines in code which are meant to take text from a .txt file and allocate it to an NSAttributedString

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

NSAttributedString *attString = [[NSAttributedString alloc] initWithString:text];

使用调试器我知道 path 被正确找到,文本被添加到变量 text 很好,但它没有转换为 NSAttributedString - 调试器只是说变量不是 NSAttributedString"

Using the debugger I know that the path is being found correctly, and the text is being added to the variable text fine, but its not converting to the NSAttributedString - the debugger just says "variable is not NSAttributedString"

我在第三行中遗漏了什么以允许将 NSString 转换为 NSSAttributedString?(我不想要任何属性,但我假设您可以将字符串加载到属性字符串中?)

What am I missing from the third line to allow the NSString to be converted to an NSSAttributedString? (I dont want any attributes, but Im presuming you can just load the string into an attributed string?)

推荐答案

我猜这个问题可能来自其他地方,刚试过:

The problem might come from somewhere else I guess, just tried :

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

NSAttributedString *attString = [[NSAttributedString alloc] initWithString:text];

NSLog(@"Text : %@\nNSAS : %@", text, attString);

这是输出:

2013-06-10 11:17:04.444 TestsSO[5554:c07] Text : hello, world !
NSAS : hello, world !{
}

您确定 text 填写正确吗?

Are you sure that text is filled correctly ?

这篇关于为什么这个变量不是一个属性字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆