NSAttributedString initWithData: options: documentAttributes: error: documentAttributes not saved in ARC

I am trying to load documents from NSData(from a file Dropboxin my application, but for simplicity, the example below uses a .txt file, which causes the same problem I'm trying to fix).

Problem: I create an NSDictionary and pass it [NSAttributedString -initWithData:options:documentAttributes:error:]as an out parameter.

However, the NSDictionary instance gets released and calls -initWithData: options: documentAttributes: error: to crash.

When I turn on NSZombie, I get the error: [__NSDictionaryI save]: message sent to the freed instance

- initWithData:options:documentAttributes:error:works fine when I pass NULL to documentAttributes.

Here is the code:

NSError* error;
NSString* path = [[NSBundle mainBundle] pathForResource:@"Forward Thinking"
                                                 ofType:@"txt"];
NSData* data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedAlways error:&error];
if (data) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSDictionary* documentAttributes = [[NSDictionary alloc] init];
        NSAttributedString* attrStr = [[NSAttributedString alloc] initWithData:data options:nil documentAttributes:&documentAttributes error:NULL];
        self.textView.attributedText = attrStr;
    });
}

Any conclusions will be wonderful!

+4
1

NSDictionary, . :

NSDictionary* documentAttributes;
NSAttributedString* attrStr = [[NSAttributedString alloc] initWithData:data options:nil documentAttributes:&documentAttributes error:NULL];

documentAttributes . .

+11

Source: https://habr.com/ru/post/1525898/


All Articles