Search code examples
iosobjective-cios9

Replacement for NSAttributedString initWithFileURL in iOS 9


I just find out that initWithFileURL from NSAttributedString was deprecated in iOS 9 and I don't see a proper replacement for it.

Maybe initWithData? How can I adapt this code, where I'm loading text from a rtf file.

_text = [[NSAttributedString alloc] initWithFileURL:fileURL 
                                            options:nil
                                 documentAttributes:nil
                                              error:&error];

EDIT:

What i'm doing now is this:

- (void)loadText
{
  NSURL *fileURL = [[NSBundle mainBundle] URLForResource:self.rtfFile withExtension:nil];
  NSData *data = [[NSFileManager defaultManager] contentsAtPath:[fileURL path]];

  NSError *error = nil;

_text = [[NSAttributedString alloc] initWithData:data
                                          options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType}
                               documentAttributes:nil
                                            error:&error];

}

But _text gets nil. I checked data and is not nil.


Solution

  • This is the function which is available in iOS 9 to Initialize NSAttribiuted String from a NSURL

    - (instancetype)initWithURL:(NSURL *)url
                    options:(NSDictionary<NSString *,
                                     id> *)options
         documentAttributes:(NSDictionary<NSString *,
                                     id> * _Nullable *)docAttributes
                      error:(NSError * _Nullable *)error
    

    and it is the link of apple documentation