Pages

Tuesday, June 15, 2010

NSString. Write to file. Read from file.

This small program below show the simplest way to create an ASCII text file, write a string into it and read the string from the file:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString* lastLine = @"Beauty is truth, truth beauty";
NSString* fileName = @"GrecianUrn.txt";
NSString* homeDir = NSHomeDirectory();

NSString* fullPath = [homeDir stringByAppendingPathComponent:fileName];

NSError* error = nil;
[lastLine writeToFile:fullPath atomically:NO encoding:NSASCIIStringEncoding error:&error];

NSStringEncoding encoding;
NSString* contents = [NSString stringWithContentsOfFile:fullPath usedEncoding:&encoding
error:&error];

NSLog(@"Content of '%@': %@", fileName, contents);

[pool drain];
return 0;
}

Saturday, June 12, 2010

Objective-C. Forwarding Messages.

Forwarding messages. It's an absolutely new feature for me in Object-Oriented Programming. I found this thing working in Objective-C (thanks to the book - on the left).
#import <Foundation/Foundation.h>

@interface Cat : NSObject
- (void) meow;
@end

@interface Dog : NSObject
- (void) woof;
@end

@implementation Cat

- (void) meow
{
NSLog( @"%@: Meow", [self className] );
}

@end

@implementation Dog

- (void) woof
{
NSLog( @"%@: woof", [self className] );
}

- (NSMethodSignature*) methodSignatureForSelector:(SEL)selector
{
NSString* name = NSStringFromSelector( selector );
NSLog( @"-[%@: methodSignatureForSelector: %@]", [self className], name );

if ([Cat instancesRespondToSelector: selector])
return [Cat instanceMethodSignatureForSelector: selector];

return [super methodSignatureForSelector: selector];
}

- (void) forwardInvocation:(NSInvocation *)anInvocation
{
NSString* name = NSStringFromSelector( anInvocation.selector );
NSLog( @"-[%@: forwardInvocation: %@]", [self className], name );

id theCat = [[Cat alloc] init];
NSLog( @"Forwarding '%@' to %@", name, theCat );
[anInvocation invokeWithTarget: theCat];
[theCat release];
}

@end

int main()
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

id dog = [[Dog alloc] init];
[dog woof];
[dog meow];
[dog release];

[pool drain];
return 0;
}
And here is the output:

Running…
Dog: woof
-[Dog: methodSignatureForSelector: meow]
-[Dog: forwardInvocation: meow]
Forwarding 'meow' to
Cat: Meow

Friday, June 11, 2010

64-bit Objective-C

On 64-bit you can use properties to generate both accessor methods and the instance variable itself. This code compiles and works on Snow Leopard:
#import <Foundation/Foundation.h>

@interface Test : NSObject
@property (retain) NSString *name;
@end

@implementation Test
@synthesize name;
@end

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Test *test = [[Test alloc] init];
[test setName: @"TheName"];
NSLog(@"Name is %@\n", [test name]);
[pool drain];
return 0;
}

Here is the console:

Program loaded.
run
[Switching to process 2963]
Running…
2010-06-12 00:42:30.308 Test64[2963:a0f] Name is TheName

Debugger stopped.
Program exited with status value:0.

Tuesday, June 8, 2010

Cocoa and Objective-C Up and Running.

I've read already many book about Objective-C and didn't think that I can find something new in this one. I was wrong. It's really a great book.
For example:
[object method];
[object methodWithInput: inputValue];
Objective-C is a dynamic language, so technically you are not calling a method directly when you use this syntax; you are actually sending a message. Calling a method is basically issuing an order, whereas sending a message is more like making a request.

Friday, June 4, 2010

Objective-C classes at runtime

Great article:
Obj-C Optimization: Allocation Basics & Foundation

AppleScript. Hello, World!

You can find AppleScript Editor in the folder Utilities of Applications:

Type "Hello":

Press Compile and then Run - see "Hello" in the result window:

Let's "complicate" the script. Type:

display alert "Hello"

Press Compile and Run. You see a simple alert with one Ok button on the screen:


Let's make an application from it. In the File menu select "Save as". You will see this save dialog:


Select "Application" in the File Format combo box. Set a name, for example Hello. Set a path - I set the desktop:

Now on the desktop I see my Hello application. When I click on it, I see my alert:
In the end of this post I'd like to show a less useless and a bit funny example. You can use AppleScript editor as a calculator. Simply type something you need to calculate, compile and run it:
Of course you can arrange it as a script:


display alert 45 + 16 * 2 / 3



This book below contains a nice section about AppleScript: