// the following directive import is the same as include but it ensures
// that the file is included only once.
//
// The following line includes the headers for all the classes in
// the Foundation framework. The headers are precompiled, so this
// approach is not as computationally intensive as it sounds.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Variable array is declared here. It is a pointer to an instance
// of NSMutableArray. Note that no array exists yet.
NSMutableArray *array;
//Here the array is created.
array = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < 10; i++)
{
NSNumber *newNumber = [[NSNumber alloc] initWithInt:(i * 3)];
// The array does not make copies of the NSNumber objects.
// Instead it simply keeps a list of pointers to the
// NSNumber objects.
[array addObject:newNumber];
}
for (i = 0; i < 10; i++)
{
NSNumber *numberToPrint = [array objectAtIndex:i];
// %@ - the object gets send the message description,
// and the string it returns replaces %@ in the string.
NSLog(@"The number at index %d is %@", i, numberToPrint);
}
[pool drain];
return 0;
}
Saturday, March 27, 2010
Objective-C. Sample with comments
Here is a simple Objective-C program where almost all lines have comments:
Labels:
Objective-C
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment