Simple file reader wont work
I trying to learn Xcode, Cocoa, Objective C. Today I thought I was going to make a simple app the uses the Nac file browser, reads the file and displays it in an NSTevtView.
Everything acts like it wants to work but the file never displays. If I debug it, it shows my string has the file contents but I can't get it to display to the TextView.
Here is my code, OpenUp.h and OpenUp.m
#import <Foundation/Foundation.h>
@interface OpenUp : NSObject
{
NSString *reader;
IBOutlet NSTextField *mainField;
}
@property(readwrite,copy)NSString *reader;
- (void)awakeFromNib;
- (IBAction)openExistingDocument:(id)sender;
@end
//OpenIt.m
#import "OpenUp.h"
@implementation OpenUp
@synthesize reader;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (IBAction)openExistingDocument:(id)sender{
NSOpenPanel* panel = [NSOpenPanel openPanel];
[panel retain];
// This method displays the panel and returns immediately.
// The completion handler is called when the user selects an
// item or cancels the panel.
[panel beginWithCompletionHandler:^(NSInteger result){
if (result == NSFileHandlingPanelOKButton) {
NSURL* theDoc = [[panel URLs] objectAtIndex:0];
NSLog(@"%@", theDoc);
//open the document
NSError *error;
self.reader = [[NSString alloc] initWithContentsOfURL: theDoc encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",error);
}
// Balance the earlier retain call.
[panel release];
}];
[mainField setStringValue: self.reader];//this should display the con开发者_如何学Pythontents of the string
}
- (void)awakeFromNib {
self.reader = @"";//initialize reader
}
@end
I'm not sure if I'm doing something wrong with memory allocation, or if a string read from a file won't use this windows. I've tried both the NSScrollViewer and the NSTextField Everything compiles and acts like it wants to work it just wont display the string.
Any help is very much appreciated. Mike
Make sure the IBOutlet
is actually connected in the XIB. Try logging its value after you set its string.
The answer is in your comment. As it correctly states to the beginWithCompletionHandler: method, this will return immediatly (before the user selects the file). So the
[mainField setStringValue: self.reader];
call should be inside the completion handler block, right after
self.reader = [[[NSString alloc] initWithContentsOfURL:theDoc encoding:NSUTF8StringEncoding] autorelease];
By the way, note that I added an autorelease call. You define the property as copy, so you would be leaking memory if you don't autorelease. Also, don't write
NSError *error;
but
NSError *error = nil;
Since I am not familiar with the Nac file browser you are using, I provided a different method to display your file. I found some code for a file chooser I use in my encryption tool that was especially helpful - The code is below:
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:YES];
[openDlg setPrompt:@"Select"];
[openDlg setAllowsMultipleSelection:NO];
if ([openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
NSArray* files = [openDlg filenames];
for(NSString* filePath in
[openDlg filenames])
{
NSLog(@"File Path:%@", filePath);
[filePathName setStringValue:filePath]; // get filepath
}
}
With this, when a user selects a file, you can return the filepath name in the console. Now what you want to do is get the data using the filepath. You can do this using dataWithContentsOfFile:
method.
NSData *data = [NSData dataWithContentsOfFile:filePath]
You now have the data of the file the user selected. We use the pointer 'data' to refer back to the data later on. You now want to display the data somewhere. I'm not sure why you would want to display the data on an NSTextfield however, because it would only work if the user would select a text file, unless you were just displaying the bytes (which wouldn't make sense).
You can then convert this data to a string:
NSString *yourStuff = [[[NSString alloc] initWithData:myData
encoding:NSUTF8StringEncoding] autorelease];
Now that you have your string, you can write it to the NSTextView:
[yourNstextiview setStringValue:yourstuff];
I hope that helps.
精彩评论