objective-c的常用基础类

来源:互联网 发布:怎么看管家婆数据密码 编辑:程序博客网 时间:2024/05/17 01:11

几个常用的基础类的使用示例

只弄了几个 下次去实验室再继续用iMac测试

////  main.m//  Array_Test////  Created by mac11 on 12-3-7.//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.//#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){    @autoreleasepool {                // insert code here...        NSArray *array = [ [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six", nil];        int count = [array count];        NSLog(@"This array has %d elements.\n",count);                //判断元素是否在数组中        if([array containsObject:@"Two"])            NSLog(@"Two is in the array");        else            NSLog(@"It's not in the array");               //返回索引最大的那个        id object = [array lastObject];        NSLog(@"%@",object);                object = [array objectAtIndex:3];        NSLog(@"%@",object);                NSMutableArray *marray = [[NSMutableArray alloc] init];        //复制整个数组        marray = [NSMutableArray arrayWithArray:array];        NSLog(@"NSMUlableArray :%@",marray);               /*        NSEnumerator *enumerator = [anArray objectEnumerator];        id object;                while ((object = [enumerator nextObject])) {            // do something with object...        */        NSEnumerator *enumerator = [array objectEnumerator];        id object1;        int num=1;        while(object1 = [enumerator nextObject])        {            NSLog(@"The %ith element of the array is :%@",num++,object1);        }                    }    return 0;}


#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){    @autoreleasepool {                // insert code here...       //NSLog(@"Hello, World!");        NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"Value1",@"key1",@"value2",@"key2", nil];        int count = [dic count];        NSLog(@"The Dictionary is :%@",dic);        NSLog(@"The count is %i",count);            }    return 0;}

////  main.m//  string_test////  Created by mac11 on 12-3-7.//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.//#import <Foundation/Foundation.h>int main (int argc, const char * argv[]){    @autoreleasepool {                // insert code here...        NSLog(@"Hello, World!");        NSString *str1 = @"I'm a men,this is a NSstring";        NSLog(@"%@",str1);        NSString *str2 = [[NSString alloc] initWithString:@"This is the string too"];        NSLog(@"%@",str2);        NSString *str3 = [[NSString alloc] initWithFormat:@"My height is %i,and my weight is %i.\n",170,65];        NSLog(@"%@",str3);                NSString *str4 = @"This is";        if([str2 hasPrefix:str4])            NSLog(@"Oh yes");        else            NSLog(@"oh no");        if([str1 isEqualToString:str2])            NSLog(@"We are the same");        else            NSLog(@"We are different");        NSUInteger length=[str4 length];        NSLog(@"The length of the string is %i",length);                NSMutableString *mstr1 = [[NSMutableString alloc] initWithCapacity:42];        [mstr1 appendString:@"I'm the MutableString ~"];        [mstr1 appendFormat:@", oh no~"];        NSLog(@"%@",mstr1);                [mstr1 insertString:@"lallala~~ " atIndex:4];        NSLog(@"%@",mstr1);                //更多的功能用 alt+NSString了解    }    return 0;}

#import <Foundation/Foundation.h>int main (int argc, const char * argv[]) {    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    // NSString usageNSString *str0 = [NSString stringWithString:@"Hello"]; // created by class method,don't need to be released by creatorNSString *str = [[NSString alloc]initWithString:@"Hello,world"];NSLog(@"%@",str);if([str isEqualToString:@"Hello,world"]==YES){NSLog(@"str is equal to Hello,world");}// seperate the string by ,NSArray *subStr = [str componentsSeparatedByString:@","];[str release];// NSNumber usageNSNumber *num = [NSNumber numberWithInt:30];NSNumber *myNum=[[NSNumber alloc]initWithBool:YES];if([[myNum className] isEqualToString:@"NSCFNumber"]){NSLog(@"a numeric value\n");}else if([[myNum className] isEqualToString:@"NSCFBoolean"]){NSLog(@"a boolean value\n");}NSLog(@"number=%@\n",myNum);[myNum release];// NSArray usage// create an array containing contents of the specified file. the file must be of type .plist//NSArray *myArr2 = [[NSArray alloc] initWithContentsOfFile:@"mydata.plist"];// create an array from objects(can be different types!),the last one must be nilNSArray *myArr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil];NSString *myStr = [myArr objectAtIndex:2];  // [myArr lastObject]  [myArr count]NSLog(@"%@",myStr);// looping through the arrayfor(id obj in myArr)NSLog(@"array element=%@\n",obj);[myArr release];// NSMutableArray usageNSMutableArray *myMutArr = [[NSMutableArray alloc]initWithObjects:@"One",@"Two",@"Three",nil];[myMutArr addObject:@"Four"];  // [myMutArr addObject:myNum] OK[myMutArr insertObject:@"Extra" atIndex:1];NSLog(@"%@",[myMutArr lastObject]);[myMutArr release];    [pool drain];    return 0;}


原创粉丝点击