book.h//Obj-c Learning 1////Created by Itfanr on 14/11/30.//Copyright (c) 2014 ITFANR. All rights reserved.//#import <Foundation/Foundation.h> @interface book:nsobject@property float price;-(ID) Initwithprice: (float) Price; @end
person.m// obj-c Learning 1//// created by itfanr on 14/11/ 30.// copyright (c) 2014 year itfanr. all rights reserved.//#import "Person.h" #import "Book.h" @implementation person:nsobject@synthesize book = _book ;-(void) Setbook: (book *) book{ if (_book != book) { [_book release] ; _book = [book retain] ; }} -(Book *) book{ &NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;_BOOK&NBSP;;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP, #pragma mark initialization-(ID) initwithage: (int) Age { self = [super init] ; _age = age ; return self;} #pragma mark destroy functions carbon- (void) dealloc{ [_book release] ; nslog (@ "person is dealloced" ); [super dealloc] ;} @end
main.m//Obj-c Learning 1////Created by Itfanr on 14/11/29.//Copyright (c) 2014 ITFANR. All rights reserved.//#import <Foundation/Foundation.h> #import "Person.h" #import "Book.h" int main (int argc, const char * argv[]) {Book *book1 = [[book alloc]initwithprice:4.5]; person * p = [[Person alloc] initwithage:18]; P.book = Book1; [Book1 release]; [P release]; return 0;}
The output is:
2014-11-30 22:38:11.427 obj-c Learning 1[1228:911238] book is dealloced!2014-11-30 22:38:11.428 obj-c learning 1[1228:911238] person is Deallocedprogram ended with exit code:0
No memory leaks were found.
Objective-c Counter Learning