• iOS:以前笔记,未整理版。太多了,先放着吧。。。。。。。


    1、**************************************************************** 单例共享 ****************************************************************

    单例 共享信息

    .m

    static OneT *newone = nil;

    +(instancetype)shalldata

    {

        if (newone == nil)

        {

            newone = [[OneT alloc]init];

            newone.data_zone = [NSMutableArray array];

        }

        return newone;

    }

    调用:

    OneT *one = [OneT shalldata];

    OneT *two = [OneT shalldata];

    2、**************************************************************** 日期 ****************************************************************

    日期时间

    //美国的时间

    NSDate *date = [[NSDate alloc]init];

    //时区

    NSTimeZone *myZone = [NSTimeZone systemTimeZone];

     //算时差 加上时差

    NSInteger interValTimer = [myZone secondsFromGMTForDate:date];

    NSDate *localDate = [date dateByAddingTimeInterval:interValTimer];

    NSLog(@"%@",localDate);

    //明天

    NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:24*60*60];

    //昨天

    NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:-ADAY];

    NSLog(@"date2 = %@",date2);

    //1970年

    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:0];

    NSLog(@"%@",date2);

    //参考日期2001年

    NSDate *date3 = [NSDate dateWithTimeIntervalSinceReferenceDate:0];

    NSLog(@"%@",date3);

    //nsdate -> nsstring 类型转换

    NSDate *date1 = [NSDate date];

    1.

    NSString *date1str = date1.description; //美国的时间

    2

    NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc]init];

    2.1

    [dateFormat1 setDateFormat:@"yyyy年MM月dd日 EEEE HH mm ss zz"];

    2.2

        [dateFormat1 setDateStyle:NSDateFormatterShortStyle];

       [dateFormat1 setTimeStyle:NSDateFormatterMediumStyle];

    NSString *datestr2 = [dateFormat1 stringFromDate:date1];

    NSLog(@"datestr2 = %@",datestr2);

    //获取所有时区的名字

    NSArray *timeZoneNames =  [NSTimeZone knownTimeZoneNames];

    //新建一个时区

    NSTimeZone *newZone = [NSTimeZone timeZoneWithName:@"Pacific/Fiji"];

    //新建一个时间格式

    NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc]init];

    //设置该时区

    [dateFormat2 setTimeZone:newZone];

    //设置时间格式

    [dateFormat2 setDateFormat:@"yyyy年MM月dd日 EEEE HH:mm:ss zz"];

    //打印

    NSString *dateString =  [dateFormat2 stringFromDate:[NSDate date]];

    NSLog(@"dateString = %@",dateString);

    //字符串转date

    NSString *dateStr4 = @"2016年07月27日 星期三 20:16:10 GMT+12";

    NSDate *date4 = [dateFormat2 dateFromString:dateStr4];

    NSLog(@"%@",date4);

    3、**************************************************************** 其他 **************************************************************** 

    NSRange newrange = NSMakeRange(1,5);

    NSValue *newvalue = [NSValue valueWithRange:newrange];

    NSLog(@"%@",newvalue);

    NSArray *newarray = [NSArray arrayWithObjects:newvalue, nil];

    NSLog(@"%@",newarray);

        

    NSRange newrange2 = [newvalue rangeValue];

    NSLog(@"%lu,%lu",newrange2.location,newrange2.length);

    //封装自定义的结构体

    struct MyPoint 

    {

    int x;

            int y;

    };

    struct MyPoint mypoint;

    mypoint.x = 10;

    mypoint.y = 100;

        

    NSValue *val2 = [NSValue value:&mypoint withObjCType:@encode(struct MyPoint)];

    NSLog(@"%@",val2);

        

    struct MyPoint mypoint2;

    [val2 getValue:&mypoint2];

    NSLog(@"mypoint2.x = %d,mypoint2.y = %d",mypoint2.x,mypoint2.y);

     9、

    @interface class_A:NSObject

    @end

    @implementation class_A

    @end

    @property int x;

    @synthesize x;

    [MyA setNumb:5]; 即使实例变量是小写,这个set后也要大写

    n=[MyA numb]; 

    [A B]  等价 A.B

    [self 该.m文件的方法],找不到再去父类找

    与文件.h.m同名

    @interface class_A:NSObject

    @end

    @interface class_B:class_A

    @ens

    继承里,class_B里的方法的可以用

    [self A的方法]

    [self A的变量]

    需要某文件的某些方法,可以用

    @class xxx.h

    如果xxx.h的某些方法访问不了,如init,还是要

    #import xxx.h

    重复包含的,其中一个.h可以用@class xxx(不用写.h),对应.m要#import xxx.h

    同名方法优先使用class_B的方法

    同名的方法,会自动判断[A set_value:(int)x]的类,如判断出A

    id x;

    x=任意变量、class类

    @try

    {

    }

    @catch

    {

    }

    @finally

    {

    }

    还有的@throw

    存储

    NSString *path = @"/Users/etcxm/test.plist";

        [dic writeToFile:path atomically:YES];

    NSArray  *A_data_R = [NSArray array];

    A_data_R = [NSArray arrayWithContentsOfFile:S_add];

    2、

    可变数组 copy 赋值给不可变数组。

    ==========================================================================

    //编码

    NSData *new_data = [s_text dataUsingEncoding:NSUTF8StringEncoding];

    //解码

    NSString *new_string = [[NSString alloc]initWithData:new_data encoding:NSUTF8StringEncoding];

    ==========================================================================

    //沙盒路径

    NSHomeDirectory()

     //工程目录

    [NSBundle mainBundle]

    ==========================================================================


        NSArray *data = [NSArray arrayWithObjects:@"zhang3",@"li4", nil];

        

        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"ceshi83.txt"];

        

        BOOL status = [NSKeyedArchiver archiveRootObject:data toFile:path];

        

        NSLog(@"%@",status?@"成功":@"失败");

        

        NSArray *r_data = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

        

        NSLog(@"%@",r_data);

     6、

    instancetype:返回与初始化类相同的类型。

    -(instancetype)initWith…

    {

    self=[super init];

    if(self)

    {

    }

    return self;

    }

    -(class_A *)initwithone:(int)a other:(int)b

    {

    self=[super init];

    if(self)

    {

    [self seta:a b:b ] ;

    }

    return self;

    }

    +(instancetype)robotWithName:(NSString*)r_name andage:(int)r_age

    {

    #if 0

        return [[self alloc]initWithName:(NSString*)r_name andage:(int)r_age];

    #else

        

        Robot *new_R = [[Robot alloc]initWithName:r_name andage:r_age];

        return new_R;

    #endif

    }

  • 相关阅读:
    外观模式
    建造者模式
    原型模式
    工厂模式
    单例模式
    设计模式入门
    SpringBoot-SpringMVC开发总结
    SpringBoot日志
    IDEA Basics
    Kafka基础学习
  • 原文地址:https://www.cnblogs.com/leonlincq/p/6169059.html
Copyright © 2020-2023  润新知