Learn Objectvie-C on the Mac 2nd Edition 笔记

Chapter 1
Apple’s Cocoa (for OS X) 和 Cocoa Touch (for iOS) toolkits 都是用 Objective-C写的.

Chapter 2

(1) .m 代表 “messages”的缩写

  .m -> Object-C compiler
  .c -> C compiler
  .cpp -> C++ compiler

(2)在 Objective-C中, #import 保证了每个头文件只被包含一次,如同c中的#ifdef头文件卫士的作用一样。

   #import <> // 系统头文件

   #import " "  // 用户自定义头文件

(3)NSLog()如同 C中的printf(),不过增加了时间和日期戳,并自动在行尾加了“\n"换行符。

      @(" ")表示" "中的字符串被当做一个NSString单元来处理。在NSLog中如果直接使用了C风格的字符串”“而缺少@(),则在编译时得到一个warning,运行时会crash。

(4)BOOL 类型

     在Objectvie-C中,BOOL类型是实际上是一个signed char的typedef,因此不仅可以用YES(值=1)和NO(值=0)表示C中的true和false,而且可以存放其他的值。这里会有一个易错点:如果将一个short或int等类型的值赋给BOOL变量后,会截断末尾的8位,如果这末尾8位值刚好为0(如8960,其十六进制值为 0X2300),此值的BOOL值就是0,即NO,而不是非NO了。

Chapter 3

(1)计算机科学的核心思想:Indirection。Instead of using a value dirctly in your code, use a pointer to the value; Instead of doing something by yourself, ask another person to do it.

(2)一个文件读取示例:

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
    if(argc == 1)
    {
        NSLog (@"You need to provide a file Name");
        return (1).
    }

    FILE *wordFile = fopen(argv[1], "r"); // argv[0] is the code file path
    char word[100];
    while(fgets(word, 100, wordFile)
    {
         // strip off the trailing \n
         word[strlen(word) - 1] = '\0';
         NSLog(@"%s kis %lu characters long", word, strlen(word));  
    }
    fclose(wordFile);
    return(0).
} // main

(3)在Objective-C中,方法调用采用的是中缀表示法(infix notation),如

[circle setFillColor: kRedColor];

    如果一个方法无参数,则无冒号和参数列表;如果有参数,则有冒号:

- (void) scratchTheCat; // 无参数
- (void) scratchTheCat: (CatType) critter; // 有参数

Chapter 6

 (1)Objective-C类的代码可以分为2个部分:

          1>. @interface 部分   .h 文件

#define part
extern global variables;
@interface
XX : YY { public structs; } public method declarations. @end

  提供了该类的公共接口声明。

          2>. @implementation 部分   .m文件

@interface XX
{
private structs;
}
@end

@implementation
XX private self-used methods @end

   提供了该类的实现以及内部函数。

   如果以.mm为文件后缀,则表示使用了Objective -C++,即使用C++和Objective-C混合代码。

(2) 头文件中,使用类前置声明,以避免交叉编译出错:

@class XYX;

chapter 8 User Interface Kit(UIKit)

(1) Foundation framework 类:

      NSString

      NSArray

      NSEnumerator

      NSRange

 

      NSNumber

 (2) CoreFoundation (Foundation framework build on it )

     函数名或变量名以”CF“开头:

      CGSize

      CGPoint, CGPointMake()

      CGRect,  CGRectMake()

posted on 2014-10-20 17:59  没有什么能够阻挡  阅读(202)  评论(0编辑  收藏  举报

导航