#import <Foundation/Foundation.h>
@interface Dot: NSObject
{
int x;
int y;
}
@property int x, y;
@end;
@implementation Dot
@synthesize x, y;
@end
@interface Something : NSObject
{
int capacity;
Dot** array;
}
@property int capacity;
@property (nonatomic, assign) Dot** array;
- (Something*) init;
- (Something*) initWithCapacity : (int)n;
- (BOOL) isEmpty;
- (void) dealloc;
@end
@implementation Something
@synthesize array;
@synthesize capacity;
- (Something*) init
{
array = Nil;
capacity = 0;
return self;
}
-(Something*) initWithCapacity : (int)n
{
int i;
self = [super init];
array = (Dot**)malloc(n * sizeof(Dot*));
for (i = 0; i < n; ++i)
{
array[i] = [[Dot alloc] init];
array[i].x = i;
array[i].y = i * i;
}
capacity = n;
return self;
}
- (BOOL)isEmpty
{
return capacity == 0;
}
- (void) dealloc
{
int i;
for (i = 0; i < capacity; ++i)
{
[array[i] release];
}
free(array);
[super dealloc];
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Something* something = [[Something alloc] initWithCapacity: 10];
int i;
for (i = 0; i < something.capacity; ++i)
{
NSLog(@"array[%i] = %i, %i", i, something.array[i].x, something.array[i].y);
}
[something release];
Something* test = [[Something alloc] init];
if (![test isEmpty])
test.array[0].x = 1;
[test release];
[pool drain];
return 0;
}
Wednesday, December 30, 2009
Objective-C. Program not from the book
First time something that was not in the book:
Labels:
Objective-C
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment