Objective-C help. I don't know where else to go. :P

What did I do wrong? It keeps logging (null) but I keep looking for different ways. I did start out with one 2 lines, I was thinking that it was insufficient data.

-(void)readbutton:(id)sender { NSString *filepath = [[NSString alloc] initWithString:@"/Users/dylanweber/Documents/file.txt"]; NSBundle *filepathdata = [[NSBundle alloc] initWithPath]; NSString *filesContent = [[NSString alloc] initWithContentsOfFile:filepathdata]; NSLog(@"%@", filesContent); [textfield setStringValue:filesContent]; }

Cut out the NSBundle line; the file isn’t in your app’s bundle.

And initWithContentsOfFile: is an incomplete method; you need initWithContentsOfFile:encoding:error: or initWithContentsOfFile:usedEncoding:error:.

But this isn’t really the right place. You might like to subscribe to Apple’s cocoa-dev list or omnigroup’s macosx-dev list, although both are high-volume/low-tolerance.

- (void)readbutton:(id)sender { NSString *filepath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/file.txt"]; NSString *filesContent = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@", filesContent); [textfield setStringValue:(filesContent) ? filesContent : @""]; }
change the encoding (to NSASCIIStringEncoding, NSMacOSRomanStringEncoding, NSISOLatin1StringEncoding, NSUnicodeStringEncoding) if it’s not UTF8

Why not? Actually you can’t write AppleScriptObjC code without basic knowledge of Objective-C

I’m happy to be corrected; I’d assumed it was a place for what it says, AppleScriptObjC.

You can’t write much, but you can write some, courtesy of Cocoa bindings.

I could be wrong – I thought the OP was after some straight Objective-C advice, and nothing to do with AppleScriptObjC. However, if he is looking at it with AppleScriptObjC in mind, he can’t use Foundation functions like NSHomeDirectory().

I want to set the string value of a multi-line text field to this string, but it apparently can’t read UTF-8. What encoding should I then use? I looked in Apple’s Developer Documentation, but that didn’t say what encoding.

P.S. I want to learn Objective-C so badly, but when I can’t find something in the documentation, I’m stuck. Google barley helps.

And it does log the string correctly.

The encoding depends on the platform and the application the file was created with.
Above I wrote the alternative encodings. If the file was created on a Mac, try NSMacOSRomanStringEncoding

The file is UTF-8, but I can’t set the string value of a text field to the file’s contents. I made it with TextEdit and saved it when I selected “Unicode (UTF-8).” It’s not MacRoman or NSUnicodeStringEncoding. I tried.

If it’s UTF8, NSUTF8StringEncoding should work

I got it! For some weird reason, all of the Cocoa bindings got destroyed because a character in my header file got deleted, but the buttons still worked. :confused: Strange.

Either way, it’s working. But, what is the method of setting the string value of a NSTextView?

[code]IBOutlet NSTextView *view;
NSString *string = @“Hello”;

[[view textStorage] setStringValue:string];[/code]

[[view textStorage] setStringValue:string];

I’ve been playing with that. It hasn’t been working. I’m going to try “setString:”