MutableAttributeString在iOS 7中工作,但在iOS 6中不工作
我一直在使用一个应用程序,它从MutableAttributeString在iOS 7中工作,但在iOS 6中不工作,ios,objective-c,ios6,ios7,nsattributedstring,Ios,Objective C,Ios6,Ios7,Nsattributedstring,我一直在使用一个应用程序,它从.rtf文件创建一个NSAttributedString。我一直在iOS 7上测试这个应用程序,没有任何问题。但是,当我在iOS 6上测试此应用程序时,出现以下错误: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSConcreteAttributedString initWithFileURL:选项:DocumentAttribute:错误:]:发送到实例0x9a77010的选择器无法识别。 以下是我的代码:
.rtf
文件创建一个NSAttributedString
。我一直在iOS 7上测试这个应用程序,没有任何问题。但是,当我在iOS 6上测试此应用程序时,出现以下错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSConcreteAttributedString initWithFileURL:选项:DocumentAttribute:错误:]:发送到实例0x9a77010的选择器无法识别。
以下是我的代码:
NSError *error;
NSURL *stringURL = [[NSBundle mainBundle] URLForResource:@"Text" withExtension:@".rtf"];
NSAttributedString *myAttributedText = [[NSAttributedString alloc] initWithFileURL:stringURL options:nil documentAttributes:nil error:&error];
从苹果文档中-
initWithFileURL:options:documentAttribute:error:
仅在iOS 7.0中可用
编辑:如评论中所述
如果要测试对象或协议(继承自NSObject)上是否有选择器,则在本例中可以使用[object respondsToSelector:@selector()]
进行检查
NSAttributedString *myAttributedText;
if ([myAttributedText respondsToSelector:@selector(initWithFileURL:options:documentAttributes:error:)]) {
myAttributedText = [[NSAttributedString alloc] initWithFileURL:stringURL options:nil documentAttributes:nil error:&error];
}
else {
// Init some other way
}
从苹果文档中-
initWithFileURL:options:documentAttribute:error:
仅在iOS 7.0中可用
编辑:如评论中所述
如果要测试对象或协议(继承自NSObject)上是否有选择器,则在本例中可以使用[object respondsToSelector:@selector()]
进行检查
NSAttributedString *myAttributedText;
if ([myAttributedText respondsToSelector:@selector(initWithFileURL:options:documentAttributes:error:)]) {
myAttributedText = [[NSAttributedString alloc] initWithFileURL:stringURL options:nil documentAttributes:nil error:&error];
}
else {
// Init some other way
}
发生这种情况是因为您调用的方法
initWithFileURL:选项:文档属性:错误:
was仅在iOS 7.0中引入
您可以在此处查看iOS 6.1到iOS 7.0 API的差异:
在这里,你可以看到
调用不存在的方法将导致应用程序崩溃。您应该将部署目标设置为7.0,或者使用类似于
ifdefs
的方法,以避免在早期版本()中调用此方法。发生这种情况是因为您正在调用的方法
initWithFileURL:选项:文档属性:错误:
was仅在iOS 7.0中引入
您可以在此处查看iOS 6.1到iOS 7.0 API的差异:
在这里,你可以看到
调用不存在的方法将导致应用程序崩溃。您应该将部署目标设置为7.0或使用类似于
ifdefs
的方法,以避免在早期版本()中调用此方法。当然,您可以测试新接口是否受支持,如果不受支持,则使用回退方法。好的,@Evan这很有意义。因此,这就引出了一个问题:如何在iOS 6中加载rtf
文件?@Cody不幸的是,这不是我必须处理的事情,所以我不确定是否有可能。你最好的选择是寻找一个这样做的图书馆。当然,您可以测试新接口是否受支持,如果不受支持,则使用回退方法。因此,这就引出了一个问题:如何在iOS 6中加载rtf
文件?@Cody不幸的是,这不是我必须处理的事情,所以我不确定是否有可能。你最好的选择是寻找一个这样做的图书馆。