OC-FUNDATION-NSDate&NSArray&NSMutableArray

1.NSDate

1.1时间对比(3种)

NSDate *time1 = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
NSDate *time2 = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
NSDate *earlierDate = [time1 earlierDate:time2];//获取两个时间中较早的一个
NSLog(@"%@",earlierDate);
NSDate *laterDate = [time1 laterDate:time2];
NSLog(@"%@",laterDate);
if ([time1 isEqualToDate:time2]) {//判断两个事假是否相同,超级敏感,精确到纳秒
    NSLog(@"两个时间相同");
}
else
    NSLog(@"两个时间不同");
//从键盘输入一个身份证号码,输出该身份证主人的年龄
NSString *IDCard = [NSString stringWithContentsOfFile:@"/Users/tarena/Desktop/IDCard" encoding:NSUTF8StringEncoding error:nil];
NSString *year = [IDCard substringWithRange:NSMakeRange(6, 4)];
int age = [year intValue] - 2016;
NSLog(@"%d",-age);
//判断某一时间是不是指定时间       
NSString *date = [NSString stringWithFormat:@"%d年%d月%d日 %d:%d:%d",2016,1,1,12,30,30];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.dateFormat = @"yyyy年MM月dd日 HH:mm:ss";
NSString *date1 = [df stringFromDate:[NSDate date]];
if ([date isEqualToString:date1]) {
    NSLog(@"时间相同");
}
else
    NSLog(@"时间不同");

2.NSArray

2.1创建方法(5种)

//创建方法
NSArray *array1 = [NSArray array];//空数组,没有意义因为NSArray是不可变数组,数组元素既不能添加也不能删除
NSArray *array2 = [NSArray arrayWithObject:@"one"];//只有一个元素的数组,意义也不太大,可以直接使用变量
NSArray *array3 = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];//有多个元素的数组
NSLog(@"%@",array3);
NSArray *array4 = @[@"one",@"two",@"three"];//优化后的方法,最常用的
NSLog(@"%@",array4);
NSArray *array5 = [NSArray arrayWithArray:array4];//生成数组的副本
NSLog(@"%@",array5);

2.2求数组长度
2.3根据指定下标获取元素值

//求数组元素个数
NSUInteger length = array4.count;
NSLog(@"%lu",length);

//求数组指定下标对应的元素值
NSString *str = [array4 objectAtIndex:2];
NSLog(@"%lu",[str length]);
NSLog(@"%@",array4[2]);//优化后的方法,最常用的
//      NSLog(@"%@",array4[100]);//当数组越界时程序崩溃
str = [array4 lastObject];
NSLog(@"%@",str);
NSLog(@"%@",[array4 firstObject]);

2.4根据元素值求下标

    //根据元素值求其在数组中的下标
NSUInteger index = [array4 indexOfObject:@"two"];
NSLog(@"%lu",index);
NSLog(@"%lu",[array4 indexOfObject:@"5"]);
if (index >= array4.count) {
    NSLog(@"该元素不在数组中");
}

2.5遍历(2种)

//遍历方法
NSLog(@"%@",array4);
for (int i = 0; i < array4.count; i++) {
    NSLog(@"%@",array4[i]);
}
NSLog(@"---------");
for (NSString* str in array4) {
    NSLog(@"%@",str);
}

NSArray *array6 = @[@"one",@"two",@"three"];
for (NSString *str in array6) {
   if ([str isEqualToString:@"two"]) {
NSLog(@"%@",str);
   }
}
//汉语转换成拼音
NSMutableString *str = [NSMutableString stringWithString:@"计算机语言"];
if (CFStringTransform((__bridge CFMutableStringRef)str, 0, kCFStringTransformMandarinLatin, NO)) {//该函数用来把可变字符串中文转换成带音标的汉语拼音
    NSLog(@"%@",str);
}
if (CFStringTransform(((__bridge CFMutableStringRef)str), 0, kCFStringTransformStripDiacritics, NO)) {//不带音标
    NSLog(@"%@",str);
}

