您的位置:首页 > 移动开发 > IOS开发

菜鸟:IOS 学习:磨难记 (第四天)

2013-01-18 22:18 281 查看
1、递增,递减
int test;
test = 0;
NSLog(@"%d %d %d", test++, test++ + ++test, --test + test++ - test-- + ++test);

NSLog(@"%d %d %d", test++, test++ + ++test, --test + test++ - test-- * ++test / --test);


2、continue,小心死循环
int test;
test = 4;
do  {
if(test ==3) {
continue;
}
NSLog(@"%d", test--);
}while(test > 1);


3、goto  更的小心
do  {
NSLog(@"%d --", test--);

if(test ==3) {
continue;
}
if(test == 2) {
goto lableDemo;
}
}while(test > 1);

int testTwo, TestThree;

testTwo  = 11;
NSLog(@"%d ++", testTwo);

TestThree  = 12;
lableDemo:

NSLog(@"%d goto", TestThree); 跳转的悲剧


4、delegate 用 assign 而不使用 retain

循环引用,引用计数增加,很容易内存泄漏
assign 不增加计数
retain 增加

5、看斯坦福大学公开课,
http://i.youku.com/u/UOTYxNjIxNTY=/videos 

CSDN国人分析贴:斯坦福iOS公开课笔记,他的其他笔记很亮,由浅入深

6、   define
<> if endif
1、#define
  用于环境替换 ,传统的
Apple 命名惯例是以一个 k 开始常量名称,剩下的名称使用 CamelCase 法命名 
 2、#if
 statements #else   otherStatements #endif
 预处理器允许条件编译   

#define KDebug 1//调试模式开启
#if KDebug
#define KMaxDemo 11
#endif


7、extern static 
1、extern 使用外部文件的变量  extern
int demo
2、static 在局部范围声明的变量在当前文件将成全局 static int demoTwo
3、static和extern不能并存,static局限于当前文件 
                 

8、register
register int demoThree 

关键字提示编译器,它所修饰的变量会频繁地被引用, 编译器可能选择将该变量存储在一个寄存器中(以便于快速访问),而不是存储 在 RAM 中 
                        

9、const
声明变量地址转化为常量地址

int demo1, demo2;
int *const pt = &demo1; //const 修饰的一个指向整数的常量指针
*pt = 2;//正常
//pt = &demo2;//报错

const int *pt1 = &demo2; //const 修饰的一个指向整数常量的指针
//*pt1 = 3;//报错
pt1 = &demo1;//正常


10、分配内存
malloc()分配内存,
memset()设置内存。 
free() 释放内存 

char *ps = (char*)malloc(sizeof(int) * 100); 
memset(array, 0, sizeof(int) * 100);
free(array);


11、 特殊数据类型

typedef struct objc_class *Class;
typedef struct objc_object {
Class isa;
} *id;
typedef struct objc_selector  *SEL;

#if !OBJC_OLD_DISPATCH_PROTOTYPES
typedef void (*IMP)(void /* id, SEL, ... */ );
#else
typedef id (*IMP)(id, SEL, ...);
#endif

#define OBJC_BOOL_DEFINED

typedef signed char BOOL;
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C"
// even if -funsigned-char is used.

#if __has_feature(objc_bool)
#define YES             __objc_yes
#define NO              __objc_no
#else
#define YES             ((BOOL)1)
#define NO              ((BOOL)0)
#endif

#ifndef Nil
# if __has_feature(cxx_nullptr)
#   define Nil nullptr
# else
#   define Nil __DARWIN_NULL
# endif
#endif

#ifndef nil
# if __has_feature(cxx_nullptr)
#   define nil nullptr
# else
#   define nil __DARWIN_NULL
# endif
#endif

#if ! (defined(__OBJC_GC__)  ||  __has_feature(objc_arr))
#define __strong /* empty */
#endif

#if !__has_feature(objc_arr)
#define __unsafe_unretained /* empty */
#define __autoreleasing /* empty */
#endif


id

id和void *并非完全一样。在上面的代码中,id是指向struct objc_object的一个指针,这个意思基本上是说,id是一个指向任何一个继承了Object(或者NSObject)类的对象。需要注意的是id是一个指针,所以你在使用id的时候不需要加星号。比如id foo=nil定义了一个nil指针,这个指针指向NSObject的一个任意子类。而id *foo=nil则定义了一个指针,这个指针指向另一个指针,被指向的这个指针指向NSObject的一个子类。

nil
nil和C语言的NULL相同,在objc/objc.h中定义。nil表示一个Objctive-C对象,这个对象的指针指向空(没有东西就是空)。

Nil
首字母大写的Nil和nil有一点不一样,Nil定义一个指向空的类(是Class,而不是对象)。

SEL
这个很有趣。SEL是“selector”的一个类型,表示一个方法的名字。比如以下方法:
-[Foo count] 和 -[Bar
count] 使用同一个selector,它们的selector叫做count。
SEL是指向 struct objc_selector的指针,但是objc_selector是什么呢?那么实际上,你使用GNU
Objective-C的运行时间库和NeXT Objective-C的运行运行时间库(Mac OS X使用NeXT的运行时间库)时,它们的定义是不一样的。实际上Mac
OSX仅仅将SEL映射为C字符串。比如,我们定义一个Foo的类,这个类带有一个-
(int) blah方法,那么以下代码:
NSLog (@"SEL=%s", @selector(blah));
会输出为 SEL=blah。SEL就是返回方法名。

IMP

从上面的头文件中我们可以看到,IMP定义为 id (*IMP)
(id, SEL,
…)。这样说来, IMP是一个指向函数的指针,这个被指向的函数包括id(“self”指针),调用的SEL(方法名),再加上一些其他参数。

说白了IMP就是实现方法。

Method的类型,是这样定义的:
typedef struct objc_method *Method;
struct objc_method {

    SEL method_name;

    char *method_types;

    IMP method_imp;

};

这个定义看上去包括了我们上面说过的其他类型。也就是说,Method(我们常说的方法)表示一种类型,这种类型与selector和实现(implementation)相关。

Class
文件在runtime.h里面,从上文的定义看,Class(类)被定义为一个指向struct objc_class的指针,它是这么定义的:

struct objc_class {
Class isa;

#if !__OBJC2__
Class super_class                                        OBJC2_UNAVAILABLE;
const char *name                                         OBJC2_UNAVAILABLE;
long version                                             OBJC2_UNAVAILABLE;
long info                                                OBJC2_UNAVAILABLE;
long instance_size                                       OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
struct objc_cache *cache                                 OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
#endif

} OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: