#import <Foundation/Foundation.h>api
#import "Person.h"數組
int main(int argc, const char * argv[]) {app
@autoreleasepool {ui
/*spa
字符串可變 不可變.net
NSMutableString3d
NSStringcode
數組 可變 不可變component
NSMutableArrayorm
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;
}