0
votes

I created a file using the following code:

    NSMutableString *tabString = [NSMutableString stringWithCapacity:0];  //  it will automatically expand

//  write column headings  <----- TODO

//  now write out the data to be exported
for(int i = 0; i < booksArray.count; i++)  {
    [tabString appendString:[NSString stringWithFormat:@"%@\t,%@\t,%@\t\n",
                             [[booksArray objectAtIndex:i] author],
                             [[booksArray objectAtIndex:i] binding],
                             [[booksArray objectAtIndex:i] bookDescription]]];
}

if (![self checkForDataFile: @"BnN.tab"])  //  does the file exist?
    [[NSFileManager defaultManager] createFileAtPath:documentsPath contents: nil attributes:nil];  //  create it if not

NSFileHandle *handle;
handle = [NSFileHandle fileHandleForWritingAtPath: [NSString stringWithFormat:@"%@/%@",documentsPath, @"BnN.tab"]];  //  <---------- userID?
[handle truncateFileAtOffset:[handle seekToEndOfFile]];   //  tell handle where's the file fo write
[handle writeData:[tabString dataUsingEncoding:NSUTF8StringEncoding]];  //position handle cursor to the end of file  (why??)

This is the code I am using to read back the file (for debugging purposes):

    //  now read it back
NSString* content = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",documentsPath, @"BnN.tab"]
                                              encoding:NSUTF8StringEncoding
                                                 error: ^{ NSLog(@"error: %@", (NSError **)error);
                                                 }];

I am getting 2 build errors on this last statement that says:

Sending 'void (^)(void)' to parameter of incompatible type 'NSError *__autoreleasing *'

and

Use of undeclared identifier 'error'

This is the first time I am using a block to handle method returned errors; I was unable to find any docs in SO or Google showing how to do this. What am I doing wrong?

1
Why do you think you can pass a block to a parameter with a type of NSError **?rmaddy
Uhh... I saw it in an example...wrong, huh? It's the block I'm having problems with... SDSpokaneDude

1 Answers

1
votes

That function is expecting an NSError** parameter, not a block. The way you should be calling it is something like:

NSError *error = nil;
NSString* content = [NSString stringWithContentsOfFile: [NSString stringWithFormat:@"%@/%@", documentsPath, @"BnN.tab"]
                                              encoding: NSUTF8StringEncoding
                                                 error: &error];
if (content == nil) {
    NSLog("error: %@", error);
}