|
|
Problem getting NSString from text file
|
|
|
|
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status:
Offline
|
|
I'm trying to open a file and get an NSString from it, which then is a data member of a class. Only problem is, when I use
Code:
[inText initWithContentsOfFile:[inPathField stringValue]];
the result of NSLog(@"%@", inText); is (null).
Here's the relevant code snippets:
Code:
@interface SYHorse : NSObject {
NSString *inText;
NSString *outText;
IBOutlet NSTextField *inPathField;
IBOutlet NSTextField *outPathField;
...
}
...
@end
@implementation SYHorse
- (IBAction)goButtonTrigger:(id)sender
{
...
[inText initWithContentsOfFile:[inPathField stringValue]];
NSLog(@"The file is %@", [inPathField stringValue]);
NSLog(@"The contents of the file are: %@", inText );
...
}
Assuming the inPathField contains "/Untitled.txt", the output, excluding the time and date stuff is
Code:
The file is /Untitled.txt
The contents of the file are: (null)
-[NSPlaceholderMutableString initWithString:]: nil string (or other) argument
The last error is what happens when the next function in the program tries to use inText.
And no, /Untitled.txt is not empty.
(
Last edited by GeeYouEye; Sep 15, 2004 at 12:54 AM.
)
|
I bring order to chaos. You are in chaos windows, you are the contradiction, a bug wishing to be an OS.
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
Have you alloc'ed "inText" anywhere? You must alloc (allocate memory) for an object before you init (initialise) it. That would explain why the init methos (eg, initWithContentsOfFile:) is returning nil (NULL).
Usually objects are allocated and initialised at once like:
inText = [[NSString alloc] init];
or
inText = [[NSString alloc] initWithContentsOfFile:......];
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status:
Offline
|
|
I was under the impression that since its an instance variable/data member (it's late and I don't remember which term is the Obj-C vs. C++ term) of my class that it doesn't need to be explicitly alloc'ed. However, I tried changing it anyway and still got the same result.
|
I bring order to chaos. You are in chaos windows, you are the contradiction, a bug wishing to be an OS.
|
|
|
|
|
|
|
|
hayesk
|
|
Originally posted by GeeYouEye:
I was under the impression that since its an instance variable/data member (it's late and I don't remember which term is the Obj-C vs. C++ term) of my class that it doesn't need to be explicitly alloc'ed. However, I tried changing it anyway and still got the same result.
You should have declared inText as:
NSString *inText;
You have then allocated a pointer only - not the data. That is what you want. Now you can go ahead and use:
inText = [[NSString alloc] initWithContentsOfFile:......];
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status:
Offline
|
|
Originally posted by hayesk:
You should have declared inText as:
NSString *inText;
You have then allocated a pointer only - not the data. That is what you want. Now you can go ahead and use:
inText = [[NSString alloc] initWithContentsOfFile:......];
Okay, that worked. Thanks! Although now I'm not entirely sure why another program of mine works at all... I'll have to look at it again.
|
I bring order to chaos. You are in chaos windows, you are the contradiction, a bug wishing to be an OS.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|