Objective C code problem

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
User avatar
bcnipod





Posts: 3580
Joined: Tue May 15, 2007 8:52 am
Location: 45 Minutes outside Boston
Contact:

Objective C code problem

Post by bcnipod »

ok here is my problem. i am building a application that will open .txt files, you can then edit them and save them. the save function works, the editing works, but i cannot get it to open them. I am writing this in Objective C and whenever i try to open a .txt file it crashes the application. I am building this in Xcode. Here is the code i am using:

Code: Select all

- (IBAction)openTextFile:(id)sender

{

	NSOpenPanel *theOpenPanel = [NSOpenPanel openPanel];

	if ([theOpenPanel runModal] == NSOKButton)

	{

		NSString *theFileName = [theOpenPanel filename];

		NSString *theFileContents = [NSString stringWithContentsOfFile:theFileName];

		[theTextView setString:theFileContents];

		[theFileName release];

		[theFileContents release];

	}

}

Twitter: Dirk Gently | Major lulz
Mr. Brightside: Worst mod I've seen since 'Nam.
Website
User avatar
Prey




Connoisseur Snitch! Pyre Articulatist 500

Posts: 1026
Joined: Wed Dec 27, 2006 6:49 am
Location: UK
Contact:

Re: Objective C code problem

Post by Prey »

Heh, Mac coding. Well I've never used the language, or owed a MAC, but perhaps try not releasing the 'theFileContents'. But if SetString does actually copy the string, then.. maybe it is because you are not releasing the 'theOpenPanel'?

[/guess]
Halo 2 Prophet - Skin with ease with the simple 3D point and click interface.
Halo 3 Research Thread - Contribute to the research into Halo 3.
User avatar
bcnipod





Posts: 3580
Joined: Tue May 15, 2007 8:52 am
Location: 45 Minutes outside Boston
Contact:

Post by bcnipod »

ok i just tried that and it did not work.
Twitter: Dirk Gently | Major lulz
Mr. Brightside: Worst mod I've seen since 'Nam.
Website
dfmjw





Posts: 21
Joined: Thu Mar 10, 2005 7:48 pm
Location: South Australia

Post by dfmjw »

hey i have never used Objective C either. but would i would suggest is adding 101 printf(char* message); statements if needed just to find out where it fails. The error you got when your program crashes would be helpful. not sure if Xcode has a debugger but a few breakpoints would help. i also found something on reading txt files in objective C it used a different reading function than you did. http://maccoder.blogspot.com/2007/02/re ... using.html
initWithContentsOfFile:(NSString *)
worst comes to worst use stdio functions and read out the contents out as a char* and then convert to a NSString object. assuming there is some way of doing that.

and like prey said releasing the NSString theFileContents is not a good idea if you are not certain what it does with the NSString pointer when you call the [theTextView setString:theFileContents]; like if it copys the contents of NSString* or just copys the pointer.
Post Reply