#import <Foundation/Foundation.h>
#import "Student.h"
void bl(NSMutableArray *temp){// bl是遍历
for (id t in temp) {
NSLog(@"%@",t);
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
//创建数组对象
NSArray *array=[NSArray arrayWithObjects:@"one",@"two",@"3",@"4",@"5",nil];
// NSArray *array=@[@"one",@"two"];//同上可以用,但不建议使用,因有些手机版本用不了
int ct=[array count];//获取数组的大小
for (int i=0; i<ct; i++) {//遍历数组的大小
// NSLog(@"%d=%@",i,array[i]);//同下可以用,但不建议使用,因有些手机版本用不了
NSLog(@"遍历数组为%d=%@",i,[array objectAtIndex:i]);
}
//遍历数组
for (int i=0; i<ct; i++) {
NSString *temp=[array objectAtIndex:i];
NSLog(@"遍历数组为%d=%@",i,temp);
}
//forin可以快速遍历容器 只适用于容器
for (NSString *temp in array) {
NSLog(@"遍历数组为%@",temp);
}
//创建数组
NSArray *array2=[NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2], nil];
bl(array2);
for (int i=0; i<[array2 count]; i++) {
NSNumber *temp=[array2 objectAtIndex:i];
//NSLog(@"%d=%@",i,temp);//等同于下排
NSLog(@"%d=%d",i,[temp intValue]);
}
//可变数组
NSLog(@"-------------可变数组");
NSMutableArray *mary=[[NSMutableArray alloc]initWithObjects:@"spple",@"htc",@"iphone", nil];
//可添加元素
[mary addObject:@"联想"];
bl(mary);
[mary removeObjectAtIndex:3];
NSLog(@"删除后的容器内容。。。。。。。");
bl(mary);
NSLog(@"排序后的结果---------");
[mary sortUsingSelector:@selector(compare:)];
bl(mary);
NSLog(@"学生对象排序-------");
Student *st1=[Student studentWithName:@"dav" andScore:90];
Student *st2=[Student studentWithName:@"lisha" andScore:60];
Student *st3=[Student studentWithName:@"micky" andScore:80];
NSArray *array01=[NSArray arrayWithObjects:st1,st2,st3, nil];
for (Student *object in array01) {
NSLog(@"学生姓名:%@,学生成绩%d",object.name,object.score);
}
//进行排序
NSArray *array02=[array01 sortedArrayUsingSelector:@selector(myCompare:)];//调用的是array01的数据
NSLog(@"学生对象排序后---------");
for (Student *object in array02) {
NSLog(@"学生姓名:%@,学生成绩:%d",object.name,object.score);
}
//字典的创建
NSLog(@"字典使用.......");
NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys:st1,@"101",st2,@"102",st3,@"250", nil];
NSArray *keys=[dic1 allKeys];
for (NSString *object in keys/*dic1 也对*/ ) {
Student *st=[dic1 objectForKey:object];
NSLog(@"学生姓名%@,学生成绩:%d",st.name,st.score);
}
NSMutableDictionary *dic2=[NSMutableDictionary dictionaryWithObjectsAndKeys:st1,@"101",st2,@"102",st3,@"250", nil];
//添加数据到字典
Student *st4=[Student studentWithName:@"kelly" andScore:70];
[dic2 setObject:st4 forKey:@"109"];
NSLog(@"添加后。。。。。。");
for (NSString *object in dic2) {
Student *st=[dic2 objectForKey:object];
NSLog(@"学生姓名:%@,学生成绩%d",st.name,st.score);
}
[dic2 removeObjectForKey:@"250"];
NSLog(@"删除后.........");
for (NSString *object in dic2) {
Student *st=[dic2 objectForKey:object];
NSLog(@"学生姓名%@,学生成绩%d",st.name,st.score);
}
//set容器的使用
NSLog(@"set容器的使用.........");
NSSet *set=[NSSet setWithObjects:@"one",@"two",@"three", nil];//创建
NSLog(@"%@",[set anyObject]);//取值(随机)
NSArray *setAry=[set allObjects];
NSLog(@"%@",[setAry objectAtIndex:1]);//循环遍历
for (id object in set) {
NSLog(@"%@",object);
}
//判断是否包含某个元素
if ([set member:@"one"]) {
NSLog(@"set中包含了one元素");
}
//数值类nsvalue可用于将基本struct变量转换为oc对象
NSRect rect=NSMakeRect(0, 0, 320, 480);
NSValue *val=[NSValue valueWithBytes:&rect objCType:@encode(NSRect)];//这排代码等同于下排代码
NSValue *val1=[NSValue valueWithRect:rect];//这排等同于上排代码
//将rect中的内容 传到rect2中
NSRect rect2;
[val getValue:&rect2];
//将结构体rect2变成nsstring,并打印
NSLog(@"%@",NSStringFromRect(rect2));
}