Re: Reading/Writing Text files
Re: Reading/Writing Text files
- Subject: Re: Reading/Writing Text files
- From: "I. Savant" <email@hidden>
- Date: Mon, 11 Sep 2006 08:51:51 -0400
On Sep 11, 2006, at 8:29 AM, email@hidden wrote:
Slightly frustrated here. What I need to do is this...
I need to read in text files off a PC, manipulate some bytes of
data and then
write the data back out to plain text files. How do I do this? It
seems every
Cocoa application example I look at assumes that you want to use an
encoder,
so all I can find are examples of reading and writing with encoders.
What kind of encoder are you referring to? There's the <NSCoding>
protocol and there's "string encoding". The former is not needed. The
latter, however, is important in many cases and you probably
shouldn't ignore it.
I just want to.
1: Read in the text file into an NSTextView
2: Have the user make some minor changes
3: Write the new data back out (To a Plain text file)
Is this possible??
No, it's quite impossible. We have applications that can
manipulate movies and graphics, perform complex operations on audio
in real time, and even manage your music collection, but you can't
manipulate text files in cocoa, sorry.
Of course I'm kidding. :-)
Have you seen the TextEdit example code in your Developer folder?
That does quite a bit more than what you're going for, but it does
what you're going for very well ...
(more below)
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)type
{
// Let's see what we're getting...
NSLog(@"loadDataRepresentation: %@: %@",type, data);
NSAttributedString *tempString = [data description];
[self setString:tempString];
if ([self string] != nil)
{
[[myTextView textStorage] setAttributedString: [self
string]];
}
return YES;
}
Your problem is lack of reading the documentation, I'm sorry to
say. The docs on -loadDataRepresentation:ofType: state, "Subclasses
must override this method unless they override readFromFile:ofType:..."
Since you're just going for plain text, you should probably be
overriding -readFromFile:ofType: instead of -
loadDataRepresentation:ofType: ... just comment the above out and
start over with -readFrom...
Second, if you *were* using NSData, asking for its -description is
not the way to get its data. NSData's docs for -description state,
"Returns an NSString object that contains a hexadecimal
representation of the receiver’s contents." ... doesn't sound like
what you want, does it?
Third, you call -setAttributedString: and pass it an NSString
rather than an NSAttributedString. The documentation for -
setAttributedString: says that it expects an NSAttributedString.
Probably a bad thing, trying to pass an unexpected object type
(unless you really know what you're doing). Reading up on what you're
using will help you prevent such mistakes.
Finally, your post is incomplete. You imply your code isn't
working, but you don't mention how. Does it fail to compile? Does it
give a run-time error? In either case, *what is the error*??? You
need to take your time and list things like this if you expect good,
solid answers. We don't know your project - help us help you.
--
I.S.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden