• 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: Writing NSString to a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing NSString to a file


  • Subject: Re: Writing NSString to a file
  • From: Paul Lynch <email@hidden>
  • Date: Thu, 1 Jun 2006 15:20:26 +0100


On 1 Jun 2006, at 10:27, dinu john wrote:

What is wrong with this code ?

See my comments in-line.

Code is compiling and working fine (Creating the temp.rtf ) but when i accessing the temp.rtf file ,
error displaying "temp.rtf couldnot be opened" What is the reson ? am i want to add anything else for writing a string to
file ?



Dinu -(void) __WriteString { NSString *m_TestStr; NSFileManager *FlMgr; FlMgr=[NSFileManager defaultManager];

Each of the names used here is inconsistent with normal ObjC/Cocoa conventions. Methods and temporary variables should start with a lower case letter. Underscores are not wrong in the same way, but convention avoids them.


	m_TestStr=@"/Users/Dinu/Desktop/Temp";
	[FlMgr createDirectoryAtPath:m_TestStr attributes:nil];
	m_TestStr=@"/Users/Dinu/Desktop/Temp/temp.rtf";

OK so far.
NSString *myString = @"Hello From DinuJohn";
const char *utfString = [myString UTF8String];
NSData *myDat = [NSData dataWithBytes: utfString length: strlen (utfString)];

If you really want a data out of an NSString, use dataUsingEncoding:.

However, if you want to create an rtf, you should promote the string to an NSAttributedString and set some attributes. Use: RTFFromRange:documentAttributes:, or the NSFileWrapper equivalent.

m_Flag=[FlMgr createFileAtPath:m_TestStr contents:myDat attributes:nil];

If you want to write out a string, use writeToFile:atomically:encoding:error:, there's no need to create an intermediate data/C string storage.


	if(m_Flag)
	{
		NSRunAlertPanel(@"Info",@"The File Created",nil,nil,nil);
	}

This is largely a matter of taste, but you really should consider making your alert on error, not on success. Consider also using NSBeginAlertSheet, running modal for the window rather than app, and setting a meaningful label for the default button (which will show "OK" unless you tell it otherwise, and Apple UI fans don't like seeing "OK".


}

Short version of your code:

NSString *myString = @"Hello From DinuJohn";
if (![myString writeToFile:"/Users/Dinu/Desktop/Temp/temp.txt" atomically:YES
encoding:NSUTF8StringEncoding error:NULL]) {
...
}



Paul

_______________________________________________
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


References: 
 >Writing NSString to a file (From: dinu john <email@hidden>)

  • Prev by Date: Re: Writing NSString to a file
  • Next by Date: Added object to MOC, why isn't this a bug?
  • Previous by thread: Re: Writing NSString to a file
  • Next by thread: Re: Writing NSString to a file
  • Index(es):
    • Date
    • Thread