Import a file - form NSOpenpanel to textview - cocoa for Mac app

342 views Asked by At

How would I be able to import a selected files (txt)from nsOpenPanel to a textView or another View?

thank's Mauro

1

There are 1 answers

0
Joshua Nozzi On

About Research

If you intend to develop software as anything more than a hobby, you really need to work on your research skills. It's evident from your question and our conversation in comments that you haven't done your homework and are actively resisting it with every ounce of your being. You know how to get a path from the open panel, you know about NSString because you said you have one in an NSArray, and you know you want it in a text view (NSTextView), yet you want to find "a method" in the docs. What I was telling you in the comments is: read the documentation for each of the involved classes. There is no shortcut; there is no alternative route - you must use the reference material for your platform ("read the docs") and familiarize yourself with the classes you intend to use. Sure, it may take a few minutes to a few hours to learn about a part of the API but unless you want to hire someone to do this job for you, it's part the the job and you're just going to have to do it.

Example

If you can state your goal, you can research your way to it. You have a path as an NSString and you want to load its (string) contents into a text view. If you read the Cocoa docs for strings and text views, you can easily find the methods that let you 1) create an NSString instance with the contents of a path or URL, 2) pass that string to the NSTextView instance. The sidebar of the class reference docs even helpfully categorize class methods by common tasks. Helpful! From there you can start to piece together some code:

// Get one path (because you have only one text view, right?)
NSString * path = myPaths.firstObject;

// Try to load its contents
NSError * error = nil;
NSStringEncoding encoding;
NSString * fileContents = [NSString stringWithContentsOfFile:path usedEncoding:encoding error:error];
if (fileContents)
{
  // Yay!
  [myTextView setString:fileContents];
} else {
  // Boo!
  [myTextView setString:@""];
  NSLog(@"Error loading %@. Underlying error: %@", path, error);
}

...and there you go. Safety checks and all.