2.6数组的排序
SHStudent.h:

#import <Foundation/Foundation.h>

@interface SHStudent : NSObject
@property NSString* name;
@property int age;
@property NSMutableString *pinYin;
-(id)initWithName:(NSString*)name andAge:(int)age;
+(id)studentWithName:(NSString*)name andAge:(int)age;
-(NSComparisonResult)comparepinYin:(SHStudent*)other;//按姓名的拼音排序
-(NSComparisonResult)comparepinAge:(SHStudent*)other;//按年龄排序
@end

SHStudent.m

#import "SHStudent.h"

@implementation SHStudent
-(id)initWithName:(NSString *)name andAge:(int)age
{
    if (self = [super init]) {
self.name = name;
self.pinYin = [NSMutableString stringWithString:name];
CFStringTransform(((__bridge CFMutableStringRef)self.pinYin), 0, kCFStringTransformMandarinLatin, NO);
self.age = age;
    }
    return self;
}
+(id)studentWithName:(NSString *)name andAge:(int)age
{
    __autoreleasing SHStudent *stu = [[SHStudent alloc]initWithName:name andAge:age];
    return stu;
}

-(NSString *)description
{
    return [NSString stringWithFormat:@"name:%@ age:%d",self.name,self.age];
}

-(NSComparisonResult)comparepinYin:(SHStudent *)other
{
    return [self.pinYin compare:other.pinYin];
}

-(NSComparisonResult)comparepinAge:(SHStudent *)other
{
    if (self.age < other.age) {
return NSOrderedAscending;
    }
    else if (self.age > other.age){
return NSOrderedDescending;
    }
    return NSOrderedSame;
}
@end

main:

#import <Foundation/Foundation.h>
#import "SHStudent.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
SHStudent *stu1 = [SHStudent studentWithName:@"张三" andAge:18];
SHStudent *stu2 = [SHStudent studentWithName:@"李四" andAge:20];
SHStudent *stu3 = [SHStudent studentWithName:@"王五" andAge:19];
SHStudent *stu4 = [SHStudent studentWithName:@"赵六" andAge:25];
SHStudent *stu5 = [SHStudent studentWithName:@"钱七" andAge:17];
NSArray *array = @[stu1,stu2,stu3,stu4,stu5];
NSArray *sorted = [array sortedArrayUsingSelector:@selector(comparepinYin:)];
for(SHStudent *str in sorted)
    NSLog(@"%@",str);
    }
    return 0;
}

2.7副本中的数组元素,深浅拷贝生成方法

SHStudent *stu = [SHStudent studentWithName:@"张三" andAge:19];
SHStudent *stu2 = [SHStudent studentWithName:@"李四" andAge:15];
SHStudent *stu3 = [SHStudent studentWithName:@"王五" andAge:20];
SHStudent *stu4 = [SHStudent studentWithName:@"赵六" andAge:25];

NSArray *stu5 = @[stu,stu2,stu3,stu4];
NSArray *copied = [[NSArray alloc]initWithArray:stu5 copyItems:NO];
NSLog(@"%p",stu5[0]);
NSLog(@"%p",stu);
NSLog(@"%p",copied[0]);
NSLog(@"------------");
NSArray *deepCopied = [[NSArray alloc]initWithArray:stu5 copyItems:YES];
NSLog(@"%p",deepCopied[0]);
NSLog(@"%p",stu5[0]);
NSLog(@"%p",stu);

运行结果:
2016-08-26 16:12:52.425 day21_08[1878:275009] 0x100102950
2016-08-26 16:12:52.427 day21_08[1878:275009] 0x100102950
2016-08-26 16:12:52.427 day21_08[1878:275009] 0x100102950
2016-08-26 16:12:52.427 day21_08[1878:275009] ————
2016-08-26 16:12:52.427 day21_08[1878:275009] 0x100100340
2016-08-26 16:12:52.427 day21_08[1878:275009] 0x100102950
2016-08-26 16:12:52.427 day21_08[1878:275009] 0x100102950

