Re: How to write a string to a file
Re: How to write a string to a file
- Subject: Re: How to write a string to a file
- From: Ken Thomases <email@hidden>
- Date: Mon, 18 Aug 2008 23:43:37 -0500
On Aug 18, 2008, at 10:28 PM, David wrote:
Why isn't this already in NSString?
I would sooner suggest removing -writeToFile:... from NSString than
adding -appendToFile:.
At some point you get a many-to-many explosion of combinations
(NString, NSData, NSArray, NSDictionary, NSValue, NSNumber, etc.)
times (NSFileHandle, NSStream, NSPort, etc.). Much better to have a
many-to-one-plus-one-to-many design. I would guess that things like -
writeToFile: are a legacy from early days which now can't be removed
easily.
NSString includes lots of methods to work with paths, URLs, etc.
This breaks
encapsulation. It does not seem to be good design for NSString to
have this
unique function dealing with paths. That should be in another class.
Actually, those methods are not in the NSString class. They're in a
category on that class, NSString(NSStringPathExtensions) in
NSPathUtilities.h. So, I don't think it breaks encapsulation.
Basic methods should exist somewhere for writing primitive types to
a file.
Agreed.
However, part of the answer to your concerns is that Objective-C is
still C and you can use all of the C libraries and system calls. The
main advantage of NSFileHandle over stdio is its asynchronous
capabilities and integration with run loops. Also, if your data is
most easily obtained in an NSData. If you don't need those
capabilities, consider using stdio or another alternative that better
matches your needs.
Also, don't forget that you can extend NSFileHandle with whatever
methods you like using categories.
NSOutputStream provides the following method,
- (NSInteger)write:(const uint8_t *)*buffer* maxLength:(NSUInteger)
*length*
Why doesn't it use NSData? What's the point of having the NSData
construct
if it isn't even used?
It probably should have a method accepting an NSData. However,
NSData does have -bytes and -length methods, which makes invoking the
above method trivial if you have an NSData.
And, of course, the above method is useful if you have a non-object
type to write to the stream. You don't need to wrap it in an NSData.
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden