NSNumber、NSValue、NSDate、NSArray

2020-09-26  本文已影响0人  我不白先生

NSNumber
1.1用于将基本数据类型的常量或变量封装成OC类的对象
1.2封装方法:numberWith...//...表示你要封装的后面的数据类型如:numberWithInt:a
1.3拆封方法:...Value //...表示你要拆封的数据的数据类型如intValue

#import "ViewController.h"

#define PRINT(T,x) self.outputLabel.text = [NSString stringWithFormat:@"%"#T,x];

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void)print:(id)obj;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    int a = 10;
    NSNumber *i = [NSNumber numberWithInt:a];//封装:将基本数据类型的数据变成OC类的对象
    [self print:i];
    int a1 = 0;
    a1 = [i intValue];//拆封:将OC类的对象变成基本数据类型的数据
    PRINT(d, a1);
    
    float f= 3.14;
    NSNumber *f1 = [NSNumber numberWithFloat:f];
    [self print:f1];
    float f2 = 0;
    f2 = [f1 floatValue];
    PRINT(g, f2);
    
    double d = 2.68;
    NSNumber *d1 = [NSNumber numberWithDouble:d];
    [self print:d1];
    double d2 = 0;
    d2 = [d1 doubleValue];
    PRINT(g, d2);

    char ch = 'a';
    NSNumber *ch1 = [NSNumber numberWithChar:'a'];
    [self print:ch1];
    char ch2 = 'b';
    ch2 = [ch1 charValue];
    PRINT(c, ch)

    BOOL b = YES;
    NSNumber *b1 = [NSNumber numberWithBool:b];
    [self print:b1];
     BOOL b2 = NO;
    b2 = [b1 boolValue];
    PRINT(d, b2);   
}
-(void)print:(id)obj
{
    self.outputLabel.text = [NSString stringWithFormat:@"%@",obj];
}

@end

2.NSValue
2.1是NSNumber的父类
2.2用于封装结构体类型的变量

#import "ViewController.h"
typedef struct
{
    int x;
    int y;
}TRPoint;
typedef struct
{
    char ch;
    double d;
    
}TRMyData;

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    int a = 10;
    NSValue *a1 = [NSValue valueWithBytes:&a objCType:@encode(int)];//封装
    //encode能获得int这个数据类型的编号
    int a2 = 0;
    [a1 getValue:&a2];//getValue拆封
    self.outputLabel.text = [NSString stringWithFormat:@"%d",a2];
    TRPoint point;
    point.x = 10;
    point.y = 20;
    NSValue *value = [NSValue valueWithBytes:&point objCType:@encode(TRPoint)];//封装结构体变量
    TRPoint point1 = {0,0};
    [value getValue:&point1];//拆封结构体变量
    self.outputLabel.text = [NSString stringWithFormat:@"(%d,%d)",point1.x,point1.y];
    
    TRMyData data = {'c',9.8};
    NSValue *value1 = [NSValue valueWithBytes:&data objCType:@encode(TRMyData)];
    TRMyData data1 = {0,0};
    [value1 getValue:&data1];
    self.outputLabel.text = [NSString stringWithFormat:@"%c,%g",data1.ch,data1.d];

3.NSDate
3.1是OC中的日期实践类
3.2创建对象即可得到世界标准时间
3.3本地时间,需要通过三行格式代码转换
3.4获取指定时间
3.5时间间隔
3.6按指定格式得到时间
3.7时间对比
NSDate+TRLocalTime.m

@implementation NSDate (TRLocalTime)
-(id)localTime
{
    return [NSDate localTime];
}
+(id)localTime
{
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSUInteger integer = [zone secondsFromGMTForDate:[NSDate date]];
    NSDate *localTime = [[NSDate date] dateByAddingTimeInterval:integer];//这三行代码为获取本地时间的格式代码
    return localTime;
}@end

ViewController.m

#import "ViewController.h"
#import "NSDate+TRLocalTime.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@property (weak, nonatomic) IBOutlet UIDatePicker *date;
-(void)print:(id)obj;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //获取世界标准时间
    NSDate *date1 = [NSDate date];
    [self print:date1];
    //本地时间
//    NSTimeZone *zone = [NSTimeZone systemTimeZone];
//    NSUInteger integer = [zone secondsFromGMTForDate:[NSDate date]];
//    NSData *localTime = [[NSDate date] dateByAddingTimeInterval:integer];//这三行代码为获取本地时间的格式代码
    [self print:[NSDate localTime]];
    [self print:[date1 localTime]];
    //获取指定时间
    NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:120];//创建比当前时间快30秒的NSDate对象
    self.outputLabel.text = [NSString stringWithFormat:@"%@\n%@",date1,date2];
    NSDate *date3 = [NSDate dateWithTimeIntervalSinceNow:-30];//创建比当前时间慢30秒的NSDate对象
    self.outputLabel.text = [NSString stringWithFormat:@"%@\n%@",date1,date3];
   //时间间隔
    NSTimeInterval seconds = [date1 timeIntervalSince1970];//从1970年1月1日0时到现在经过的总秒数
    self.outputLabel.text = [NSString stringWithFormat:@"%g",seconds];
    NSDate *date4 = [NSDate dateWithTimeIntervalSinceNow:60];//date4比当前快一分钟的时间
    seconds = [date4 timeIntervalSinceNow];//指定时间与当前时间的秒数差
    self.outputLabel.text = [NSString stringWithFormat:@"%g",seconds];
    NSDate *time1 = [NSDate dateWithTimeIntervalSinceNow:-60 * 60 * 24];
    NSDate *time2 = [NSDate dateWithTimeIntervalSinceNow:60 * 60 *24];
    seconds = [time1 timeIntervalSinceDate:time2];//两个指定时间之间的总秒数
    self.outputLabel.text = [NSString stringWithFormat:@"%g",seconds];
    //按指定格式得到时间
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy年MM月dd日 HH:ss EEEE";//HH表示的24小时制
    df.dateFormat = @"yyyy年MM月dd日 hh:ss a EEEE";//hh表示的12小时制
    self.outputLabel.text =[df stringFromDate:[NSDate date]];
    //时间对比
    NSDate *earlierTime = [time1 earlierDate:time2];
    [self print:earlierTime];
    NSDate *laterTime = [time1 laterDate:time2];
    [self print:laterTime];
    if ([earlierTime isEqualToDate:time1])
    {
        [self print:@"两个时间相同"];
    }
    NSDate *date5 = [NSDate date];
    NSDate *date6 = [NSDate date];
    if([date5 isEqualToDate:date6])
    {
        [self print:@"两个时间相同"];
    }
    else
    {
        [self print:@"两个时间不同"];
    }
    self.date.date = date2;
}
-(void)print:(id)obj
{
    self.outputLabel.text = [NSString stringWithFormat:@"%@",obj];
}
- (IBAction)display:(UIButton *)sender {
    [self print:self.date.date];
}

@end

4.NSArray

4.1创建方法
4.2求数组长度
4.3根据下标求数组元素值
4.4根据元素值求数组下标
4.5数组的遍历
ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void)print:(id)obj;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //NSArray创建方法
    NSArray *array1 = [NSArray array];//空数组
    NSArray *array2 = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];//数组必须是oc的对象//创建有多个元素的数组
    [self print:array2];
    NSArray *array3 = @[@"one",@"two",@"three"];//优化方法
    [self print:array3];
    NSArray *array4 = [NSArray arrayWithArray:array3];//副本
    [self print:array4];
    //求数组长度
    self.outputLabel.text = [NSString stringWithFormat:@"%lu",array3.count];
    //根据下标求数组元素值
    NSString *str = [array3 objectAtIndex:0];
    [self print:str];
    [self print:array3[0]];
    //[self print:array3[10]];//下标越界,程序奔溃
    [self print:[array3 firstObject]];//firstObject第一个数组元素
    [self print:[array3 lastObject]];
    //根据元素值求数组下标
    NSUInteger index = [array3 indexOfObject:@"two"];
    [self print:[NSNumber numberWithUnsignedLong:index]];
    index = [array3 indexOfObject:@"five"];
    [self print:[NSNumber numberWithUnsignedLong:index]];
    if(index >= array3.count)
    {
        [self print:@"该元素不存在"];
    }
    //遍历
    NSMutableString *str1 = [NSMutableString stringWithCapacity:20];
    [str1 appendString:@"(\n"];
    for(int i = 0; i<array3.count;i++)
    {
        [str1 appendString:array3[I]];
        [str1 appendString:@"\n"];
    }
        [str1 appendString:@")"];
        self.outputLabel.text = str1;
    for(NSString *str in array3)
    {
        [str1 appendString:str];
        [str1 appendString:@"\n"];
    }
      self.outputLabel.text = str1;
}
-(void)print:(id)obj
{
    self.outputLabel.text = [NSString stringWithFormat:@"%@",obj];
}

@end

练习


每日一练遍历.png

新建学生类TRStudent
包括姓名,年龄以及标配方法然后让ViewController.m包含这个学生类

#import "ViewController.h"
#import "TRStudent.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *ooutputLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    TRStudent *stu1 = [TRStudent studentWithName:@"张三" andAge:18];
    TRStudent *stu2 = [TRStudent studentWithName:@"李四" andAge:20];
    TRStudent *stu3 = [TRStudent studentWithName:@"王五" andAge:19];
    TRStudent *stu4 = [TRStudent studentWithName:@"赵六" andAge:22];
    TRStudent *stu5 = [TRStudent studentWithName:@"钱七" andAge:21];
    TRStudent *stu6 = [TRStudent studentWithName:@"关羽" andAge:40];
    TRStudent *stu7 = [TRStudent studentWithName:@"张飞" andAge:38];
    TRStudent *stu8 = [TRStudent studentWithName:@"赵云" andAge:35];
    NSArray *class1 = @[stu1,stu2];
    NSArray *class2 = @[stu3,stu4];
    NSArray *class3 = @[stu5,stu6];
    NSArray *class4 = @[stu7,stu8];
    NSArray *college1 = @[class1,class2];
    NSArray *college2 = @[class3,class4];
    NSArray *school = @[college1,college2];
    NSMutableString *str = [NSMutableString stringWithCapacity:15 * 8];
    for(NSArray *college in school)
    {
        for(NSArray *class in college)
        {
            for (TRStudent *stu in class)
            {
                if(stu.age <= 20)
            {
                [str appendFormat:@"%@\n",stu];
            }
            }
        }
    }
    self.ooutputLabel.text = str;
}
@end
上一篇下一篇

猜你喜欢

热点阅读