// Calculate the day of the week according to the date

+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate { NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @ "on Sunday," @ "on Monday," @ "on Tuesday," @ "on Wednesday," @ "on Thursday," @ "Friday," @ "on Saturday," nil]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"]; [calendar setTimeZone: timeZone]; NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit; NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate]; return [weekdays objectAtIndex:theComponents.weekday]; }Copy the code

// Converts to a time string

+ (nsstrings *) dateStringFromNumberTimer: (timerStr nsstrings *) {/ / into a Double Double t = [timerStr doubleValue]; / / calculate the distance of 1970 NSDate NSDate * date = [NSDate dateWithTimeIntervalSince1970: t]; //NSDateFormatter *df = [[[NSDateFormatter alloc] init] autoRelease]; NSDateFormatter *df = [[NSDateFormatter alloc] init]; df.dateFormat = @"yyyy-MM-dd HH:mm:ss"; Return [df stringFromDate:date]; }Copy the code

// Dynamically calculate the row height

// Dynamically calculate the actual height based on how much of the actual content of the string is fixed in the width and font size

+ (CGFloat)textHeightFromTextString:(NSString *)text width:(CGFloat)textWidth fontSize:(CGFloat)size{ if ([MZLUtility GetCurrentIOS] >= 7.0) {// after iOS7 /* First argument: preset space width fixed height preset a maximum value second argument: line spacing truncated if out of range third argument: NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:size]}; NSFontAttributeName:[UIFont systemFontOfSize:size]}; CGRect rect = [text boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil]; // Return rect.size. Height; }else {//iOS7 before /* 1. The first parameter set font fixed size 2. 3. Newline mode character newline */ CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:size] constrainedToSize:CGSizeMake(textWidth, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping]; return textSize.height; // Return the calculated line height}}Copy the code

// Obtain the iOS version number

+ (double)getCurrentIOS {
    return [[[UIDevice currentDevice] systemVersion] doubleValue];
}
+ (CGSize)getScreenSize {
    return [[UIScreen mainScreen] bounds].size;
}Copy the code

// Get the time difference between the current system time and the specified time string, pass in the target time string and format

+(NSString*)stringNowToDate:(NSString*)toDate formater:(NSString*)formatStr
{

NSDateFormatter *formater=[[NSDateFormatter alloc] init];
if (formatStr) {
    [formater setDateFormat:formatStr];
}
else{
    [formater setDateFormat:[NSString stringWithFormat:@"yyyy-MM-dd HH:mm:ss"]];
}
NSDate *date=[formater dateFromString:toDate];

return [self stringNowToDate:date];

}Copy the code

// Get the time difference string at the specified time, formatted as needed before returning in this method

+(NSString*)stringNowToDate:(NSDate*)toDate {// create date NSCalendar object NSCalendar * CAL = [NSCalendar currentCalendar]; NSDate *today = [NSDate date]; / / used to get the specific time, bit operations unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ; If (toDate && today) {// Do not convert to nil NSDateComponents *d = [CAL Components :unitFlags fromDate: toDate:toDate options:0 ]; //NSString *dateStr=[NSString stringWithFormat:@"%d year % D month % D day %d hour %d minute %d second ",[d year],[d month], [d day], [d hour], [d minute], [d second]]; NSString *dateStr=[NSString stringWithFormat:@"%02ld:%02ld:%02ld",[d hour], [d minute], [d second]]; return dateStr; } return @""; }Copy the code

// Get the path to a file in the sandbox Library/Caches/ directory

+ (NSString *)getFullPathWithFile:(NSString *)urlName {// get Library/Caches/ path NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *myCacheDirectory = [docPath stringByAppendingPathComponent:@"MyCaches"]; // Check MyCaches if (! [[NSFileManager defaultManager] fileExistsAtPath: myCacheDirectory]) {/ / does not exist Then create [[NSFileManager defaultManager] createDirectoryAtPath:myCacheDirectory withIntermediateDirectories:YES attributes:nil error:nil]; } // Use MD5 encryption to convert a string to a hexadecimal string (MD5 encryption converts a string to a unique hexadecimal string) NSString * newName = MD5Hash(urlName); / / stitching path return [myCacheDirectory stringByAppendingPathComponent: newName]; }Copy the code

// Check whether the cache file times out

+ (BOOL)isTimeOutWithFile:(NSString *)filePath timeOut:(double)timeOut {NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; / / get the last time the file modification time NSDate * lastModfyDate = fileDict fileModificationDate; / / calculate the time difference for current system time and the time interval lastModfyDate NSTimeInterval sub = [[NSDate date] timeIntervalSinceDate: lastModfyDate]; if (sub < 0) { sub = -sub; If (sub > timeOut) {return YES; } return NO; }Copy the code

// Clear the cache

+ (void) resetCache {
    [[NSFileManager defaultManager] removeItemAtPath:[MZLCache cacheDirectory] error:nil];
}Copy the code

// Cache directory

+ (NSString*) cacheDirectory {
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cacheDirectory = [paths objectAtIndex:0];
    cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"MZLCaches"];
    return cacheDirectory;
}Copy the code

// Delete the specified cache

+ (NSData*) objectForKey:(NSString*)key {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filename = [self.cacheDirectory stringByAppendingPathComponent:key];

    if ([fileManager fileExistsAtPath:filename])
    {
        NSDate *modificationDate = [[fileManager attributesOfItemAtPath:filename error:nil] objectForKey:NSFileModificationDate];
        if ([modificationDate timeIntervalSinceNow] > cacheTime) {
            [fileManager removeItemAtPath:filename error:nil];
        } else {
            NSData *data = [NSData dataWithContentsOfFile:filename];
            return data;
        }
    }
    return nil;
}Copy the code

// Create the specified cache

+ (void) setObject:(NSData*)data forKey:(NSString*)key { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filename = [self.cacheDirectory stringByAppendingPathComponent:key]; BOOL isDir = YES; if (! [fileManager fileExistsAtPath:self.cacheDirectory isDirectory:&isDir]) { [fileManager createDirectoryAtPath:self.cacheDirectory withIntermediateDirectories:NO attributes:nil error:nil]; } NSError *error; @try { [data writeToFile:filename options:NSDataWritingAtomic error:&error]; } @catch (NSException * e) { //TODO: error handling maybe } }Copy the code

// Get the file size in a path

+ (long long) fileSizeAtPath:(NSString*) filePath{

    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){

    return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
}
return 0;

}Copy the code

// Get the cache size

+ (float ) folderSizeAtPath:(NSString*) folderPath{ NSFileManager* manager = [NSFileManager defaultManager]; if (! [manager fileExistsAtPath:folderPath]) return 0; NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator]; NSString* fileName; long long folderSize = 0; while ((fileName = [childFilesEnumerator nextObject]) ! = nil){ NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName]; folderSize += [MZLCache fileSizeAtPath:fileAbsolutePath]; } return folderSize/(1024.0 * 1024.0); }Copy the code

//MD5 encrypted character string

NSString * MD5Hash(NSString *aString) {
    const char *cStr = [aString UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
    return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]];
}Copy the code