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
}
?
轉載于:https://www.cnblogs.com/leonlincq/p/6169059.html
總結
以上是生活随笔為你收集整理的iOS:以前笔记,未整理版。太多了,先放着吧。。。。。。。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到西瓜是什么意思
- 下一篇: 梦到男朋友出轨是暗示吗