User Name
Password
AppleNova Forums » Programmer's Nook »

Simple Text Editor App.. Saving


Register Members List Calendar Search FAQ Posting Guidelines
Simple Text Editor App.. Saving
Thread Tools
mooty
Senior Member
 
Join Date: Jan 2005
Location: London, UK
 
2006-07-04, 06:58

Hi All,

I am trying to write my first text application in cocoa... I have got a working app from an Apple tutorial, however when saving, it uses the following:

Code:
- (NSData *)dataRepresentationOfType:(NSString *)aType { NSData *data; [self setString:[textView textStorage]]; data = [NSArchiver archivedDataWithRootObject:[self string]]; return data; }
Which saves the content as not plain text. Is there anyway I can change the above to save as plain text ?

Any help / direction would be great! Thanks,
  quote
chucker
 
Join Date: May 2004
Location: near Bremen, Germany
Send a message via ICQ to chucker Send a message via AIM to chucker Send a message via MSN to chucker Send a message via Yahoo to chucker Send a message via Skype™ to chucker 
2006-07-04, 07:49

Here's an IBAction that pulls up a save panel to ask the user for the destination, then stores a text file with the string textualOutput there:

Code:
234 - (IBAction)saveTextualOutput:(id)sender 235 { 236 #pragma unused(sender) 237 NSSavePanel * savePanel; 238 int runResult; 239 240 savePanel = [NSSavePanel savePanel]; 241 242 [savePanel setRequiredFileType:@"txt"]; 243 244 // TODO: auto-renaming, e.g. "filename 1.txt"? 245 runResult = [savePanel runModalForDirectory: 246 [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"] file: 247 NSLocalizedString(@"MenuTemperature Report", 248 @"MenuTemperature Report")]; 249 250 if (runResult == NSOKButton) 251 [[textualOutput dataUsingEncoding:NSUTF8StringEncoding] 252 writeToFile:[savePanel filename] atomically:YES]; 253 }
The main piece, of course, is the very last line, which calls - [NSString dataUsingEncoding:] on the NSString, then - [NSData writeToFile: atomically:] on the resulting NSData.
  quote
mooty
Senior Member
 
Join Date: Jan 2005
Location: London, UK
 
2006-07-05, 12:32

Cheers Chucker!!

How about this one?

I have an NSString variable like so:

Code:
NSString *filename = @"/Users/Me/whatever.txt";
I am trying to join that variable with a string like so:

Code:
NSString *newFilename = (@"file://%c", filename);
But all that does is give newFilename the value of filename. Obviously I am missing a trick here :-) any help is goooood! :-)

Thanks!
  quote
chucker
 
Join Date: May 2004
Location: near Bremen, Germany
Send a message via ICQ to chucker Send a message via AIM to chucker Send a message via MSN to chucker Send a message via Yahoo to chucker Send a message via Skype™ to chucker 
2006-07-05, 13:57

You could do:

Code:
NSString * newFilename = [@"file://" stringByAppendingString:filename];
Or, more like the way you wanted it:

Code:
NSString * newFilename = [NSString stringWithFormat:@"file://%@", filename];
%c is for C strings; %@ is for objects (including NSStrings).

There's also a stringByAppendingPathComponent method.

Lastly, what you're doing actually isn't necessary; filename is already a complete absolute path, at least in your particular example. You could turn it into a file:// URL if you wanted to, but it's not necessary (although I believe Apple is slowly trying to migrate over to using paths everywhere, but that shouldn't concern you until like 10.6).

If you want to go from @"/Users/Me/whatever.txt" (a path) to a URL, use [NSURL fileURLWithPath:@"/Users/Me/whatever.txt"]; , and to go backwards, use [NSURL path].
  quote
mooty
Senior Member
 
Join Date: Jan 2005
Location: London, UK
 
2006-07-05, 14:57

Quote:
Originally Posted by chucker
If you want to go from @"/Users/Me/whatever.txt" (a path) to a URL, use [NSURL fileURLWithPath:@"/Users/Me/whatever.txt"]; , and to go backwards, use [NSURL path].
EXACTLY what I needed! :-) thats shortened my code considerably too :-) Thanks Chucker!

Okay, next question :-) hehe

I have my variable and search term:

Code:
NSString *filename = @"User/Me/Files/whatever.jpg"; NSString *searchTerm = @"Me/Files";
How can I search filename for my searchTerm ?

Also, can I split filename up into seperate array items or something, by the / character ?

Thx again for any advice!
  quote
chucker
 
Join Date: May 2004
Location: near Bremen, Germany
Send a message via ICQ to chucker Send a message via AIM to chucker Send a message via MSN to chucker Send a message via Yahoo to chucker Send a message via Skype™ to chucker 
2006-07-05, 15:11

