Obj-C 学习的一些基础语法

//The first OC Program
#import <Fundation/Function.h>
int main(int arg, const char args[])
{
	NSAutoreleasePool* pool = [[NSAutorelease alloc] init ];
	NSLog(@"Hello World");
	[pool drain];
	return (0);
}

//Second Test
#import <Fundation/Function.h>
int main(int arg, const char args[])
{
	NSAutoreleasePool* pool = [[NSAutorelease alloc] init ];
	int x = 50, y = 40;
	int sum = x + y;
	NSLog(@"The sum of x and y is %d ", sum);   

	[pool drain];
	return (0);
}
// Obj-C 中的BOOL 
// Obj-C 中的BOOL变量不同于其他语言(比如C/C++, Java),BOOL 的值只有YES和NO,
// 原理上BOOL 变量也是用8位二进制数表示。如果把一个整数赋值给布尔类型,在C++中的不等于0的都是真(true),而Obj-C中
// 变量要看8位二进制数的最低位,最低位为0则是NO,最低位为1则是YES

//间接
#import <Fundation/Funtion.h>

int main(int arg, const char args[])
{
	const char* words[4] = {"x1", "x2", "x3", "x4"};// 每次需要改变这些字符串时,都需要改动源代码
	int i = 0;
	int wordcounts = 4;
	for(; i < wordcounts; ++i)
	{
		NSLog(@"The %s  word's length is %d ", words[i], strlen(words[i]));
	}
	return (0);
}
//改为文件读取
#import <Fundation/Funtion.h>

int main(int arg, const char args[])
{
	FILE* pFile = fopen("/temp/words.txt","r");   //字符串存于文件中
	char words[100];
	while(fgets(words, 100, pFile))
	{
		words[strlen(words)-1] = '\0';
		NSLog(@"The %s  word's length is %d ", words, strlen(words[i]));
	}

	fclose(pFile);
	return (0);
}


//oop
//面向对象编程大师 Bertrand Meyer 的开放/关闭原则(open/closed principle),即软件实体应该对扩展开放,对修改关闭。
@interface NSCircle:NSObject
{
	shapeType fillShape;
	colorType fillColor;
}
-(void) setShape: (shapeType) shape;
-(void) setColor: (colorType) color;
-(void) draw;
@end

@implementation NSCircle  //实现方法不必按顺序,也可以有一些类中没有声明的方法,类中没有声明的方法在@implementation中算作私有方法
							//但 并不是真正的私有方法! Objective-C 中并没有真正的私有方法,这只是Objective-C的副作用
-(void) setShape:(shapeType) shape // 这里注意形参与@interface中的成员变量重名时会隐藏成员变量
{
	fillShape = shape;
}
-(void) setColor: (colorType) color
{
	fillColor = color;
}
-(void) draw
{
	NSLog(@"The shape is %@ and the color is %@", fillShape, fillColor); //实际上省略了self,   %@是调用了类的description方法
}
@end

//inherit  继承
@interface Shape
{
	shapeType fillShape;
	colorType  fillColor; 
}
-(void) setShape: (shapeType) shape;
-(void) setColor: (colorType) color;
-(void) draw;
@end

@interface Circle : Shape// 子类可以共用父类的成员变量,只需重写相应的方法即可
@end

@interface Rectanle : Shape
@end

@implementation Shape
-(void) setShape: (shapeType) shape  //会复制传入参数的值,如果参数是指针,则复制指针的值,传入一个指针的副本(其实是变量本身,只是指针地址被复制)
{
	fillShape = shape;
}
-(void) setColor: (colorType) color
{
	fillColor = color;
}
-(void) draw
{
	NSLog(@"The shape is %@ and the color is %@")
}
@end

// Obj-C中的init方法 
@interface MyClass
-(id) init
{
	if (self = [super init])
	{
		// do something
	}
}
MyClass* pMyClass1 = [[MyClass alloc] init];
MyClass* pMyClass2 = [MyClass new];
// 通常setter 方法是set+变量名的首字母大写
//getter方法的方法名与变量名称相同~~~~~~get这个词在Cocoa中有特殊的含义,存取方法中千万不要加入get前缀
// 在Cocoa中,get 出现在方法名里面时,通常表示这个方法会通过你传入的(指针参数)来返回数值

//合成存取方法:(调用时的命名规则不变)
//1. 在类的接口部分,在所有的属性前面@property
//2. 在类的实现部分,@synthesize (name = newName)
// 其中括号中的参数可选。使用可选的规则时,setter和getter方法不变,而类的实例变量为newName

// 点语法的使用:
// (.)在 “=” 左边,表示调用了setter
// (.)在 “=” 右边,表示调用了getter 
// 例如: self.name = nil; 是调用参数为nil的setter方法,该方法先释放name所占内存,再置其值为nil

//Obj-C中所有对象间的交互都是通过指针来完成的

//不要再description方法中打印self,因为这样会导致死循环,因为打印self本身又一次调用了description方法。
NSRange:用来表示相关事物的范围
typedef _NSRange
{
unsigned int location;
unsigned int length;
} NSRange;
例如:"I like apple." apple 可以用location为8,length为5表示。
location未被赋值时,可能为NSNotFound.
@ NSRange range;
  range.location = 5;
  range.length = 9;

