生成100个 "20180520" 这样的时间字符串 写入txt文件
主要想记录一下 1 . 写NSString 到txt 2. 数组的去重 3. 数组的截取 4. 数组分割
代码如下:
NSString *year = @"2018";
NSArray *momth = @[@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
NSArray *day = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31"];
NSMutableArray *dateStrArr = [NSMutableArray array];
for (int i = 0 ; i < 200; i++) {
NSString *momthStr = momth[arc4random()%momth.count];
NSString *dayStr = day[arc4random()%day.count];
if ([momthStr isEqualToString:@"02"] && [dayStr integerValue] > 28) {
continue;
}
//NSLog(@"%@%@%@",year,momthStr,dayStr);
NSString *dateStr = [NSString stringWithFormat:@"%@%@%@",year,momthStr,dayStr];
[dateStrArr addObject:dateStr];
//dateStr = [NSString stringWithFormat:@"\n%@",dateStr];
}
//去重
NSArray *result = [dateStrArr valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSLog(@"去重1之后 = %d",result.count);
//选100个
NSArray *finalArr = [result subarrayWithRange:NSMakeRange(0, 100)];
NSLog(@"去重1之后 = %d",finalArr.count);
NSString *final = [finalArr componentsJoinedByString:@"\n"];
//NSLog(@"%@",final);
//NSFileHandle文件
//1.创建文件
NSFileManager *fm = [NSFileManager defaultManager];
//NSString *str = @"this is a test.\n 666";
NSData *data = [final dataUsingEncoding:NSUTF8StringEncoding];
[fm createFileAtPath:@"/Users/liuwei/Desktop/date2.txt" contents:data attributes:nil];