Objective-C 2.0 attribute Property concise tutorial

Source: Internet
Author: User

Objective-C 2.0AttributePropertyThe concise tutorial is the content to be introduced in this article, mainly to learn and understandObjective-C 2.0.Objective-C 2.0ProvidedProperty. It greatly simplifies the process of creating Read and Write Functions for data members. More importantly, it provides a simpler and easier-to-understand way to access data members.

Let's take a look at the header file of the Book class declared under Objective-C 1. x:

 
 
  1. ////  Book.h #import <Cocoa/Cocoa.h>    
  2. @interface Book : NSObject {   
  3. NSString *title;   
  4. NSNumber* numofpages;  
  5. }   
  6. - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num;   
  7. - (NSString*) title;  
  8. - (void) setTitle:(NSString*)newtitle;   
  9. - (NSNumber*) numofpages;  
  10. - (void) setNumofpages:(NSNumber*)newnumofpages;   
  11. - (NSString*) summary;   
  12. end 

In Objective-C 2.0, we can declare a property with the same name as a data member to save the declaration of a read/write function. The Code is as follows:

 
 
  1. ////  Book.h #import <Cocoa/Cocoa.h>    
  2. @interface Book : NSObject {   
  3. NSString *title;   
  4. NSNumber* numofpages;  
  5. }   
  6. - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num;   
  7. @property (retain) NSString* title;@property (retain) NSNumber* numofpages;   
  8. @property (readonly) NSString* summary;   
  9. @end 

We declare a property for each data member. Even if the Book class does not contain the summary data member, we can declare a property named summary. The syntax for declaring property is:

@ Property (parameter) type name;

The parameters here are mainly divided into three types: readwrite/readonly), setter semantic assign/retain/copy), and atomicitynonatomic ).

Assign/retain/copy determines how to assign a new value to a data member. We use readonly when declaring summary propery, which means that the client can only read this property. The default value of atomicity is atomic, And the READ function is atomic.

Next let's take a look at the implementation file in Objective-C 1.x:

 
 
  1.  ////  Book.m #import "Book.h"    
  2.  @implementation Book //  
  3.  @synthesize title;   
  4.  - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num{   
  5.  self = [super init];   
  6.  if(nil != self) {   
  7.   [self setNumofpages:num];    
  8.   [self setTitle:booktitle];   
  9.   }   
  10.  return self;  
  11. }   
  12. - (NSString*) title{   
  13. return title;  
  14. }   
  15. - (void) setTitle:(NSString*)newtitle{   
  16. [title release];   
  17. title = [newtitle retain];  
  18. }   
  19. - (NSString*) description{   
  20. return title;  
  21. }   
  22.  - (NSNumber*) numofpages{   
  23.  return numofpages;  
  24.  }   
  25.  - (void) setNumofpages:(NSNumber*)newnumofpages{   
  26.  [numofpages release];   
  27.  numofpages = [newnumofpages retain];  
  28.  }  
  29.   -(NSString*) summary{   
  30.   NSString* retstr = [[NSString alloc]initWithFormat:@"Title: %@, Number of pages: %@",  
  31.                           title, numofpages];   
  32.             [retstr autorelease];   
  33.             return retstr;  
  34.         }   
  35.    - (void) dealloc{   
  36.    [numofpages release];   
  37.    [title release];   
  38.    [super dealloc];  
  39. }   
  40. @end 

In Objective-C 2.0, the implementation file can be changed as follows:

 
 
  1.  ////  Book.m #import "Book.h"    
  2.  @implementation Book   
  3.  @synthesize title;@synthesize numofpages;   
  4.  - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num{   
  5.       self = [super init];   
  6.       if(nil != self) {    
  7.       [self setNumofpages:num];    
  8.       [self setTitle:booktitle];   
  9.    }   
  10.    return self;  
  11.  }   
  12.  - (NSString*) description{ return title;  
  13. }   
  14. -(NSString*) summary{   
  15. NSString* retstr = [[NSString alloc]initWithFormat:@"Title: %@, Number of pages: %@",    
  16.                                    title, numofpages];   
  17.          [retstr autorelease];   
  18.    return retstr;  
  19.  }   
  20. - (void) dealloc{   
  21. [numofpages release];   
  22. [title release];   
  23. [super dealloc];  
  24. }   
  25. @end 

We can see that the Read and Write Functions of the data member title and numofpages no longer exist. Instead, they are replaced by two rows @ synthesize, which enables the compiler to automatically generate the Read and Write functions when we do not provide the Read and Write Functions.

With property defined, the client can use book. title to replace [book title]. This syntax is more intuitive and concise than previously.

The 16-17 lines of code in the implementation file can be modified as follows:

 
 
  1. self.numofpages = num;  
  2. self.title = booktitle 

Note: many people may easily forget the self in the above two lines of code. In this case, the Read and Write Functions generated by the machine will not be called. Instead, the direct pointer assignment will cause memory leakage.

The client code is as follows:

 
 
  1. #import <Foundation/Foundation.h> 
  2. #import "Book.h" int main (int argc, const char * argv[]) {  
  3.  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    
  4.  NSString* name = [[NSString alloc] initWithString:@"Harry Porter"];   
  5.  NSNumber* number = [[NSNumber alloc] initWithInt:100];   
  6.  Book *book = [[Book alloc] initWithTitle:name andNumofpages:number];   
  7.  [number release];   
  8.  [name release];    
  9.  book.title = @"Twilight";   
  10.  book.numofpages = [NSNumber numberWithInt:200];   
  11.  NSString* str = book.summary;   
  12.  NSLog(@"summary: %@", str);   
  13.  [book release];    
  14.  [pool drain];      
  15.  return 0;  

Summary:Objective-C 2.0AttributePropertyThe content of the concise tutorial is complete. I hope you can learn this article to help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.