Tuesday, June 5, 2007

Pulling in RTF Information in Objective-C


So, a view pointed out that you can't pull in an RTF file because it's in binary.

I did some playing around and found a class derived from a string class called NSAttributedString.

If you want to pull in RTF information, this string does it. I successfully used this to pull in every line from an RTF in the application I posted to the demo server. I still haven't fixed it yet. But hey, RTF's are a possibility! :-)

To get the string data, I just created a new NSAttributedString and called the following init method.
  • [[NSAttributedString alloc] initWithPath:@"location.rtf" documentAttributes:nil];
Then I called the string property and returned a standard string.

The rest I solved using the way I demonstrated before.

Happy Coding :-)

Sunday, June 3, 2007

Reading in individual lines from a file in Objective-C

Okay, I had someone comment on the blog saying that they can get in the string information, they just need to filter out each line. This can be done easily. I also have an application up on the demo server so you can download the source.

It took me a little bit to figure out what the key was to symbolize a new line but it turns out to be a simple \n. So, this is what you have to do:

Read in your text to an NSString object, like so:
  • NSString *info = [NSString stringWithContentsOfFile:@"FilePath.txt"];
Or like this:
  • NSString *info = [[NSString alloc] initWithContentsOfFile:@"FilePath.txt"];
Then you split the string into an array list by doing the following.
  • NSArray *arrayOfLines = [info componentsSeparatedByString:@"\n"];
Now, you will have an array of all the lines in the file. This might actually be one of the slowest means of doing things, since the file has to be read into memory, but it works. And if you have some settings that need to be loaded in through a file, this might just be your thing.

The only bug I had with this was reading in a rich text file. A standard text file works fine doing this method. I hope this helps out. :-)

Saturday, June 2, 2007

Interacting with the command line in Objective-C


Before, I thought that you could interact with the command line only through Apple Script. I found a different way of doing it. If you have a script file for command line, you can access it and utilize it by using one of the following methods in Objective-C (Note, These are C functions)
  • int result = system("Your Command Here");
If you want to get the output from this call, you will have to pipe the output to a file and read it in using Objective-C. You can pipe the output like this:
  • int result = system("Your Command Here > File.txt");
I'm going to try and get a demo up for this to better understand this sometime this weekend.

The result contains from my understanding a 1 if it completed without error or a 0 if it had an error, I could be wrong. All I know is if you handle the result like this.
  • if (!result) {}
Your code will fall through to this method if it had an error.

Now, if you have a script that can run in the background, you can use the following C command
  • FILE *fileObject = popen("Your Command Here");
As shown above, this will return a FILE Object. This is a C object. This contains all the information being sent to and returned from the console, so if you need to get back information, you will have to use some C functions to print it out.