小黑米的生活

只玩技术

Archive for the ‘objective-c’ Category

[Object-C] 常量

leave a comment

预处理程序对宏定义的参数实行文本替换,要避免一些有趣的陷阱

#define SQUARE(x) x*x;
y=SQUARE(v); //work fine;
y=SQUARE(v+1); //这样会造成和期望不一样的结果,因为会执行这样 y= x+1*x+1;
#define SQUARE(x) ((x)*(x)); //这样的更改是正确的,而且非常重要, 尤其是外面的括号~
#define IS_LEAP_YEAR(y) y%4==0 && y%100!=0 \
||y%400==0

继续阅读

Written by edwardhey

六月 15th, 2011 at 11:42 上午

Posted in objective-c

[Object-C] 数据结构

one comment

枚举类型:

enum direction {east,west,south,north};//定义枚举
enum direction dir;//声明变量

enum {east,west,south,north} direction;//定义枚举并声明变量,注意作用域…

typedef enum {east,west,south,north} Direction;//定义Direction类型是枚举类型的,注意和上面的语法不同,typedef ,所以理解成 typedef enum {east,west,south,north} Direction;

字符对象

//字符串替换
[mstr replaceOccurrencesOfString:search withString:replace options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mstr length])];

//从文件读入内容,并创建字符串
NSString* pat

//以下被省略了…

继续阅读

Written by edwardhey

六月 11th, 2011 at 9:08 上午

Posted in objective-c

[Object-C]面向对象小试牛刀

leave a comment

XYPoint.h

//
// Rectangle.h
// study
//
// Created by edwardhey on 11-5-25.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@class XYPoint; //相当于占位符,编译的时候会载入XYPoint类而不至于载入整个头文件,以保证下面的代码可以继续编写,这种效率会更好,如果要使用类里面的方法的时候就必须把整个头文件载入
@interface Rectangle : NSObject {
int width;
int height;
XYPoint *origin;
}

@property int width, height;

-(XYPoint *) origin; //相当于getter , .方式访问会访问此函数
-(void) setOrigin: (XYPoint*) pt; //相当于setter
//以下被省略了…

继续阅读

Written by edwardhey

五月 25th, 2011 at 6:11 下午

Posted in objective-c,技术