Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Problem getting NSString from text file

Problem getting NSString from text file
Thread Tools
GeeYouEye
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status: Offline
Reply With Quote
Sep 15, 2004, 12:48 AM
 
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.
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Sep 15, 2004, 01:33 AM
 
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:......];
     
GeeYouEye  (op)
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status: Offline
Reply With Quote
Sep 15, 2004, 01:59 AM
 
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
Guest
Status:
Reply With Quote
Sep 15, 2004, 10:35 AM
 
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:......];
     
GeeYouEye  (op)
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status: Offline
Reply With Quote
Sep 15, 2004, 11:01 AM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 11:12 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,