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
    }
  • 相关阅读:
    Shell基本语法
    CURL简单使用
    <C> 字符串简单习题
    <C> 字符串相关的函数
    <C> 内存分区
    <C> 最大值以及最大值下标 二分查找(折半查找)
    <C> 函数 函数指针
    <C> 冒泡排序及其非常非常非常简单的优化
    <C> typedef 宏 const 位运算
    <C> 数组
  • 原文地址:https://www.cnblogs.com/chenyg32/p/4913931.html
Copyright © 2011-2022 走看看