主要内容:
NSArray
NSNumber
SEL
数组排序
字典
//
// main.m
// 02-NSNumber
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
/**
NSNumber 作用:将简单的数据类型转化为对象
原因:why?
在oc当中数组,字典等存储的数据都要求是对象
*/
//创建:
//简单数据类型
int weight = 40;
float height = 10;
//转化成相应的对象
NSNumber *intNum = [[NSNumber alloc]initWithInt:weight];
NSNumber *floatNum = [[NSNumber alloc]initWithFloat:height];
NSLog(@"intNum:%@,floatNum:%@",intNum,floatNum);
//将对象存入数组
NSArray *array = @[intNum,floatNum];
//将对象转化成
int nowInt = [intNum intValue];
float nowFloat = [floatNum floatValue];
//比较
NSComparisonResult result = [intNum compare:floatNum];
NSLog(@"%li",result);
}
return 0;
}
//
// main.m
// 03-SEL
//
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//让编译器对不确定是否存在的方法的警告不提示
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
//SEL:是一种专门存储方法的数据类型
//格式:@selector(方法)
// SEL sel = @selector(bark);
//
// Dog *dog = [[Dog alloc]init];
// // [dog bark];
// // [dog eat:@"苹果"];
//
//
// //容错处理
// //respondsToSelector: 看对象是否响应后面的方法
// if ([dog respondsToSelector:sel]) {
// //performSelector: 专门来完成方法的调用的
// [dog performSelector:sel];
// }
//
// SEL sel1 = @selector(eat:);
// //respondsToSelector: 看对象是否响应后面的方法
// if ([dog respondsToSelector:sel1]) {
// //performSelector: withObject:专门来完成方法的调用的,调用的时带参数的
// [dog performSelector:sel1 withObject:@"苹果"];
// }
NSString *string1 = [NSString stringWithContentsOfFile:@"/Users/qianfeng/Desktop/方法列表.rtf" encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [string1 componentsSeparatedByString:@"*"];
NSLog(@"array:%@",array);
for (NSString *string in array) {
SEL sel2 = NSSelectorFromString(string);
Dog *dog1 = [[Dog alloc]init];
if ([dog1 respondsToSelector:sel2]) {
[dog1 performSelector:sel2];
}
}
}
return 0;
}
//
// main.m
// 04-数组排序
//
#import <Foundation/Foundation.h>
#import "MyClass.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//创建学生
Student *student1 = [[Student alloc]initWithName:@"xiaolan" age:21 andScore:100];
Student *student2 = [[Student alloc]initWithName:@"ligang" age:10 andScore:10];
Student *student3 = [[Student alloc]initWithName:@"zhangliang" age:11 andScore:80];
//创建班级
MyClass *myClass = [[MyClass alloc]initWithName:@"1438"];
[myClass.students addObject:student1];
[myClass.students addObject:student2];
[myClass.students addObject:student3];
//现在实现了sortUsingSelector:方法
//按照学生的年龄 降序排列
[myClass.students sortUsingSelector:@selector(isYoungerThanotherStudent:)];
//但是没有给标准 (按照年龄,姓名,是升序还是降序啊)
//所以要给他一个标准(通过一个方法得到的)
//方法要放在那里?
//放在数组的元素所在的类里面
//按照学生的年龄 升序排列
[myClass.students sortUsingSelector:@selector(isSmallThanOtherStudent:)];
NSLog(@"%@",myClass);
}
return 0;
}
//比年龄
//降序
-(BOOL)isYoungerThanotherStudent:(Student *)student{
//比较条件
return self.age < student.age;
}
//比姓名
//升序
-(BOOL)isSmallThanOtherStudent:(Student *)student{
return [self.name compare:student.name] > 0;
}
//
// main.m
// 05-字典
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
/**
字典:是对象,是一种存储数据的对象
形式:键(key)-值(value)对 的形式保存数据,字典没有顺序之说。
分类:可变字典,不可变字典
*/
//介绍不可变字典
//创建
//1.空的
NSDictionary *dictionary1 = [NSDictionary dictionary];
//2.通过对象方法
//一般key都是字符串
//注意:字典也是以nil结尾
NSDictionary *dic2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"apple",@"1",@"orange",@"2", nil];
NSLog(@"dic2:%@",dic2);
//objects:所有的值 forKeys:所有的键(key)
NSDictionary *dic3 = [[NSDictionary alloc]initWithObjects:@[@"apple",@"orange"] forKeys:@[@"1",@"2"]];
NSLog(@"dic3:%@",dic3);
//类方法
NSDictionary *dic4 = [NSDictionary dictionaryWithObjects:@[@"apple",@"orange"] forKeys:@[@"1",@"2"]];
//添加一个元素
NSDictionary *dic5 = [NSDictionary dictionaryWithObject:@"apple" forKey:@"1"];
//快速创建
//@{键:值,键:值}
NSDictionary *dic6 = @{@"1": @"apple",@"2":@"orange"};
//取元素
NSString *string = [dic3 objectForKey:@"1"];
NSLog(@"string:%@",string);
//取所有的key
NSArray *keys = [dic3 allKeys];
//取所有的value
NSArray *values = [dic3 allValues];
//遍历
for (int i = 0; i<[dic3 count]; i++) {
NSString *key = [NSString stringWithFormat:@"%i",i+1];
NSLog(@"%@",[dic3 objectForKey:key]);
}
//快速遍历
//in前面是key
for (NSString *key in dic3) {
NSLog(@"%@",[dic3 objectForKey:key]);
}
/**
例子
通讯录
张三 12345 男
李四 234344 女
王五 08909 男
李红 244344 女
*/
NSDictionary *dic7 = @{@"张三":@[@"12345",@"男"],
@"李四":@[@"234564",@"女"],
@"王五":@[@"08909",@"男"],
@"李红":@[@"244344",@"女"]
};
for (NSString *key in dic7) {
//快速取值:dic7[写键]
NSArray *array = dic7[key];
NSLog(@"%@的电话是%@,性别是:%@",key,array[0],array[1]);
}
//可变字典
//创建 空可变字典
NSMutableDictionary *mutDic1 = [[NSMutableDictionary alloc]initWithCapacity:20];
//操作
//增
[mutDic1 addEntriesFromDictionary:dic7];
NSLog(@"mutDic1:%@",mutDic1);
//增加一个
[mutDic1 setObject:@"banana" forKey:@"55"];
// //删
// [mutDic1 removeObjectForKey:@"55"];
//
// //删多个
// [mutDic1 removeObjectsForKeys:@[@"1",@"2"]];
//
// //删全部
// [mutDic1 removeAllObjects];
//改
[mutDic1 setObject:@"pair" forKey:@"55"];
NSLog(@"mutDic1:%@",mutDic1);
//查
//遍历
NSMutableDictionary *mutDic8 = [NSMutableDictionary dictionary];
[mutDic8 setObject:@[@"2222",@"男"] forKey:@"张三"];
}
return 0;
}