UIDateTest
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
字符串可变 不可变
NSMutableString
NSString
数组 可变 不可变
NSMutableArray
NSArray
字典 可变 不可变
NSMutableDictionary
NSDictionary
NSSet
NSNumber封装C的基本数据类型
NSValue可以封装结构体
NSDate 表示日期
结构体
NSRange NSPoint NSSize NSRect
*/
//不可变的字符串定义 基本上有三种方式 直接定义、实例化、相对应的静态方法定义
NSString *stringFirst = @"abcd";
NSString *stringSend = [[NSString alloc]initWithFormat:@"abcd"];
//字符串的分割: substringFromIndex:通过第几个字符开始到结尾的分割 、 substringToIndex从0开始到指定的字符分割、获得字符串中的某个字符方法:characterAtIndex、以特殊字符分割的方法:componentsSeparatedByString:
NSString *stringTir =[stringFirst substringFromIndex:2];
NSLog(@"%@",stringTir);
unichar ca = [stringFirst characterAtIndex:3];
NSLog(@"%c",ca);
NSArray *array=[stringFirst componentsSeparatedByString:@","];
NSLog(@"%@",array);
//转化成小写的方法:lowercaseString
NSString *lowerString= [stringFirst lowercaseString];
//转化成大写的方法:uppercaseString
NSString *upperString = [stringFirst uppercaseString];
NSLog(@"%@,%@",lowerString,upperString);
//首字母大写的方法:capitalizedString
NSString *capString = [stringFirst capitalizedString];
//判断是否以某字符串开头的方法:hasPrefix 或者结尾的方法:hasSuffix
NSLog(@"%i %i",[stringFirst hasPrefix:@"ab"],[stringFirst hasSuffix:@"de"]);
//用i去替换字符串中的b
NSLog(@"%@", [stringFirst stringByReplacingOccurrencesOfString:@"b" withString:@"i"]);
//字符串比较的方法compare: 0是相等 1前者大于后者 -1 后者大于前者
NSLog(@"compare:%ld",[stringFirst compare:stringSend]);
//NSMutableString
//创建一个可变的字符串
NSMutableString *strMu = [NSMutableString string];
//给可变字符串增加字符串
[strMu appendString:@"abcd"];
//删除可变字符串中的某个区间
[strMu deleteCharactersInRange:NSMakeRange(1, 1)];
//C里面结构体的写法
NSRange range = {1,2};
//修改字符串中的某个字符 返回了一个新的字符串
[strMu stringByReplacingCharactersInRange:range withString:@"de"];
NSLog(@"strMu:%@",strMu);
NSMutableString *strMuII;
Person *person= [[Person alloc]init];
person->string = [NSMutableString stringWithCapacity:10];
strMuII =[NSMutableString stringWithCapacity:10];
[strMuII appendString:@"hhh"];
NSLog(@"strMuII:%@",strMuII);
//NSArray 数组的创建 数组的遍历 数组元素的类型有对象类型 NSNumber NSString NSDictionary NSArray NSValue
NSArray *arrayI = @[@1,strMuII,person];
NSArray *arrayII = [[NSArray alloc]init];
NSArray *arrayIII = [NSArray array];
NSLog(@"arrayI%@",arrayI);
//遍历:
for (int i=0; i<[arrayI count]; i++) {
NSLog(@"for method:%@", [arrayI objectAtIndex:i]);
// arrayI[i];c中某个数组元素的写法
}
for (id name in arrayI) {
NSLog(@"for in method:%@",name);
}
//将数组中的元素拼接起来
NSString *strJoin = [arrayI componentsJoinedByString:@""];
NSLog(@"strjoin:%@",strJoin);
// 不可以通过KVC修改某元素的值
// [arrayI setValue:@2 forKey:arrayI[0]];
// [arrayI subarrayWithRange:NSMakeRange(1, 2)];
// NSLog(@"+++%@",arrayI);
//给不可变的数组追加元素会生成新的数组
[arrayI arrayByAddingObject:@"oo"];
//NSMutableArray
//定义
NSMutableArray *arrayMuI = [[NSMutableArray alloc]initWithArray:arrayI];
//在下面方法中需要注意的是nil是结尾 不是空值
// NSMutableArray *arrayMuII = [NSMutableArray arrayWithObjects:@"jack",[[NSNull alloc]init],nil,[NSNull null],nil,nil];
NSMutableArray *arrayMuII = [NSMutableArray arrayWithObjects:@"jack",@"rose",@"hilary", nil];
//增加元素
[arrayMuI addObject:@"abcd"];
[arrayMuI addObjectsFromArray:@[@"123"]];
//删除元素
// [arrayMuI removeObjectAtIndex:0];
NSLog(@"arrayMuI:%@",arrayMuI);
//修改元素的值 如果是直接赋值 需要用C的写法
arrayMuI[0] = @"ooo";
//通过数组个元素找下标
[arrayMuI indexOfObject:@"ooo"];
NSRange rangeMu = NSMakeRange(1, 3);
//遍历数组
[arrayMuI subarrayWithRange:rangeMu];
[arrayMuII sortUsingSelector:@selector(compare:)];
NSLog(@"arrayMuII%@",arrayMuII);
//字典 NSDictionary NSMutableDictionary
//创建一个不可变的字典
NSDictionary *dict = @{@"name":@"alice"};
NSDictionary *dictI = nil;
//初始化的时候有一个元素
dictI = [NSDictionary dictionaryWithObject:@"age" forKey:@"20"];
//初始化的时候多个元素的
NSArray *keyArray = @[@"name",@"age"];
NSArray *valueArray = @[@"hilary",@"20"];
NSDictionary *dictII = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
NSDictionary *dictIII = @{dict:@"name"};
//遍历字典时需注意:element表示的是字典的键
for (id element in dictIII) {
NSLog(@"dictIII字典的遍历Key%@,Value:%@",element,dictIII[element]);
}
for (id element in dictII) {
// NSLog(@"%@",element);
}
// [dictII setValue:@"24" forKey:@"age"];
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
[mutableDict addEntriesFromDictionary:dictII];
[mutableDict setValue:@"24" forKey:@"age"];
[mutableDict setObject:@"jack" forKey:@"name"];
//删除某个元素
[mutableDict removeObjectForKey:@"name"];
for (id element in mutableDict) {
NSLog(@"%@ %@",element,mutableDict[element]);
}
//判断这个字典里面存不存在某个键
if ([[mutableDict allKeys] containsObject:@"age"]) {
NSLog(@"存在这个键");
}
else{
NSLog(@"不存在这个键");
}
//枚举 字典通过keyEnumerator方法申请枚举器 数组申请枚举器通过objectEnumerator
NSEnumerator *enumer =[mutableDict keyEnumerator];
id name;
//枚举的遍历
while (name = [enumer nextObject]) {
NSLog(@"name:%@",name);
}
//NSSet 无序的
NSSet *set = [NSSet setWithObjects:@"name",@"jack",@"1",@"2", nil];
NSLog(@"-----%@",set);
for (id name in set) {
NSLog(@"+++%@",name);
}
//NSValue封装结构体 NSNumber封装基本数据类型
NSNumber *number = @1;
NSNumber *numberI = [NSNumber numberWithInt:1];
// const int a=3;
//结构体 NSRect NSPoint NSSize NSRange
NSRect rect = NSMakeRect(0, 0, 0, 0);
rect.origin.x = 3;
rect.size.height = 100;
NSRange rangeII = NSMakeRange(1, 3);
NSSize size = NSMakeSize(10, 10);
NSPoint point = NSMakePoint(30, 60);
//NSValue封装OC中定义好的结构体
NSValue *valueII = [NSValue valueWithPoint:point];
NSValue *value = @1;
typedef struct DateII{
int year;
int Month;
int Day;
}myStruct;
//初始化结构体
myStruct setValue = {0};
setValue.year = 2015;
setValue.Month = 11;
//封装自定义的结构体
NSValue *valueStr = [NSValue value:&setValue withObjCType:@encode(myStruct)];
myStruct getValueSt = {0};
[valueStr getValue:&getValueSt];
NSLog(@"%i",getValueSt.year);
//转化成本地时间
NSDate *date = [[NSDate alloc]init];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *stringNow = [formatter stringFromDate:date];
NSLog(@"%@",stringNow);
//NSDate 时间差
NSDate *myDate = [NSDate date];
NSTimeInterval timeSecondes = [myDate timeIntervalSince1970];
NSLog(@"%f",[myDate timeIntervalSinceNow]);
NSLog(@"%f",timeSecondes);
//定义特定的日期
NSTimeInterval yestedaySeconds = 24*60*60;
NSDate *yesterDate = [NSDate dateWithTimeIntervalSinceNow:-yestedaySeconds];
NSLog(@"%@",yesterDate);
//两个日期的时间差
NSTimeInterval betweenSenconds = [yesterDate timeIntervalSinceDate:myDate];
NSLog(@"%f",betweenSenconds);
// NSData与NSString之间的转化
//NSString 转化成 NSData
NSData *data = [lowerString dataUsingEncoding:NSUTF8StringEncoding];
//NSData 转化成 NSString
NSString *stringData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//可变的数据流 可以追加
NSMutableData *mutableData = [[NSMutableData alloc]init];
[mutableData appendData:data];
}
UImage
- (void)viewDidLoad {
[super viewDidLoad];
_i=20;
//图片实例化方法一:
UIImageView * imageView = [ [ UIImageView alloc ] init ] ;
//图片实例化方法二:
UIImageView * imageViewI = [ [ UIImageView alloc ] initWithFrame: CGRectMake ( 10, 10, 200, 200 ) ] ;
//设置ImageView的内容
imageViewI.image = [ UIImage imageNamed:@"0" ] ;
//图片实例化方法三:
UIImageView * imageViewII = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed : @"4" ] ] ;
//设置imageView的大小
imageViewII.frame = CGRectMake ( 180, 100, 100, 100 ) ;
//图片实例化方法四:(图片两个状态)
_imageViewIII = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed:@"1" ] highlightedImage : [ UIImage imageNamed:@"2" ] ] ;
_imageViewIII.frame = CGRectMake ( 5,5, 100, 100 ) ;
//将图片添加到页面上
[ self.view addSubview:_imageViewIII ] ;
//添加按钮控制图片状态
UIButton * button = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 80, 80, 180, 30 ) ] ;
[ button setTitle : @"高亮切换" forState : UIControlStateNormal ] ;
button.backgroundColor = [ UIColor redColor ] ;
[ button addTarget : self action : @selector(gaoLiang) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// ========= 关于图片的手势 ========
_imageViewIIII = [ [ UIImageView alloc ] initWithImage : [ UIImage imageNamed:@"4" ] ];
_imageViewIIII.frame = CGRectMake(10, 150, 100, 100);
_imageViewIIII.backgroundColor=[UIColor redColor];//可以给图片添加背景颜色明确image和imageView的差别
//将图片添加到页面上
[self.view addSubview:_imageViewIIII];
//图片支持交互
_imageViewIIII.userInteractionEnabled = YES;
// 定义手势
UITapGestureRecognizer *gestur = [ [ UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(shouShi) ];
// 将图片添加手势
[_imageViewIIII addGestureRecognizer:gestur];
// =========== 图片旋转 ============
_rotationImageView = [[ UIImageView alloc]initWithImage:[ UIImage imageNamed:@"0" ] ] ;
_rotationImageView.frame=CGRectMake(200, 200, 100, 100);
[self.view addSubview:_rotationImageView];
//添加按钮控制图片状态
UIButton * buttonI = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 200, 150, 80, 30 ) ] ;
[ buttonI setTitle : @"图片旋转" forState : UIControlStateNormal ] ;
buttonI.backgroundColor = [ UIColor redColor ] ;
[ buttonI addTarget : self action : @selector(xuanZhuan) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonI];
// =========== 图片放缩 ============
_changeSizeImageView = [[ UIImageView alloc]initWithImage:[ UIImage imageNamed:@"2" ] ] ;
_changeSizeImageView.frame=CGRectMake(20, 300, 100, 100);
[self.view addSubview:_changeSizeImageView];
//添加按钮控制图片放大
UIButton * buttonBig = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 110, 320, 80, 30 ) ] ;
[ buttonBig setTitle : @"图片放大" forState : UIControlStateNormal ] ;
buttonBig.backgroundColor = [ UIColor redColor ] ;
[ buttonBig addTarget : self action : @selector(fangDa) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonBig];
//添加按钮控制图片缩小
UIButton * buttonSmall = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 110, 360, 80, 30 ) ] ;
[ buttonSmall setTitle : @"图片缩小" forState : UIControlStateNormal ] ;
buttonSmall.backgroundColor = [ UIColor redColor ] ;
[ buttonSmall addTarget : self action : @selector(suoXiao) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonSmall];
//====== 利用Frame控制缩放 ======
_changeFrameImageView = [[ UIImageView alloc]initWithImage:[ UIImage imageNamed:@"5" ] ] ;
_changeFrameImageView.frame=CGRectMake(120, 430, 100, 100);
[self.view addSubview:_changeFrameImageView];
//添加按钮控制图片放大
UIButton * frameBig = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 110, 600, 50, 30 ) ] ;
[ frameBig setTitle : @"放大" forState : UIControlStateNormal ] ;
frameBig.backgroundColor = [ UIColor redColor ] ;
[ frameBig addTarget : self action : @selector(zengDa) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:frameBig];
//添加按钮控制图片缩小
UIButton * frameSmall = [ [ UIButton alloc ] initWithFrame : CGRectMake ( 220, 600,50, 30 ) ] ;
[ frameSmall setTitle : @"缩小" forState : UIControlStateNormal ] ;
frameSmall.backgroundColor = [ UIColor redColor ] ;
[ frameSmall addTarget : self action : @selector(bianXiao) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:frameSmall];
// _imageViewIII.center=CGPointZero;//设置图片中心位置,CGPointZero是原点(0,0)
// [self.view bringSubviewToFront:imageViewI];//将控件移至最前方
// _imageViewIIII.contentMode=UIViewContentModeCenter;
// _imageViewIII.transform=CGAffineTransformMakeTranslation(100, 200);
}
//添加按钮控制图片放大回调方法
-(void)fangDa{
_changeSizeImageView.transform=CGAffineTransformMakeScale(2, 5);
}
//添加按钮控制图片缩小回调方法
-(void)suoXiao{
_changeSizeImageView.transform=CGAffineTransformMakeScale(0.2, 0.5);
}
//图片放大回调方法
-(void)zengDa{
CGRect bonus = _changeFrameImageView.bounds;//图片中心为中点
bonus.size.width += 30;
bonus.size.height += 30;
_changeFrameImageView.bounds = bonus;
}
//图片缩小回调方法
-(void)bianXiao{
CGRect frame = _changeFrameImageView.frame;//图片左上角为中心
frame.size.height -= 30;
frame.size.width -= 30;
_changeFrameImageView.frame = frame;
}
//为切换高亮添加的方法
-(void)gaoLiang{
if (_imageViewIII.highlighted==YES) {
_imageViewIII.highlighted=NO;
}else{
_imageViewIII.highlighted=YES;
}
}
-(void)shouShi{
NSLog(@"图片手势方法。。。");
}
//点击图片旋转按钮调用的自定义方法,其中设置图片旋转多少
-(void)xuanZhuan{
_i+=20;
_rotationImageView.transform=CGAffineTransformMakeRotation(M_PI*_i/180);
// 这样的旋转是以图片中心为旋转中心的,旋转角度为:M_PI*?/180中?位置
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}