关闭

OC视频教程29课-第01讲 Classes

标签: importincludeclass methodinstance method
224人阅读 评论(0) 收藏 举报
分类:

第01讲 Classes

    ObjectiveC is an object-oriented language:it manages classes and objects.Objective-C uses a strict object model.

            strict |strɪkt| adj严厉的、明确的、精确的、严格的

    Like in C++, it is useful to split the code between interface and implementation for each class.

            split |splɪt| v切开、劈开 

    Objective-C uses .h files for heads,and .m files for the code.

    Objective-C introduces the #import directive to replace #include.Indeed,every C header should use compilation guards to prevent multiple inclusions. This is automatic when using #import.

            introduce |ˌɪntrəˈdjuːs, American -ˈduːs| v介绍、引出、推行

            indeed |ɪnˈdiːd| 实际上

            compilation |ˌkɒmpɪˈleɪʃn|noun编辑、编写、撰写、编制

            guard |gɑːd| noun警卫、警戒

            prevent |prɪˈvent| v阻止

            multiple |ˈmʌltɪpl| noun倍数、连锁店 adj多个部分组成的、多处的、多种的、多重的

            inclusion |ɪnˈkluːʒn| noun包括

            header file 头文件


    #import keeps track of which headers have already been included and is ignored if a header is imported more than once in a compilation unit. This makes it unnecessary to use header guards.

            track |træk| noun足迹、车辙

            necessary |ˈnesəsəri, American -serɪ| adjective必要的、必需的


    Below is a typical interface/implementation example.The Objective-C syntax is explained later.

            syntax |ˈsɪntæks| noun句法、[计算机语言的]句法

            explain |ɪkˈspleɪn|解释、说明 


C++:

    //In file Foo.h


    #ifndef __FOO_H__    //compilation guard

    #define __FOO_H__


    class Foo

    {

    …

    };

    #endif


    //In file Foo.cpp


    #include “Foo.h”

    ….



Objective-C

    //In file Foo.h

    @interface Foo:NSObject

    {

    …

    }

    @end


    //In file Foo.m

    #import “Foo.h”

    @implementation Foo

    …

    @end


1.Class Definition 类的声明

    Interface Section 接口部分(.h文件)

        General Format:


        @interface className : parentClass<protocol,…>//OC不支持多继承,可以通过支持多个协议的方法来实现相似的功能。

        {

            instanceVariable Declarations;

        }

        method Declarations

        …

        @end


    Objective-C defines a root class:NSObject.Every new class should be a descendant of the NSObject class.It provides a huge number of facilities for the run-time system.

            descendant |dɪˈsendənt| noun后代

            facility |fəˈsɪləti| noun场所、设施、便利、才能


    Strictly speaking,every object should be of type NSObject,and every pointer to an object could be declared as NSObject*. In face, one can use the type id instead. This is a short and handy way to declare a pointer to any object, and provides dynamic type-checking instead of static type-checking.It is very useful for some weak typing on generic methods.

            strictly |ˈstrɪktli| adverb严格地、绝对地

            handy |ˈhændi| adjective好用的、近便的

            generic |dʒɪˈnerɪk| adj普通的

    Method Declarations

        General Format

        #import ””

        mType (returnType) name1:(type1)param1  name2:(type2)param2…


    The method name1:name2:… is declared, which returns a value of type returnType and has formal parameters param1,param2,…param1 is declared to be of type type1,param2 is declared to be of type2,and so on.

        formal |ˈfɔːml| adjective正式的、正规的

    name2 and type2 can be omitted(omit |əˈmɪt| v删除、疏忽).

        [selftestFun:@"one" :@"two"];//onetwo__NSCFConstantString

        - (void)testFun:(NSString*)str1 :str2

        {

            NSLog(@"%@%@%@",str1,str2,NSStringFromClass([str2class]));

        }

    Colons(:) are parts of the method name.

        colon |ˈkəʊlən,-lɒn| noun冒号

    If myType is +,a class method is declared,but if myType is -, aninstance method is declared.


iOSidNSObject*id<NSObject>的区别

参考:http://www.devdiv.com/ios_id_nsobject_id_lt_nsobject_gt_-blog-1-50764.html

    1. 我们来看看id的定义,它就是一个指针,一个结构体指针,它可以指向的类型不仅限于NSObject

        /// An opaque type that represents an Objective-C class.

        typedefstruct objc_class *Class;

        /// Represents an instance of a class.

        struct objc_object {

            Class isa  OBJC_ISA_AVAILABILITY;

        };

        /// A pointer to an instance of a class.

        typedefstruct objc_object *id;


    2. NSObject*就是 NSObject类型的指针了,它范围较小。

        @interface NSObject <NSObject> {

            Class isa OBJC_ISA_AVAILABILITY;

        }



    3. id<NSObject>是指针,它要求它指向的类型要实现NSObject protocol

        iOS中很多类定义很奇葩,类名叫NSObject,定义个接口也叫NSObject,但是它俩不是一个东东。


    NSObject类实现了NSOject接口,所以id<NSObject>可以指向NSObject的对象。


    如果我们来看看NSProxy的定义,你会发现,它不是继承自NSObject,但是却实现了NSObjecct接口,

        NSProxy定义类似这样

            @interface NSProxy <NSObject> {

                Classisa;

            }

    所以id<NSObject>可以指向NSProxy的对象。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:73674次
    • 积分:2389
    • 等级:
    • 排名:第16486名
    • 原创:170篇
    • 转载:5篇
    • 译文:5篇
    • 评论:1条