Object-C学习笔记

类的声明办法
@interface MyClass
{
   int num;
   NSString* name;
   id data;        //id表示弱类型
}
-(id)initWithString:(NSString*)aName;    //返回值为弱类型函数名为initWithString 参数为aName的函数  
+(MyClass*)createMyClassWithString:(NSString*)aName;//“+”表示为一个类函数
-(void)insertObject:(id)anObject atIndex:(NSUInteger)index;//两个参数“-“表示这是一个实例函数




申请内存
numBytesNeeded=imageHeight*imageWidth*sizeof(unsigned char);
unsigned char* pixels =(unsigned char*)malloc(numBytesNeeded) ;
free(pixels);
pixels=NULL;




调用类方法
[对象名 函数名:参数]




id mycar=[car new];


常量NSString
int main()
{
NSAutoreleasePool *pool =[[NSAutorealeasePool alloc] init];
NSString *testString =@"helloworld";
int stringLength=[testString length];  //消息传递机制  向testString对象传递一个length消息
NSLog(@"the length of testString is:%d",stringLength);
[pool drain]
return 0;
}


字符串相等比较
-(BOOL)isEqualToString(NSString *) aString;


int main()
{
NSAutorealsepool *pool=[[NSAutorealsePool alloc] init];


NSString *testString=@"helloworld";
NSString *testString=@"helloMyWorld";


if(![testString isEqualToString:testString2])
NSLog(@"they are different!");
[pool drain];
return 0;
}


字符串比较
int main()
{
NSAutorealsePool *pool =[[NSAutorealsePool alloc] init];
NSString *testString =@"it is my World";
NSString *testString2=@"My World";
NSRange myRange;
myRange.location=6;//从第六个字符开始
myRange.length=8;//往后找8个字符
NSComparisonResult res=                  //枚举类型 有NSOrderedAscending =-1  NSOrderedSame  NSOrderedDescending三种
[testString compare:testString2 
options:NSCaseInsensitiveSearch //不区分大小写  NSLiteralSearch 区分大小写  NSNumericSearch字符串的数量
range:myRange];
if(res===NSOrderedSame)
NSLog(@"result is same");
[pool drain];
return 0;
}


字符串处理
-(NSRange)rangeOfString:(NSString *)aString  //返回第一次出现的aString位置和长度


-(NSString *)substringFromIndex:(NSUInteger)anIndex  //返回从给定位置开始到结尾的字符串对象


-(NSString *)substringToIndex:(NSUInteger)anIndex //从开始到指定位置


-(NSString *)substringWithRange:(NSRange)aRange


-(BOOL)hasPrefix:(NSString *)aString  //是否以aString开头


-(BOOL)hasSuffix:(NSString *)aString //是否以aString结尾




可变字符串NSMutableString
int main()
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSMutableString *testString;
testString=[NSMutableString stringWithCapacity:30];//构造一个长度为30的字符串
[testString appendString:@"it is my world"];//在可变字符串后加上一串字符串


NSLog(testString);


NSRange myRange{10,5};
[testString deleteCharactersInRange:myRange];//删除指定范围内的字符
NSLog(testString);


NSString *aString =@"Asia";
[testString insertString:aString atIndex:10];  //在指定位置插入字符串
NSLog(testString);


NSString *bString =@"China";
[testString replaceCharactersInRange:myRange withString:bString];//替代指定范围内的字符
NSLog(testString);
[pool drain];
return 0;
}






静态数组NSArray
int main()
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
int i=0;
NSString *testString=@"monday tuesday wednesday thursday friday saturday sunday";
NSArray *testArray=[testString componentsSeperatedByString " "];//把字符串通过 空格划分开 返回一个数组
int count=[testArray count];  //返回数组的长度
for(;i<count;i++)
{
NSLog("testArray[%i] is %s",i,[[testArray objectAtIndex:i]UTF8String]);//返回对应索引值上的对象 objectAi
}
int indexDay;
if(indexDay=indexOfObject:@"tuesday")
NSLog(@"tuesday index is : %i",indexDay);
[pool drain];
return 0;
}


动态数组NSMutableArray
int main()
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];


NSMutableArray *testMutableArray=[NSMutableArray arrayWithCapicity:16];//创建动态数组
[testMutableArray addObject:@"Monday"];       //数组中插入对象
[testMutableArray addObject:@"Tuesday"];
[testMutableArray addObject:@"Wednesday"];
[testMutableArray addObject:@"Thusday"];
[testMutableArray addObject:@"Firday"];


NSLog(@"initial Array:%s",[[testMutableArray description] UTF8String]);  //输出数组详细信息


[testMutableArray insertObject:@"Saturday" atIndex:5];
[testMutableArray insertObject:@"Sunday" atIndex:6];


NSLog(@"initial Array:%s",[[testMutableArray description] UTF8String]);


[testMutableArray removeObjectAtIndex:5];  //去掉索引上的对象
[testMutableArray removeLastObject];
[testMutableArray replaceObjectAtIndex:5 withObject:@"LuckyDay"];
[pool drain];
return 0;
}






字典NSDictionary
int main()
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSDictionary *testDic=[NSDictionary dictionaryWithObjectsAndKeys:@"first",@"monday",@"second",@"tuesday",nil];//创建数据字典
NSLog(@"the dictionary count:%d",[testDic count]);
for(NSString *key in testDic)
NSLog(@"key:%@;value:%@",key,[testDic objectForkey:key]);//根据键值返回对象

[pool drain];
return 0;
}


可变字典NSMutableDictionary
int main()
{
NSAutoreleasePool * pool=[[NSAutoreleasePool alloc] init];


NSMutableDictionary *testDic=[NSMutableDictionary dictionary];//构建可变数据字典


[testDic setObject:@"first" forKey:@"monday"];         //用setObject来向变量字典里加入新对象
[testDic setObject:@"second" forKey:@"tuesday"];
[testDic setObject:@"third" forKey:@"Wednesday"];


for(NSString *key in testDic)
NSLog(@"initial key:%@;value:%@",key,[testDic objectForKey:key]);       //objectForKey:key来返回对应的值


[testDic removeObjectForKey:@"tuesday"];                             //去除元素
[pool drain];
return 0;


}


//数组跟字典都不能保存C语言的基本类型 如int float等,只能保存对象
posted @ 2011-12-13 10:55  nightkidzxc  阅读(1035)  评论(0编辑  收藏  举报