Cody Winton
Cody Winton

Reputation: 2989

MutableAttributedString Works in iOS 7 but not iOS 6

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];

Upvotes: 1

Views: 550

Answers (2)

Evan
Evan

Reputation: 750

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
}

Upvotes: 4

Bruno Koga
Bruno Koga

Reputation: 3874

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).

Upvotes: 0

Related Questions