zoukankan      html  css  js  c++  java
  • [Objective-c 基础

    A. 实例对象打印-description

    1.当使用NSLog函数并且使用%@占位符的时候,会调用对象的-description方法

    2.拿到-description的返回值,显示到console中
    默认返回指针地址
     1 @implementation Person
     2 
     3 - (NSString *)description
     4 {
     5     return [NSString stringWithFormat:@"age = %d, name=%@", self.age, self.name];
     6 }
     7 
     8 @end
     9  
    10 int main(int argc, const char * argv[]) {
    11     @autoreleasepool {
    12         Person *p = [[Person alloc] init];
    13         p.age = 33;
    14         p.name = @"张三";
    15         NSLog(@"%@", p);
    16     }
    17     return 0;
    18 }
     out:
    2014-11-13 17:08:53.553 07-description方法[7765:303] age = 33, name=张三
    Program ended with exit code: 0
     
     
    B.类对象+description
    提供给类对象使用的对象打印方法,默认返回类名
     1 + (NSString *)description
     2 {
     3     return @"这是一个Person类";
     4 }
     5  
     6 int main(int argc, const char * argv[]) {
     7     @autoreleasepool {
     8         Class c = [Person class];
     9         NSLog(@"%@", c);
    10     }
    11     return 0;
    12 }
    2014-11-13 17:12:15.890 07-description方法[7791:303] 这是一个Person
    Program ended with exit code: 0
     
    C.NSLog的更多功能
    1.占位符%p:打印指针地址,重写了实例对象的-description方法之后,可以使用此占位符打印地址
    2.宏定义,NSLog提供了各种宏定义变量,如行号__LINE__、文件名__FILE__等
    sample: NSLog(@“%d”, __LINE__);
     
     
     
     
  • 相关阅读:
    如何进行函数式编程
    HTML文本格式化
    非模态对话框的创建及注意事项
    中国第一代程序员列传
    野指针
    缓冲区溢出攻击
    windows全部启动项
    HTML 样式
    Volatile关键字
    HTML基本标签
  • 原文地址:https://www.cnblogs.com/hellovoidworld/p/4119368.html
Copyright © 2011-2022 走看看