
Kiichi and I really liked the demo from apple about the iPhone SDK.
If you haven’t seen it, check it out@
http://www.apple.com/quicktime/qtv/iphoneroadmap/
We at ObjectGraph think the new app store feature will level the playing field for all developers, big(EA) and small(OG). Now its up to developers to create new applications and make them success stories. So We started developing in Objective C. With its kinda wierd syntax coming from C#, Java, ActionScript and Python, but here is my first try anyway
I am creating a MyPoint class with a distance function that takes a reference of an another point.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #import <Cocoa/Cocoa.h> @interface MyPoint : NSObject { int x; int y; } -(void) print; -(void) setX:(int) a; // void setX(int a); -(void) setY:(int) b; -(int) getX; -(int) getY; -(double) distance:(MyPoint*) p; @end |
Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #import "MyPoint.h" #import <stdio.h> #import <math.h> @implementation MyPoint -(void) print { printf("(%d,%d)",x,y); } -(void) setX:(int) a { x=a; } -(void) setY:(int) b { y=b; } -(int) getX { return x; } -(int) getY { return y; } -(double) distance:(MyPoint*) p; { return sqrt((x-[p getX])*(x-[p getX])+(y-[p getY])*(y-[p getY])); } @end |
Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #import <Foundation/Foundation.h> #include <stdio.h> #import "MyPoint.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; MyPoint *point1=[[MyPoint alloc] init]; [point1 setX:10]; [point1 setY:20]; [point1 print]; MyPoint *point2=[[MyPoint alloc] init]; [point2 setX:10]; [point2 setY:30]; [point2 print]; printf("Distance: %lf\n", [point1 distance:point2]); [point1 release]; [point2 release]; [pool drain]; return 0; } |
Related posts:
- NSMutableDictionary Example – iPhone App Development Basics in Objective-C Here is a simple example to add elements in a...
- Encrypting / Decrypting / Base64 Encode / Decode in iPhone Objective-C I went through websites to find good tricks to send...
- iPhone SDK 3.0 – Playing with Game Kit – Part 1 My first series of articles explaining how to use Game...
- iPhone SDK 3.0 – Playing with Map Kit – Part 3 This is the 3 part of me Playing with Map...
- [iPhone Development] How to Parse HTML A few weeks ago, I was looking for a simple...
Related posts brought to you by Yet Another Related Posts Plugin.





















One Response
Even easier
#import
@interface MyPoint : NSObject {
NSinteger x;
NSinteger y;
}
@property (nonatomic) NSInteger x;
@property (nonatomic) NSInteger y;
-(void) print;
-(double) distance:(MyPoint*) p;
@end
#import “MyPoint.h”
#import
#import
@implementation MyPoint
@synthesize x,y;
-(void) print
{
NSLog(@”(%d,%d)”,x,y);
}
-(double) distance:(MyPoint*) p;
{
return sqrt((x-p.X)*(x-p.X)+(y-p.Y)*(y-p.Y));
}
@end
#import
#include
#import “MyPoint.h”
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MyPoint *point1=[[MyPoint alloc] init];
point1.X =10;
point1.Y =20;
[point1 print];
MyPoint *point2=[[MyPoint alloc] init];
point2.X = 10;
point2.Y = 30;
[point2 print];
printf(”Distance: %lf\n”, [point1 distance:point2]);
[point1 release];
[point2 release];
[pool return];
return 0;
}
I *think* NSInteger is just a macro for the standard int size.
Also on an iphone, just release the pool, drain doesnt work. If I’m not you might need to cast them when using NSInteger. I forgot.