Skip to content

Instantly share code, notes, and snippets.

@giraphics
Created September 14, 2018 08:01
Show Gist options
  • Save giraphics/6bc603ed9a7a7b7eab18a8e894fc21d8 to your computer and use it in GitHub Desktop.
Save giraphics/6bc603ed9a7a7b7eab18a8e894fc21d8 to your computer and use it in GitHub Desktop.
#import <Cocoa/Cocoa.h>
// Ideally the interface should in .h
@interface Photo : NSObject {
NSString* caption;
NSString* photographer;
NSString* instrument;
NSString* musician;
}
@property (retain) NSString* instrument;
@property (retain) NSString* musician;
- (NSString*) caption;
- (NSString*) photographer;
- (void) setCaption: (NSString*)input;
- (void) setPhotographer: (NSString*)input;
@end
// the implementation should in .mm
@implementation Photo
@synthesize instrument;
@synthesize musician;
- (NSString*) caption {
return caption;
}
- (NSString*) photographer {
return photographer;
}
- (void) setCaption: (NSString*)input
{
[caption autorelease];
caption = [input retain];
}
- (void) setPhotographer: (NSString*)input
{
[photographer autorelease];
photographer = [input retain];
}
- (id) init
{
if ( self = [super init] )
{
[self setCaption:@"Default Caption"];
[self setPhotographer:@"Default Photographer"];
}
//NSLog ( @"\n Photo Alloced");
return self;
}
- (void) dealloc
{
//NSLog ( @"\n Photo Dealloced");
[caption release];
[photographer release];
[super dealloc];
}
@end
int main(int argc, const char * argv[])
{
Photo* obj1 = [[Photo alloc] init];
[obj1 setCaption:@"My First Musician"];
[obj1 setPhotographer:@"ABC"];
[obj1 setInstrument:@"Piano"];
[obj1 setMusician:@"Yanni"];
NSLog ( @"\n ===> %@, %@, %@, %@", [obj1 caption], [obj1 photographer], [obj1 instrument], [obj1 musician]);
//NSLog ( @"\n RETAINT CNT ===> %ld", [[obj1 caption] retainCount]);
NSLog ( @"\n RETAINT CNT ===> %ld", [obj1 retainCount]);
Photo* obj2 = [[Photo alloc] init];
[obj2 setCaption:@"My Second Musician"];
[obj2 setPhotographer:@"XYZ"];
[obj2 setInstrument:@"Flute"];
[obj2 setMusician:@"Andre Rieu"];
NSLog ( @"\n ===> %@, %@, %@, %@", [obj2 caption], [obj2 photographer], [obj2 instrument], [obj2 musician]);
[obj1 release];
[obj2 release];
return 0;
}
// Command to build in TERMINAL
/*clang -framework Cocoa Class.mm -o Class.app*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment