IOS学习日志(OC)1.5

来源:互联网 发布:excel数据有效性在哪 编辑:程序博客网 时间:2024/05/22 19:48

1.NSDate

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        //获取世界标准时的方法        NSDate *date=[[NSDate alloc]init];        NSLog(@"%@",date);        NSDate *date1=[NSDate date];        NSLog(@"%@",date1);        //如何获取本地时间        NSTimeZone *zone=[NSTimeZone systemTimeZone];        NSInteger interval=[zone secondsFromGMTForDate:date];        NSDate *localTime=[date dateByAddingTimeInterval:interval];        NSLog(@"%@",localTime);        NSDate *date2=[NSDate dateWithTimeIntervalSinceNow:30];//得到比当前时间快30s        NSLog(@"%@",date2);        NSDate *date3=[NSDate date];        NSTimeInterval seconds=[date3 timeIntervalSince1970];//从1970年1月1日0时到现在的秒数        NSLog(@"%lf",seconds);        NSDate *date4=[NSDate dateWithTimeIntervalSinceNow:-60];        NSTimeInterval seconds1=[date4 timeIntervalSinceNow];//指定时间到当前时间的秒差        NSLog(@"%lf",seconds1);        NSDate *date5=[NSDate dateWithTimeIntervalSinceNow:60*60*24];        NSLog(@"%@",date5);        NSDate *date6=[NSDate dateWithTimeIntervalSinceNow:60*60*24];        NSLog(@"%@",date6);        NSTimeInterval seconds2=[date5 timeIntervalSinceDate:date6];//date5和date6两个指定时间时间的总秒数        NSLog(@"%lf",seconds2);        //对比时间        NSDate *earlierDate=[date5 earlierDate:date6];//获取较早的时间        NSLog(@"%@",earlierDate);        NSDate *laterDate=[date5 laterDate:date6];//获得较晚的时间        NSLog(@"%@",laterDate);        if ([date5 isEqualToDate:date6]) {            NSLog(@"两者时间相等");        }        else        {            NSLog(@"两者时间不相等");        }        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//时间格式模版        dateFormatter.dateFormat=@"yyyy年MM月dd日hh:mm:ss";        NSString *strDate=[dateFormatter stringFromDate:date];        NSLog(@"%@",strDate);        }    return 0;}

2.NSArray(常量)

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        //创建        NSArray *array=[NSArray array];//用工厂方法创建了一个空数组,没有意义,指向常量数组,不能删除添加元素,更不能修改元素,但可以指向新的对象        NSArray *array1=[NSArray arrayWithObject:@"one"];//只有一个元素的数组        NSArray *array2=[NSArray arrayWithObjects:@"one",@"two",@"three", nil];//nil表示数组元素结束了,有多个数组元素        NSArray *array3=@[@"one",@"two",@"three"];//优化创建,现在最常用的        NSLog(@"%@",array3);        NSArray *array4=[NSArray arrayWithArray:array3];//用数组创建一个数组        NSLog(@"%@",array4);        //长度        NSUInteger length=[array4 count];        NSLog(@"%lu",length);        //获取数组元素        NSString *str=[array4 objectAtIndex:0];//下标为0的时候,得到的是数组第一个元素,经典方法,现在不用        NSLog(@"%@",str);        str=array4[1];//现代方法        NSLog(@"%@",str);        str=[array4 lastObject];//得到数组里的最后一个元素,在不知道有多少个的情况下获取最后一个        NSLog(@"%@",str);        //数组元素的类型可以不一致,很少使用        str=[array4 firstObject];//获取第一个元素        NSLog(@"%@",str);        NSLog(@"------------");        NSArray *array5=[NSArray arrayWithObjects:@"one",[NSNumber numberWithInt:10],[NSDate date], nil];        NSLog(@"%@",array5);        //查找制定元素在数组中的下标        NSUInteger index=[array4 indexOfObject:@"two"];        NSLog(@"%lu",index);        index=[array4 indexOfObject:@"5"];//当指定的数组元素在数组中不存在时,返回的下标是垃圾值        NSLog(@"%lu",index);        //遍历数组        for (int i=0; i<[array4 count]; i++) {            NSLog(@"%@",array4[i]);        }        for (NSString *str in array4)//将array4中的元素一个一个放到str中,遍历速度快,要求数组里的元素都要求是字符串        {            NSLog(@"%@",str);        }    }    return 0;}

2015-12-23 22:36:06.956 day08_02NSArray[2557:2397508] (
one,
two,
three ) 2015-12-23 22:36:06.957 day08_02NSArray[2557:2397508] (
one,
two,
three ) 2015-12-23 22:36:06.957 day08_02NSArray[2557:2397508] 3 2015-12-23 22:36:06.957 day08_02NSArray[2557:2397508] one 2015-12-23
22:36:06.957 day08_02NSArray[2557:2397508] two 2015-12-23 22:36:06.957
day08_02NSArray[2557:2397508] three 2015-12-23 22:36:06.957
day08_02NSArray[2557:2397508] one 2015-12-23 22:36:06.958
day08_02NSArray[2557:2397508] ———— 2015-12-23 22:36:06.962
day08_02NSArray[2557:2397508] (
one,
10,
“2015-12-23 14:36:06 +0000” ) 2015-12-23 22:36:06.962 day08_02NSArray[2557:2397508] 1 2015-12-23 22:36:06.962
day08_02NSArray[2557:2397508] 9223372036854775807 2015-12-23
22:36:06.963 day08_02NSArray[2557:2397508] one 2015-12-23 22:36:06.963
day08_02NSArray[2557:2397508] two 2015-12-23 22:36:06.963
day08_02NSArray[2557:2397508] three 2015-12-23 22:36:06.963
day08_02NSArray[2557:2397508] one 2015-12-23 22:36:06.963
day08_02NSArray[2557:2397508] two 2015-12-23 22:36:06.963
day08_02NSArray[2557:2397508] three Program ended with exit code: 0


