• 《Programming with Objective-C》第三章 Working with Objects


    Object和普通变量的区别

    If you’re used to using terms like the stack and the heap, a local variable is allocated on the stack, while objects are allocated on the heap.

    - (void)f
    {
        int a = 2;                    //Stack
        NSString *s = @"Hello";        //Heap
    }

    函数f中,a指向的内存在栈中,函数退出的时候变量a将不能再被访问,其内存也会被释放;s指向的内存在堆中,函数退出的时候s也不能再被访问,但是s指向的内存可能继续存在。

    Factory Method v.s. Abstract Factory

     Todo 等查找资料后再补上

    Objective-C是一门动态语言

    id someObject = @"Hello, World!";
    [someObject removeAllObjects];

    编译时,someObject是一个id类型,所以编译器不会报错。
    运行时,编译器会出现Runtime Error因为NSString对象不能响应removeAllObjects.

    NSString *someObject = @"Hello, World!";
    [someObject removeAllObjects];

    编译时,编译器知道someObject是一个NSString类型,NSString对象不能响应removeAllObjects,所以编译时编译器会报错

    等于/不等于

    //基本类型
    if (someInteger == 42) 
    {
        // someInteger has the value 42
    }
    
    //比较是否是同一个对象
    if (firstPerson == secondPerson)    
    {
        // firstPerson is the same object as secondPerson
    }
    
    //比较2个对象的内容是否相等
    if ([firstPerson isEqual:secondPerson])     
    {
        // firstPerson is identical to secondPerson
    }
    
    //NSNumber, NSString and NSDate等类型比较大小不能用>、<,应该用compare:方法
    if ([someDate compare:anotherDate] == NSOrderedAscending) 
    {
        // someDate is earlier than anotherDate
    }
  • 相关阅读:
    分布式文件系统--->fastDFS
    varnish4.0缓存代理配置
    varnish4.0缓存代理配置
    varnish4.0缓存代理配置
    SDN 是什么
    SDN 是什么
    SDN 是什么
    Solidworks如何保存为网页可以浏览的3D格式
    github 的 配置SSH
    当一个实例被创建,__init__()就会被自动调用
  • 原文地址:https://www.cnblogs.com/chenyg32/p/4913931.html
Copyright © 2020-2023  润新知