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;}
- objective-c的常用基础类
- Objective-C常用类的常用方法
- Objective-C基础之NSDate的常用方法
- Objective-c 常用类
- Objective-C类 基础
- Objective-C NSString类的常用方法
- Objective-C ,ios,iphone开发基础:几个常用类-NSNumber
- Objective-C基础笔记(8)Foundation常用类NSString
- Objective-C基础笔记(9)Foundation常用类NSArray
- objective C 基础 类的组成
- Objective-C 类的基础程序
- Objective-C基础-类方法的学习
- 常用的Objective-C开源类库
- Objective-c的Block基础
- Objective-C 基础集合类
- Objective-C 类基础(二)
- Objective-C 类基础(三)
- 【Objective-C基础】集合类
- 《编程珠玑》学习总结1—bitmap
- Excel导入数据
- android定位和地图开发实例
- Qt绘制图形保存图片 (solaris下保存失败)
- php json 中文乱码
- objective-c的常用基础类
- 类型后面带三点啥意思?
- android ViewFlipper屏幕切换
- 代码调不通,OJ题不会做?寻求帮助,学会提问-《提问的智慧》
- 最新一批毕业课题参考网址资源分享
- 手机摄像头介绍及其参数配置
- Coroutine(协程)
- 学习实用英语(1)
- 杭电1016