简单练习

现在有一些数据,它们是整型数据10、字符型数据‘a’、单精度浮点型数据10.1f和自定义类TRStudent的一个对象,将它们存放在数组NSArray中。

#import <Foundation/Foundation.h>#import "TRStudent.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        TRStudent *stu=[[TRStudent alloc]init];        stu.name=@"jimmy";        NSLog(@"%@",stu);//调用description,如果没写description、OC不知道你这个类里面定义什么东西了,所以只返回地址        NSArray *array=@[[NSNumber numberWithInt:10],[NSNumber numberWithChar:'a'],[NSNumber numberWithFloat:10.1f],stu];        NSLog(@"%@",array);    }    return 0;}
#import <Foundation/Foundation.h>@interface TRStudent : NSObject@property NSString* name;@end
#import "TRStudent.h"@implementation TRStudent-(NSString *)description{    NSString *str=[NSString stringWithFormat:@"name:%@",self.name];    return str;}@end2015-12-23 22:58:46.687 day08向数组中存放数据[2633:2530169] name:jimmy2015-12-23 22:58:46.688 day08向数组中存放数据[2633:2530169] (    10,    97,    "10.1",    "name:jimmy")Program ended with exit code: 0

3.数组排序

#import <Foundation/Foundation.h>#import "TRInteger.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        TRInteger *i1=[[TRInteger alloc]initWithInteger:10];        TRInteger *i2=[[TRInteger alloc]initWithInteger:3];        TRInteger *i3=[[TRInteger alloc]initWithInteger:8];        TRInteger *i4=[[TRInteger alloc]initWithInteger:17];        TRInteger *i5=[[TRInteger alloc]initWithInteger:5];        NSArray *i=@[i1,i2,i3,i4,i5];//数组中所有元素必须是同一类型        NSArray *sorted=[i sortedArrayUsingSelector:@selector(compareInt:)];//将函数的方法选择器传到了sortedArrayUsingSelector这个方法里,这个方法就会把i这个数组进行排序,该类型必须有一个compare...的函数,用于对比两个对象的        for (TRInteger *i in sorted)//如果不写description得到的将是所有对象的地址,要写成数组元素的类型,i的元素是指向TRInteger的类型        {            NSLog(@"%@",i);        }    }    return 0;}
#import <Foundation/Foundation.h>@interface TRInteger : NSObject@property int integer;-(id)initWithInteger:(int)integer;-(NSComparisonResult)compareInt:(TRInteger*)other;@end
#import "TRInteger.h"@implementation TRInteger-(id)initWithInteger:(int)integer{    if (self=[super init]) {        self.integer=integer;    }    return self;}-(NSComparisonResult)compareInt:(TRInteger *)other//枚举类型,包含return 回来的三个元素{    if (self.integer<other.integer) {        return NSOrderedAscending;    }    else if(self.integer>other.integer)    {        return NSOrderedDescending;    }    else    {        return NSOrderedSame;    }}-(NSString *)description{    NSString *str=[NSString stringWithFormat:@"%d",self.integer];    return str;}@end
比较两个字符串的时候可以用这个方法-(NSComparisonResult)compareName:(Student*)other{    return [self.name compare:other.name];}

4.copy

#import <Foundation/Foundation.h>#import "TRStudent.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        TRStudent *stu1=[[TRStudent alloc]initWithName:@"jimmy" andID:1];        TRStudent *stu2=[[TRStudent alloc]initWithName:@"tom" andID:2];        TRStudent *stu3=[[TRStudent alloc]initWithName:@"linda" andID:3];        NSArray *stu=@[stu1,stu2,stu3];        NSArray *copiedStu=[[NSArray alloc]initWithArray:stu copyItems:NO];//浅拷贝        NSLog(@"stu[0]->%p",stu[0]);        NSLog(@"copiedStu[0]->%p",copiedStu[0]);        NSArray *deepCopiedStu=[[NSArray alloc]initWithArray:stu copyItems:YES];//深拷贝        NSLog(@"stu[0]->%p",stu[0]);        NSLog(@"copiedStu[0]->%p",deepCopiedStu[0]);    }    return 0;}
#import <Foundation/Foundation.h>@interface TRStudent : NSObject<NSCopying>@property NSString* name;@property int ID;-(id)initWithName:(NSString*)name andID:(int)ID;@end
#import "TRStudent.h"@implementation TRStudent-(id)initWithName:(NSString *)name andID:(int)ID{    if (self=[super init]) {        self.name=name;        self.ID=ID;    }    return self;}-(id)copyWithZone:(NSZone *)zone{    TRStudent *stu=[[TRStudent allocWithZone:zone]initWithName:self.name andID:self.ID];    return stu;}@end
0 0