Dark Horse programmer--"Dark Horse Video Notes" description method of OC Language Foundation

Source: Internet
Author: User

General usefulness of the 1.description method
   1:  //address of the pointer variable
   2:      NSLog (@ "%p", &p);
   3:      //Address of Object
   4:      NSLog (@ "%p", p);
   5:      //< Class Name: Object address >
   6:      NSLog (@ "%@", p);

   1:  class];
   2:      
   3:      //1. The +description method that invokes the class
   4:      //2. Get the return value of the +description method (NSString *) displayed on the screen
   5:      NSLog (@ "%@", c);

Java-like ToString ()

2, the general use of description method attention points
   1:  //By default, when using NSLog and%@ output objects, the result is:< class name: Memory address >
   2:      
   3:      //1. The-description method that invokes the object P
   4:      //2. Get the return value of the-description method (NSString *) displayed on the screen
   5:      //The 3.-description method returns "class name + memory address" by default
   6:      NSLog (@ "%@", p);
   7:      
   8:      //person *p2 = [[Person alloc] init];
   9:      //nslog (@ "%@", p2);
  10:      
One   :      //nsstring *name = @ "Rose";
  12:      
  :      //nslog (@ "My name is%@", name);
  14:      
  :  person    *p2 = [[Person alloc] init];
  :      p2.age = 25;
  :      @ "Jake";
  18:      
  :      NSLog (@ "%@", p2);

3. Usage of the Description method
   1:  //determines the output of the instance object
   2:  //-(NSString *) Description
   3:  //{
   4:  ////The    following code will cause a dead loop
   5:  ////    NSLog (@ "%@", self);
   6:  //    return [NSString stringwithformat:@ "age=%d, name=%@", _age, _name];
   7:  //    //return @ "3424324";
   8:  //}
   9:  
  Ten:  //determines the output of the class object
One   :  + (NSString *) description
  :  {
  :      return@ "ABC";
  :  }

Dark Horse programmer--"Dark Horse Video Notes" description method of OC Language Foundation

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.