zoukankan      html  css  js  c++  java
  • Objectivec语法快速记忆法

    转自http://www.raywenderlich.com/4872/objective-c-cheat-sheet-and-quick-reference

     

    Class Header (.h)

    #import "AnyHeaderFile.h"

    @protocol ClassNameDataSource, ClassNameDelegate;

    //声明协议:
    @protocal ClassNameDataSource <NSObject>

        @optional//选择实现

        - (NSUInteger)numberOfItemsInCarousel:(ClassName*)carousel;

        @required//必须实现

         -(id) copywithzone:(NSZone *) zone; //方法列表
    @end

     

    @protocal ClassNameDelegate <NSObject>

        @optional//选择实现

        - (NSUInteger)numberOfItemsInCarousel:(ClassName*)carousel;

        @required//必须实现

         -(id) copywithzone:(NSZone *) zone; //方法列表
    @end

     

     

    @interface ClassName : SuperClass {

    //declare instance variables

     

        id<ClassNameDelegate> delegate;

        id<ClassNameDataSource> dataSource;

    }

    // define properties

    //define methods (including any

    //custom initializers)

    @end

     

    Class Implementation (.m)

     

    #import "YourClassName.h"

     

    @implementation ClassName

     

    // synthesize properties

     

    // implement methods (including any

    //custom initializers, and dealloc)

    @end

     

    Defining Methods

    - (anytype)doIt;

    - (anytype)doItWithA:(anytype)a;

    - (anytype)doItWithA:(anytype)a andB:(anytype)b;

     

    Implementing Methods

     

    - (anytype)doItWithA:(anytype)a andB:(anytype)b {

         // Do something with a and b...

          return retVal;

    }

    Dealloc

    - (void)dealloc {

     

      // Release any retained variables...

        [super dealloc];

    }

     

    Creating a Class Instance

     

    ClassName * myClass = [[ClassName alloc] init];

    // myClass valid until you call:

    [myClass release]; // Don’t need w/ ARC

    ClassName * myClass = [[[ClassName alloc] init]autorelease];

    // myClass valid until next run loop

     

    Calling a Method

    [myClass doIt];

    [myClass doItWithA:a];

    [myClass doItWithA:a andB:b];

     

    Defining Properties

    @property (attribute1, attribute2) propertyName;

     

    retain | Call retain on assign

    assign | Normal assign(default)

    copy   | Make copy on assign

    nonatomic |Make not threadsafe

    readwrite | Create getter&setter(default)

    readonly | Create just getter

     

    Synthesizing Properties

     

    @synthesize propertyName;

    @synthesize propertyName =_myInstanceVariable;

    Using Properties

    [myClass setPropertyName:a];

    myClass.propertyName = a; // alternative

    a = [myClass propertyName];

    a = test.propertyName; // alternative

     

    Declaring Variables

    anytype myVariable

    int | 1,2,500,10000

    float |   1.5,3.14,578.234

    double  |

    BOOL |  YES,NO,TRUE, FALSE

    ClassName * |  NSString *,NSArray *,etc

    id  | Can hold ref to any object

    Custom Initializer Example

     

    - (id)initWithParam:(anytype)param {

          if ((self = [super init])) {

              self.propertyName = param;

          }

         return self;

    }

    NSString Quick Examples

    NSString *personOne = @"Ray";

    NSString *personTwo = @"Shawn";

    NSString *combinedString =[NSString stringWithFormat: @"%@: Hello, %@!", personOne, personTwo];

    NSLog(@"%@", combinedString);

    NSString *tipString = @"24.99";

    float tipFloat = [tipString floatValue];

    NSArray Quick Examples

    NSMutableArray *array = [NSMutableArray arrayWithObjects:personOne, personTwo, nil];

    [array addObject:@"Waldo"];

    NSLog(@"%d items!", array.count);

    for (NSString *person in array) {

      NSLog(@"Person: %@", person);

    }

    NSString *waldo = [array objectAtIndex:2];

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    QT QQuickView嵌入到QT MDI中
    地理入门-经纬度时区速成总结篇(转)
    Linux下系统时间函数、DST等相关问题总结(转)
    camera接口---MIPI CSI-2接口、DVP接口和FPD-Link III LVDS、GMSL等接口对比(转)
    基于spi FLASH的嵌入式文件系统 littlefs(转)
    PID 温控系统 解决方法(转)
    单相计量芯片RN8209D使用经验分享(转)
    【开源】EasyFlash 新年发布 V4.0 beta 版,完全重写(转)
    在uboot中加入cmd_run命令,运行环境变量(转)
    MPU6050开发 -- 数据分析(转)
  • 原文地址:https://www.cnblogs.com/jiangshiyong/p/2669813.html
Copyright © 2011-2022 走看看