IPhone app learning notes object initialization

Source: Internet
Author: User

IPhone applicationsLearning notesObjectThe initialization of is the content to be introduced in this article, mainly to learnObjectThe initialization content. Code is included for easy learning!

1. About self = [super init]

The first code to run is [super init], which enables the superclasses to complete their own initialization work. The class inherited from the root NSObject class calls the superclass initialization method, so that NSObject can perform any operation required, so that the object can respond to the message and process the reserved counter. The class inherited from other classes calls the superclass initialization method, so that the subclass has the opportunity to implement its own new initialization.

The distance between the memory location of the instance variable and the hidden self parameter is fixed. If a new object is returned from the init method, you must update self, so that the reference of any instance variable can be mapped to the correct memory location, which is why we need to assign values in the form of self = [super init.

The value assignment affects the self value in the init method, without any content beyond the scope of the method.

2. Convenient initialization functions

Many classes contain convenient initialization functions, which are used to complete initialization methods for some additional work.

NSString class:

 
 
  1. -id) init; 

The above method initializes a new Null String. This method is of little use for immutable NSString classes. However, you can allocate and initialize a new NSMutableString Class Object and start saving characters to the object. You can use this object as follows:

 
 
  1. NSString *emptyString = [[NSString alloc] init]; 

The above code returns an empty string

Of course, you can also accept formatted strings and output the same formatted results.

 
 
  1. string  = [[NSString alloc] initWithFormat:@"%d or %d", 25, 624]; 

The code above returns a string with a value of 25 or 624 ";

What's more powerful is that you can open a text file in the specified path, read the file content too much, and use the File Content to initialize a string.

 
 
  1. string  = [[NSString alloc] initWithContentsOfFile: @"words.txt"]; 

To create an NSMutableArray array, replaceObjectAtIndex: withObject is a simple method. This method is most suitable for setTire: atIndex.

To use the replaceObjectAtIndex: withObject: method, an object that can be replaced must exist at the specified index position.

The new NSMutableArray array does not contain any content. Therefore, some objects need to be used as placeholders. The NSNull class objects are ideal for this purpose. Therefore, we add four NSNull objects to the array.

The code for attaching Tire. m is as follows:

 
 
  1. #import "Tire.h"  
  2. @implementation Tire  
  3. - (id) init  
  4. {  
  5. if (self = [self initWithPressure: 34  
  6. treadDepth: 20]) {  
  7. }  
  8. return (self);  
  9. } // init  
  10. - (id) initWithPressure: (float) p  
  11. {  
  12. if (self = [self initWithPressure: p  
  13. treadDepth: 20.0]) {  
  14. }  
  15. return (self);  
  16. } // initWithPressure  
  17. - (id) initWithTreadDepth: (float) td  
  18. {  
  19. if (self = [self initWithPressure: 34.0  
  20. treadDepth: td]) {  
  21. }  
  22. return (self);  
  23. } // initWithTreadDepth  
  24. - (id) initWithPressure: (float) p  
  25. treadDepth: (float) td  
  26. {  
  27. if (self = [super init]) {  
  28. ppressure = p;  
  29. treadDepth = td;  
  30. }  
  31. return (self);  
  32. } // initWithPressure:treadDepth:  
  33. - (void) setPressure: (float) p  
  34. {  
  35. ppressure = p;  
  36. } // setPressure  
  37. - (float) pressure  
  38. {  
  39. return (pressure);  
  40. } // pressure  
  41. - (void) setTreadDepth: (float) td  
  42. {  
  43. treadDepth = td;  
  44. } // setTreadDepth  
  45. - (float) treadDepth  
  46. {  
  47. return (treadDepth);  
  48. } // treadDepth  
  49. - (NSString *) description  
  50. {  
  51. NSString *desc;  
  52. desc = [NSString stringWithFormat:  
  53. @"Tire: Pressure: %.1f TreadDepth: %.1f",  
  54. pressure, treadDepth];  
  55. return (desc);  
  56. } // description  
  57. @end // Tire 

Summary:IPhone applicationsStudy NotesObjectThe initialization is complete. I hope this article will help you!

Related Article

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.