To compile Objective-C code on a Mac you need to have XCode tools installed. XCode is supplied free with the OS-X Leopard DVD. It is located under the optional installs folder. Once installed you should be able to compile the Objective-c code below using the command line.
The Unix command line is available via the Terminal application you can navigate to it via Applications->Utilities->Terminal.
The code below will fetch a html file from the web and print it's output. It also has some basic error handling to catch any Errors or Exceptions which may occur and print them. This makes sure any http url's the user may provide as an argument will be handled correctly should an error occur.
//
// Created by devnull[at]london.com on Sept 2008.
// Copyleft (c) 2008. some rights reserved.
// macateeny.blogspot.com
//
// Compile from the command line with command below:
// gcc urlRead.m -Wall -o urlRead -framework Foundation
//
#import <Foundation/Foundation.h>
// main entry point of our urlRead foundation tool with
// C style argument counter and vector parameters
int main(int argc,char *argv[])
{
// allocate a memory pool for our NS Objects
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// try catch block to catch exceptions generated by its contents
@try
{
// declare 2 NSString Obj pointers
NSString *str, *urlStr ;
// declare character pointer initialise it with argument vector [1]
// we are reading 1st argument/parameter provided to the application
char *urlChr = argv[1];
if(urlChr==NULL) {
// no url passed set it to default url string @"string"
urlStr = @"http://www.kipling.org.uk/poems_if.htm";
} else {
// convert c char string to Objective-c NSString
urlStr = [NSString stringWithCString:urlChr];
}
// init NSURL with url string and store it in an NSData Object
NSData *dataStr = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
str = [[NSString alloc] initWithData:dataStr encoding:NSASCIIStringEncoding];
// if the string has data display it
if( str!=nil ) {
// prints log timstamp NSLog(@"%@",str);
printf([[NSString stringWithFormat:@"%@",str] UTF8String]);
}
}
@catch(NSException *error) {
// An Exception occured print the value in the NSException error Object
NSLog( @"Error Exception:%@ cause:%@",[error name],[error reason]);
}
@finally {
// unleash the allocated pool
[pool release];
}
// The app is terminated
return 0;
}
Save the content above to a urlRead.m file or download it here.
Then compile it from the command line using:gcc urlRead.m -Wall -o urlRead -framework Foundation
Now once compiled it will create a urlRead binary which you can execute.
To run it type ./urlRead from the command line.
Finally for an additional bit of fun you can send the html text to the OS X text-to-speech engine and yes your mac will read you an inspiring poem. This script may even change your life so try it out../urlRead | textutil -format html -convert txt -stdin -stdout | say
Enjoy...
Learn Objective-C Basic's in minutes
Labels: NSString to Char, Objective-C, printf NSTring, XCode Code snippetPosted by devnull at 13:09
Subscribe to:
Post Comments (Atom)
Design and icons by N.Design Studio | A Blogger Template by Blog and Web
Apple, the Apple logo, iTunes, Cocoa, GarageBand, Mac OS, Xcode and Mac are trademarks of Apple Computer, Inc., registered in the U.S. and other countries. OpenGL is a registered trademark of SGI.Unix® is a trademark of the Open Group. BSD is a registered trademark of UUnet Technologies, Inc.

Blog related articles and applications by devnill is licensed under a Creative Commons Attribution 2.0 UK: England & Wales License.
Based on a work at macateeny.blogspot.com.
Permissions beyond the scope of this license may be available at macateeny.blogspot.com.
0 comments:
Post a Comment