Code:
[filename rangeOfString:searchTerm]
will return an NSRange of the first occurrence. To check if there is at least one occurrence, you could try (I haven't tested this!):

Code:
if ([filename rangeOfString:searchTerm].location != NSNotFound) {}
There may be an easier way.
  quote
mooty
Senior Member
 
Join Date: Jan 2005
Location: London, UK
 
2006-07-05, 16:37

thanks chucker...

I have split my filename up into an array... how do i access the values? I tried arrayName[2] but that doesn't seem to go good! :-)
  quote
chucker
 
Join Date: May 2004
Location: near Bremen, Germany
Send a message via ICQ to chucker Send a message via AIM to chucker Send a message via MSN to chucker Send a message via Yahoo to chucker Send a message via Skype™ to chucker 
2006-07-05, 16:57

No, that would be for C arrays.

Code:
[someArray objectAtIndex:2];
is the Cocoa equivalent.

Code:
[someArray count];
gives you an integer of how many items the array has.

If you want to modify the array later on, you need to use NSMutableArray.
  quote
mooty
Senior Member
 
Join Date: Jan 2005
Location: London, UK
 
2006-07-06, 05:31

thanks Chucker!! You're being a great help here...

I currently have the following:

Code:
NSString *filenameString = @"User/Me/Blah/hello.html"; NSArray *theComponents = [filenameString pathComponents]; int i = 1; int theComponentsCount = [theComponents count]; NSString *newFilename = @""; while (i < theComponentsCount) { if ([theComponents objectAtIndex:i] == @"Blah") { //Do Nothing } else { // Add to my new string newFilename = [NSString stringWithFormat:@"%@ NO MATCH", newFilename]; } i = i + 1; }
theComponents splits my filename into seperates:
[0]/
[1]User
[2]Me
[3]Blah

etc, but my if clause never matches key [3] with the string "Blah"... why god why!????

All I am trying to achieve is if the filename contains "Blah", get rid of it so we are left with "/Users/Me/hello.html"

But it shouldnt matter where "Blah" is..
  quote
Brad
Selfish Heathen
 
Join Date: May 2004
Location: Zone of Pain
 
2006-07-06, 07:42

Quote:
Originally Posted by mooty
theComponents splits my filename into seperates:
[0]/
[1]User
[2]Me
[3]Blah
Are you sure about this? Double-check that it actually does what you think it does.
  quote
mooty
Senior Member
 
Join Date: Jan 2005
Location: London, UK
 
2006-07-06, 08:05

when i put the values of the keys into a textfield, they show as above...
  quote
chucker
 
Join Date: May 2004
Location: near Bremen, Germany
Send a message via ICQ to chucker Send a message via AIM to chucker Send a message via MSN to chucker Send a message via Yahoo to chucker Send a message via Skype™ to chucker 
2006-07-06, 08:29

First, technically, the Cocoa way of doing things is to use an NSEnumerator here, not an integer iterator. Shouldn't matter though and I can't be bothered to look it up right now

Second,
Code:
if ([theComponents objectAtIndex:i] == @"Blah")
is not an object comparison (I think this may, in fact, be your problem). You'd use
Code:
if ([[theComponents objectAtIndex:i] isEqualTo:@"Blah"])
or, since this is a string, better yet:
Code:
if ([[theComponents objectAtIndex:i] isEqualToString:@"Blah"])
Finally, and more relevant to your problem: I'd insert a few 'NSLog's to trace down what's going wrong. It's a very simple macro but can prove very powerful in tracking down mistakes. I have modified your code accordingly.

Code:
NSString *filenameString = @"User/Me/Blah/hello.html"; NSArray *theComponents = [filenameString pathComponents]; NSLog(@"Complete array: %@, with %i items", theComponents, [theComponents count]); int i = 1; int theComponentsCount = [theComponents count]; NSString *newFilename = @""; while (i < theComponentsCount) { NSLog(@"Current object: %@", [theComponents objectAtIndex:i]); if ([[theComponents objectAtIndex:i] isEqualToString:@"Blah"]) { //Do Nothing } else { // Add to my new string newFilename = [NSString stringWithFormat:@"%@ NO MATCH", newFilename]; } i = i + 1; }
Run it, and check console.log or STDOUT for log messages. If you're still unsure where the problem lies, post the output here.
  quote
chucker
 
Join Date: May 2004
Location: near Bremen, Germany
Send a message via ICQ to chucker Send a message via AIM to chucker Send a message via MSN to chucker Send a message via Yahoo to chucker Send a message via Skype™ to chucker 
2006-07-06, 08:37

Code:
NSString *filenameString = @"User/Me/Blah/hello.html"; NSArray *theComponents = [filenameString pathComponents]; NSEnumerator *ourEnumerator = [theComponents objectEnumerator]; NSObject *currentObject; NSLog(@"Complete array: %@, with %i items", theComponents, [theComponents count]); NSString *newFilename = @""; while (object = [enumerator nextObject]) { NSLog(@"Current object: %@", object); if ([[theComponents objectAtIndex:i] isEqualToString:@"Blah"]) { //Do Nothing } else { // Add to my new string newFilename = [NSString stringWithFormat:@"%@ NO MATCH", newFilename]; } }
Untested, mind you, but this is pretty much the equivalent code only using NSEnumerator.
  quote
mooty
Senior Member
 
Join Date: Jan 2005
Location: London, UK
 
2006-07-06, 08:50

that was it!! :-) IsEqualToString! Sorry I am still trying to get the hang of all this coming from PHP and .NET :-)
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools
Similar Threads
Thread Thread Starter Forum Replies Last Post
Illustrator Text Wrap BenP Genius Bar 1 2005-07-23 11:57
iPod to iTunes omem Genius Bar 5 2005-07-16 13:32
Pretty serious text bug: List text disappears! torifile Apple Products 8 2005-05-15 13:38
iDvd Problem, How to add Text Box zman2005 Genius Bar 2 2005-03-20 22:37
Does anyone Know how to add Text to idvd productions? zman2005 General Discussion 1 2005-03-19 13:36


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 01:25.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2024, AppleNova