1

I've been working with an application that creates an NSAttributedString from an .rtf file. I've been testing this app on iOS 7 with no problems. However, when I tested this app on iOS 6, I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteAttributedString initWithFileURL:options:documentAttributes:error:]: unrecognized selector sent to instance 0x9a77010'

Here's the code that I have:

NSError *error;
NSURL *stringURL = [[NSBundle mainBundle] URLForResource:@"Text" withExtension:@".rtf"];
NSAttributedString *myAttributedText = [[NSAttributedString alloc] initWithFileURL:stringURL options:nil documentAttributes:nil error:&error];
Cody Winton
  • 2,989
  • 5
  • 25
  • 48

2 Answers2

4

From the Apple Docs - NSAttributedString UIKit Additions Reference

initWithFileURL:options:documentAttributes:error: is only available in iOS 7.0

EDIT: As mentioned in comments

If you want to test whether a selector is available on an object or protocol (that inherits from NSObject) then you can check using [object respondsToSelector:@selector()] in this case

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
}
Evan
  • 750
  • 7
  • 13
  • 1
    You can, of course, test whether the new interface is supported, and, if not, use a fallback approach. – Hot Licks Dec 03 '13 at 00:11
  • Ok, @Evan that makes sense. So, that begs the question: How do I load the `rtf` file in iOS 6? – Cody Winton Dec 03 '13 at 22:58
  • @Cody unfortunately this isn't something I have ever had to work with so I am unsure on the possibilities. Your best bet would be searching for a library that does it. [StackOverflow article that might help](http://stackoverflow.com/questions/10920678/ios-equivalent-to-macos-nsattributedstring-initwithrtf) – Evan Dec 05 '13 at 02:23
0

That's happening because the method you're calling

initWithFileURL:options:documentAttributes:error:

was introduces only in iOS 7.0.

You can check the iOS 6.1 to iOS 7.0 API Diffs here: iOS 6.1 to iOS 7.0 API Differences

And here, specifically, you can see the NSAttributedString UIKit Additions Class Reference

Calling a method that doesn't exist will cause your app to crash. You should set your deployment target to 7.0 or use something like ifdefs to avoid calling this method in earlier versions (reference link here).

Community
  • 1
  • 1
Bruno Koga
  • 3,864
  • 2
  • 34
  • 45