zoukankan      html  css  js  c++  java
  • 《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
    }
  • 相关阅读:
    关于微信三点定位法
    PHP 取302跳转后真实 URL 的两种方法
    前端布局神器display:flex
    JS实现document.ready
    为什么无返回值的链表的插入操作头结点一定要用指向指针的指针
    常量字符串和指针
    C语言中指针数组和数组指针的区别
    二维数组简介与使用
    访问者模式
    解释器模式
  • 原文地址:https://www.cnblogs.com/chenyg32/p/4913931.html
Copyright © 2011-2022 走看看