3.NSMutableArray

3.1是可变数组,是NSArray的子类
3.2创建方法

//创建方法
NSMutableArray *array1 = [NSMutableArray array];//空数组有意义
NSMutableArray *array2 = [NSMutableArray arrayWithCapacity:100];//预估值
NSMutableArray *array3 = @[@"one",@"two"@"three"];//会退化成NSArray,不能用
NSMutableArray *array4 = [NSMutableArray arrayWithObjects:@"one",@"two",@"three", nil];//标准方法,最常用
NSLog(@"%@",array4);

3.3添加方法(2种)

//添加方法
[array4 addObject:@"four"];//在最后追加一个元素
NSLog(@"%@",array4);
[array4 insertObject:@"five" atIndex:1];//在指定下标处插入元素
NSLog(@"%@",array4);     

3.4修改方法(2种)

[array4 replaceObjectAtIndex:1 withObject:@"six"];//修改指定下标的元素
NSArray *replace = @[@"five",@"six",@"seven",@"eight",@"nine"];
[array4 replaceObjectsInRange:NSMakeRange(2, 2) withObjectsFromArray:replace];//批量修改

3.5删除方法(6种)

//删除
[array4 removeLastObject];//最后一个
NSLog(@"%@",array4);
[array4 removeObjectAtIndex:1];//指定下标
NSLog(@"%@",array4);
[array4 insertObject:@"six" atIndex:3];
NSLog(@"%@",array4);
[array4 removeObject:@"six"];//删掉所有指定元素
NSLog(@"%@",array4);
[array4 removeObjectsInRange:NSMakeRange(1, 2)];//从下标1号位置开始删掉2个元素
NSLog(@"%@",array4);
NSArray *del = @[@"seven",@"three",@"one",@"nine"];
[array4 removeObjectsInArray:del];
NSLog(@"%@",array4);
[array4 removeAllObjects];
NSLog(@"%lu",array4.count);

思考练习

有一个学校,有两个学院,每个学院有两个班,每个班有两个学生。用数组分别建立学校、学院、班
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
解析:

SHStudent *stu1 = [SHStudent studentWithName:@"aaaa" andAge:20];
SHStudent *stu2 = [SHStudent studentWithName:@"bbbb" andAge:21];
SHStudent *stu3 = [SHStudent studentWithName:@"cccc" andAge:20];
SHStudent *stu4 = [SHStudent studentWithName:@"dddd" andAge:20];
SHStudent *stu5 = [SHStudent studentWithName:@"eeee" andAge:20];
SHStudent *stu6 = [SHStudent studentWithName:@"ffff" andAge:20];
SHStudent *stu7 = [SHStudent studentWithName:@"gggg" andAge:20];
SHStudent *stu8 = [SHStudent studentWithName:@"hhhh" andAge:20];
NSMutableArray *class1 = [NSMutableArray arrayWithObjects:stu1,stu2, nil];
NSMutableArray *class2 = [NSMutableArray arrayWithObjects:stu3,stu4, nil];

NSMutableArray *class3 = [NSMutableArray arrayWithObjects:stu5,stu6, nil];
NSMutableArray *class4 = [NSMutableArray arrayWithObjects:stu7,stu8, nil];

NSMutableArray *institue1 = [NSMutableArray arrayWithObjects:class1,class2, nil];
NSMutableArray *institue2 = [NSMutableArray arrayWithObjects:class3,class4, nil];

NSMutableArray *school = [NSMutableArray arrayWithObjects:institue1,institue2, nil];

for (NSMutableArray *str in school) {
    for (NSMutableArray *str2 in str) {
for (NSMutableArray *str3 in str2) {
    NSLog(@"%@",str3);
}
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值