@NSRange range = {5, 9}; // C语言风格

@NSRange range = NSMakeRange(5, 9);  // Obj-C风格
Obj-C风格的好处是可以在任何的函数调用中使用
[anObject function: NsSMakeRange(5, 9)];


typedef struct _NSPoint     //NSMakePoint()
{
float x;
float y;
}NSPiont;
表示笛卡尔平面中的一个点

typedef struct _NSSize    //NSMakeSize()
{ 
float width;
float height;
}NSSize; 
数据的宽度和长度

typedef struct _NSRect   //NSMakeRect()
{ 
NSPoint origin;
NSSize  size;
}NSRect;
// 用数据结构是为了提升性能,因为GUI程序会用到很多临时的点,这些数据如果采用
//类来表示就要用动态分配,会牺牲性能。


NSString:处理字符串的类
//NSString 类不可变,一旦创建后不能改变它,例如不能添加和删除字符
//NSString 有一个子类NSMultableString, 是可以改变的。

NSString 的stringWithFormat的类方法原型如下:

+(id) stringWithFormat: (NSString* ) format, ...;
NSString* string = [NSString stringWithFormat:
                   @"I have %d apples", 5] ;
//”+“ 代表这个方法是类方法,通常用于创建新的实例时调用,创建新对象的类方法称
//为工厂方法, 类方法也可以用来访问全局变量
//例如AppKit中的NSColor类就有redColor和blueColor方法。

NSColor* color = [NSColor redColor];

//Obj-C中生成一个类的时候,会创建一个代表该类的类对象。类对象中包含
//1.指向超类的指针
//2.指向类方法列表的指针
//3.类名
//4.一个long型数据,为新创建的类实例对象指定大小(以字节为单位)


-(unsigned int) length; // 可以准确的计算标准字符串和国际化字符编码,
//如俄文,法文等~

NSString* string = [NSString stringWithFormat:"I like apple."];
int length = [string length];

-(BOOL) isEqualToString: (NSString*) string;
NSString* string1 = @"Hello";
NSString* string2 ;
string2 = [NSString stringWithFormat:"Hello"];
if ([string1 isEqualToString:string2])  
//不能简单的比较指针值,比较指针值时其实是判断两个string是否指向同一个字符串
{
    NSLog(@"They are the same string!");
}

// 区分大小写的比较
-(NSComparisonResult)compare:(NSString*) string;
typedef enum _NSComparisonResult //一个枚举变量
{
    NSOrderedAscending = -1;  //左侧的值小于右侧,即字母表中第一个字符串要靠前
    NSOrderedSame;
    NSOrderedDescending;
    NSComparisonResult;
}
[@"a" compare: @"b"];  // 返回NSOrderedAscending;

//不区分大小写的比较
-(NSComparisonResult)compare:(NSString*) string 
                     options:(unsigned) mask; 
//options的参数是一个位掩码,可以使用|(位或)来结合使用
//常用的位掩码有:
NSCaseInsensitiveSearch //不区分大小写
NSLiteralSearch        //完全比较,区分大小写
NSNumericSearch       //比较字符串的字符个数,而不是字符串的值

//忽略大小写且按字符串的字符数量排序
if(NSOrderSame == [string1 compare: string2
                           options:NSCaseInsensitiveSearch 
                                   | NSNumericSearch])
{
NSLog(@"");
}

//判断字符串开头前缀和结尾后缀的方法
-(BOOL) hasPrefix: (NSString*) string; //是否以string作为前缀开头
-(BOOL) hasSuffix: (NSString*) string; //是否以string作为后缀结尾

NSString* string = @"abc.mov";
if([string hasPrefix:@"abc"]){NSLog();}
if([string hasSuffix:@".mov"]){NSLog();}

//判断某个字符串中是否包含另一个另一个字符串
-(NSRange)rangeOfString: (NSString*) string;

NSRange range;  //返回一个range.start 和 一个 range.length
range = [@"fsfghdhfhg" rangeOfString: @"abc"];


//头文件中通常存放: 1. 类的@interface指令
                 2. 公共的struct定义
                 3. enum常量
                 4. #defines 和取 extern 全局变量

// 实现文件中存放: 1. 类的实现@implementation
                 2. 全局变量的定义
                 3. 私有struct

//#import <>  用来导入系统头文件  
//不会重复导入

//#import “” 用来导入自定义头文件

//@class 是用来防止多重依赖时修改一个文件导致重编译的问题。这样只是声明一个类即可,而不需要了解类中有多少个实例变量。
//也可以用@class 防止循环引用的问题。
// 在继承关系链中,@class不能成功,因为继承的类需要知道父类的全部信息,需要使用#import“X.h”文件

Cocoa 框架包含两个子框架:

 1.Foundation Kit    //开发文档位置:/Developer/ADC Reference Library/documentation/index.html  
 2. Application Kit

转载于:https://my.oschina.net/u/1782374/blog/372830

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值