ios 计算某个文件\文件夹的大小
1 2 ```objc 3 @implementation NSString (XMGExtension) 4 //- (unsigned long long)fileSize 5 //{ 6 // // 总大小 7 // unsigned long long size = 0; 8 // 9 // // 文件管理者 10 // NSFileManager *mgr = [NSFileManager defaultManager]; 11 // 12 // // 文件属性 13 // NSDictionary *attrs = [mgr attributesOfItemAtPath:self error:nil]; 14 // 15 // if ([attrs.fileType isEqualToString:NSFileTypeDirectory]) { // 文件夹 16 // // 获得文件夹的大小 == 获得文件夹中所有文件的总大小 17 // NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self]; 18 // for (NSString *subpath in enumerator) { 19 // // 全路径 20 // NSString *fullSubpath = [self stringByAppendingPathComponent:subpath]; 21 // // 累加文件大小 22 // size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize; 23 // } 24 // } else { // 文件 25 // size = attrs.fileSize; 26 // } 27 // 28 // return size; 29 //} 30 31 - (unsigned long long)fileSize 32 { 33 // 总大小 34 unsigned long long size = 0; 35 36 // 文件管理者 37 NSFileManager *mgr = [NSFileManager defaultManager]; 38 39 // 是否为文件夹 40 BOOL isDirectory = NO; 41 42 // 路径是否存在 43 BOOL exists = [mgr fileExistsAtPath:self isDirectory:&isDirectory]; 44 if (!exists) return size; 45 46 if (isDirectory) { // 文件夹 47 // 获得文件夹的大小 == 获得文件夹中所有文件的总大小 48 NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:self]; 49 for (NSString *subpath in enumerator) { 50 // 全路径 51 NSString *fullSubpath = [self stringByAppendingPathComponent:subpath]; 52 // 累加文件大小 53 size += [mgr attributesOfItemAtPath:fullSubpath error:nil].fileSize; 54 } 55 } else { // 文件 56 size = [mgr attributesOfItemAtPath:self error:nil].fileSize; 57 } 58 59 return size; 60 } 61 @end 62 63 MKLog(@"%zd", @"/Users/xiaomage/Desktop".fileSize); 64 ``` 65 66 ## 计算文字的宽度 67 ```objc 68 CGFloat titleW = [字符串 sizeWithFont:字体大小].width; 69 CGFloat titleW = [字符串 sizeWithAttributes:@{NSFontAttributeName : 字体大小}].width; 70 ``` 71 72 ## 有透明度的颜色 73 ```objc 74 [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.2]; 75 [UIColor colorWithWhite:1.0 alpha:0.2]; 76 [[UIColor whiteColor] colorWithAlphaComponent:0.2]; 77 ``` 78 79 ## viewWithTag:内部的大致实现思路 80 ```objc 81 @implementation UIView 82 - (UIView *)viewWithTag:(NSInteger)tag 83 { 84 if (self.tag == tag) return self; 85 86 for (UIView *subview in self.subviews) { 87 return [subview viewWithTag:tag]; 88 } 89 } 90 @end 91 ```
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现