• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: how to deal with files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: how to deal with files


  • Subject: RE: how to deal with files
  • From: Marco Binder <email@hidden>
  • Date: Fri, 22 Nov 2002 18:35:28 +0100

Dont release Data... as far as I can see, your quite familiar with cocoas retain/release mechanism. You probably just forgot, that "convenience methods" (like most class methods) return an autoreleased instance. So does [NSData dataWithBytes:]...

marco


Am Freitag, 22.11.02 um 17:52 Uhr schrieb Pascal Goguey:

Hello,

I am new to Cocoa, and I am trying to figure out how to work with
files. I wrote a simple test. One GUI with a single button. One object
(see below). The button calls the method WriteFile, and that's
about it.
Everytime I push the button (the only one), it should write one
line at the end of the file. The code of the object is listed below.
The problem is that the program exits after the first button press.

Any hint?

Thanks,

Pascal


---------------
/* FileObject */

#import <Cocoa/Cocoa.h>

@interface FileObject : NSObject {
IBOutlet id mWriteButton;
NSFileHandle * mFileHandle;
}
- (IBAction)WriteFile:(id)sender;

@end
---------------
#import "FileObject.h"

@implementation FileObject

- (void)awakeFromNib {
mFileHandle = NULL;
}

- (IBAction)WriteFile:(id)sender {
NSString * Path = [[NSString alloc]
initWithCString:"/Users/pascal/Desktop/helpfile"];
NSData * Data = [NSData dataWithBytes:"help!\n" length:6];
// Create the file object if it does not exist yet
if(mFileHandle == NULL) {
// Check if the file itself exists at the specified path. If not,
create it
NSFileManager * mFileManager = [NSFileManager defaultManager];
if([mFileManager fileExistsAtPath: Path] == NO) {
[mFileManager createFileAtPath:Path contents:nil attributes:nil];
}
// Then at this point, the file exists physically on the disk
mFileHandle = [NSFileHandle fileHandleForUpdatingAtPath:Path];
}
[mFileHandle seekToEndOfFile];
[mFileHandle writeData:Data];
[mFileHandle closeFile];
[mFileHandle release];
mFileHandle = NULL;
[Path release];
[Data release];
}

@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


--
|\ /| E-Mail: email@hidden WWW: www.marco-binder.de
| \/ | Telefon: 07531 / 94 19 94 Fax: 07531 / 94 19 92
| |ARCO Snail-Mail: Banater Str. 3 - 78467 Konstanz
BINDER _____________________________________________________
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Creating your own genie effect
  • Next by Date: Re: text encodings
  • Previous by thread: Re: Problems subclassing NSTextField
  • Next by thread: RE:Problems subclassing TextFields
  • Index(es):
    • Date
    • Thread