您的位置:首页 > 移动开发 > Objective-C

OBjectve-c 基本数据类型 总结

2015-11-13 00:00 405 查看
#import <Foundation/Foundation.h>
#import "Person.h"
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);

//查找

NSString * wo=@"zhangxiaong";
NSRange weizhi=[wo rangeOfString:@"zhang"];

NSLog(@"location:%ld length:%ld ",weizhi.location ,weizhi.length );

NSString * fanhui=[wo substringWithRange:weizhi];

NSLog(@"%@",fanhui);

//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];

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: