Show Menu
Cheatography

Objective-C Cheat Sheet by

Assumes "Automatic Reference Counting" (ARC) is enabled.

Class Interfaces

@interface Shape : NSObject
@property (assign) NSPoint center;
@property (strong) NSColor* color;
@end
 
@interface Rectangle : Shape
@property (assign) NSSize size;
@end
 
@interface Circle : Shape
@property (assign) CGFloat radius;
@end
 

Class Implem­ent­ation

@imple­men­tation Shape
@synth­esize centre;
@synth­esize color;
- (void)draw {
[color set];
}
@end
 
@imple­men­tation Rectangle
@synth­esize size;
- (void)draw {
[super draw];
[NSBez­ierPath fillRe­ct:­NSM­ake­Rec­t(s­elf.ce­ntre.x - size.width / 2, self.c­entre.y - size.h­eight / 2, size.w­idth, size.h­eig­ht)];
}
@end
 
@imple­men­tation Circle
@synth­esize radius;
- (void)draw {
[super draw];
[[NSBe­zie­rPath bezier­Pat­hWi­thO­val­InR­ect­:NS­Mak­eRe­ct(­sel­f.c­entre.x - radius, self.c­entre.y - radius, radius2, radius2)] fill];
}
@end
 

Class Use

- (void)­dra­wRe­ct:­(NS­Rec­t)rect {
rect = [self bounds];
[[NSColor cyanColor] set];
[NSBez­ierPath fillRe­ct:­rect];
 
Rectangle* rectangle = [[Rect­angle alloc] init];
rectan­gle.size = NSMake­Siz­e(300, 200);
rectan­gle.centre = NSMake­Poi­nt(­NSM­idX­(rect), NSMidY­(re­ct));
rectan­gle.color = [NSColor whiteC­olor];
[rectangle draw];
 
Circle* circle = [[Circle alloc] init];
circle.radius = 60;
circle.centre = rectan­gle.ce­ntre;
circle.color = [NSColor redColor];
[circle draw];
}

Result

               
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Sublime Text 2 - Shortcuts (Verbose Mac) Keyboard Shortcuts
          Amethyst Keyboard Shortcuts
          My Mac Productivity Keyboard Shortcuts