Example code programming Objective-C with Cocoa in Xcode and Interface Builder (Leopard)
13: A simple test rig
Simple test rig for examining Cocoa classes and their methods.
We often need to make a hands on examination of the methods of a given class.
This is how I tend to do it using XCode
In XCode do File->New Project.
Choose Cocoa Application.
Save it as 013-Test-Rigs.
In the XCode window open 013-Test-Rigs->Other Sources->main.m
Remove the line return NSApplicationMain(argc, (const char **) argv);
Save, we'll add code later
Go into Project->Edit Project Settings.
Select the General section.
Project Format: Xcode 3.1 compatible
Base SDK for All Configurations : Mac OS X 10.5
Select the Build section.
Code Generation: I'm never sure whether to have
Instruction Scheduling = Power PC G4 or G5.
Typically I choose G5.
Objective C Garbage Collection: Required
Interface Builder: drag Menu onto the Menu Bar and rename its Menu Item
Interface Builder: drag Menu onto the Menu Bar and rename its Menu Item
There now follows an example of experimenting with some NSString methods.
// main.m // 013-Test-Rigs // #import <Cocoa/Cocoa.h> int main(int argc, char *argv[]) { // boolValue NSString *myNSString = @"T"; NSLog(@"NSString = %@",myNSString); if ([myNSString boolValue]) { NSLog(@"boolValue of %@ = Yes",myNSString); } else { NSLog(@"boolValue of %@ = No",myNSString); } // end if myNSString = @"the quality of mercy"; if ([myNSString canBeConvertedToEncoding: NSASCIIStringEncoding ]) { NSLog(@"%@ canBeConvertedTo NSASCIIStringEncoding = Yes",myNSString); } else { NSLog(@"%@ canBeConvertedTo NSASCIIStringEncoding = No",myNSString); } // end if // capitalizedString myNSString = @"how now brown Cow"; NSString *myCapitalisedString = [myNSString capitalizedString]; NSLog(@"capitalized %@ = %@",myNSString,myCapitalisedString); unsigned short myChar = [myCapitalisedString characterAtIndex:4]; NSLog(@"myChar = %d",myChar); } // end main
Build and run.