标签:
编号 | 主题 | 内容 |
---|---|---|
一 | NSFileManager | NSFileManager介绍/用法(常见的判断)/文件访问/文件操作 |
二 | 集合对象的内存管理 | 集合对象的内存管理/内存管理总结 |
三 | *copy | copy基本概念/使用/copy快速入门/内存管理 |
四 | @Property中的copy关键字 | @property中的copy的作用/@property内存管理策略选择 |
五 | 自定义类实现copy操作 | 自定义类实现copy操作 |
六 | 单例设计模式 | 单例模式概念/简单的单例模式实现 |
七 | 宏定义抽取单例 |
什么是NSFileManager
NSFileManager使用了单例模式
单例
对象[NSFileManager defaultManager]
NSFileManager *manager = [NSFileManager defaultManager]; // 可以判断文件 BOOL flag = [manager fileExistsAtPath:@"/Users/MJ-Hee/Desktop/lnj.txt"]; NSLog(@"flag = %i", flag); // 可以判断文件夹 flag = [manager fileExistsAtPath:@"/Users/MJ-Hee/Desktop/未命名文件夹"]; NSLog(@"flag = %i", flag);
NSFileManager *manager = [NSFileManager defaultManager]; BOOL directory = NO; BOOL flag = [manager fileExistsAtPath:@"/Users/MJ-Hee/Desktop/未命名文件夹" isDirectory:&directory]; NSLog(@"flag = %i, directory = %i", flag, directory);
- (BOOL)isReadableFileAtPath:(NSString *)path;
- (BOOL)isWritableFileAtPath:(NSString *)path;
系统目录不允许写入
- (BOOL)isDeletableFileAtPath:(NSString *)path;
系统目录不允许删除
NSFileManager *manager = [NSFileManager defaultManager]; NSDictionary *dict = [manager attributesOfItemAtPath:@"/Users/MJ-Hee/Desktop/hmj.txt" error:nil]; NSLog(@"dit = %@", dict);
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;
- (NSData )contentsAtPath:(NSString )path;
NSFileManager *manager = [NSFileManager defaultManager]; NSArray *paths = [manager contentsOfDirectoryAtPath:@"/Users/MJ-Hee/Desktop" error:nil]; NSLog(@"paths = %@", paths);
- (NSArray )subpathsAtPath:(NSString )path;
- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;
NSFileManager *manager = [NSFileManager defaultManager]; NSArray *paths = [manager subpathsAtPath:@"/Users/LNJ/Desktop/"]; NSLog(@"paths = %@", paths);
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error;
- (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error;
NSFileManager *manager = [NSFileManager defaultManager]; BOOL flag = [manager createDirectoryAtPath:@"/Users/LNJ/Desktop/test" withIntermediateDirectories:YES attributes:nil error:nil]; NSLog(@"flag = %i", flag);
NSString *str = @"lnj"; //NSData : 二进制数据 //将字符串转化成二进制数据 NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; NSFileManager *manager = [NSFileManager defaultManager]; BOOL flag = [manager createFileAtPath:@"/Users/LNJ/Desktop/abc.txt" contents:data attributes:nil]; NSLog(@"flag = %i", flag);
NSMutableArray *arr = [[NSMutableArray alloc] init]; Person *p = [[Person alloc] init]; NSLog(@"retainCount = %lu", [p retainCount]); //如果将一个对象添加到一个数组/字典中,那么数组会对对象进行一次retain [arr addObject:p]; NSLog(@"retainCount = %lu", [p retainCount]); [p release]; NSLog(@"retainCount = %lu", [p retainCount]); //当数组对象释放后,会给数组中所有的对象发送一条release消息 [arr release];
NSMutableArray *arr = [[NSMutableArray alloc] init]; Person *p = [[Person alloc] init]; NSLog(@"retainCount = %lu", [p retainCount]); [arr addObject:p]; NSLog(@"retainCount = %lu", [p retainCount]); //当数组移除一个对象之后,会给这个对象发送一条release消息 [arr removeObject:p]; NSLog(@"retainCount = %lu", [p retainCount]); [p release]; [arr release];
1.官方内存管理原则
2.集合的内存管理细节
3.普遍规律
什么是copy
常见的复制有:文件复制
特点:
OC中的copy
如何使用copy功能
使用copy功能的前提
@protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
@protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
NSString *srcStr = @"lnj"; NSString *copyStr = [srcStr copy]; NSLog(@"src = %p, copy = %p", srcStr, copyStr);
NSString *srcStr = @"lnj"; NSMutableString *copyStr = [srcStr mutableCopy]; NSLog(@"src = %p, copy = %p", srcStr, copyStr); NSLog(@"src = %@, copy = %@", srcStr, copyStr); [copyStr appendString:@" cool"]; NSLog(@"src = %@, copy = %@", srcStr, copyStr); NSMutableString *srcStr = [NSMutableString stringWithFormat:@"lnj"]; NSString *copyStr = [srcStr copy]; [srcStr appendString:@" cool"]; NSLog(@"src = %p, copy = %p", srcStr, copyStr); NSLog(@"src = %@, copy = %@", srcStr, copyStr); NSMutableString *srcStr = [NSMutableString stringWithFormat:@"lnj"]; NSMutableString *copyStr = [srcStr mutableCopy]; [srcStr appendString:@" cool"]; [copyStr appendString:@" 520it"]; NSLog(@"src = %p, copy = %p", srcStr, copyStr); NSLog(@"src = %@, copy = %@", srcStr, copyStr);
总结 +如果是浅拷贝,那么系统会自动对源对象进行一次retain
浅拷贝的原则
@interface Person : NSObject //用retain,外界可以修改内部的数据,此处改为copy可以防止外界修改内部的数据 @property (nonatomic, retain) NSString *name; @end NSMutableString *str = [NSMutableString stringWithFormat:@"lnj"]; Person *p = [[Person alloc] init]; p.name = str; // person中的属性会被修改 [str appendString:@" cool"]; NSLog(@"name = %@", p.name);
Person *p = [[Person alloc] init]; p.name = @"lnj"; Dog *d = [[Dog alloc] init]; d.age = 10; NSLog(@"retainCount = %lu", [d retainCount]); // 1 p.pBlock = ^{ // 报错, 调用之前就销毁了 NSLog(@"age = %d", d.age); }; [d release]; // 0 p.pBlock(); [p release];
Person *p = [[Person alloc] init]; p.name = @"lnj"; Dog *d = [[Dog alloc] init]; d.age = 10; NSLog(@"retainCount = %lu", [d retainCount]); // 1 p.pBlock = ^{ // 会对使用到的外界对象进行一次retain NSLog(@"age = %d", d.age); NSLog(@"retainCount = %lu", [d retainCount]); // 1 }; [d release]; // 1 p.pBlock(); [p release];
非ARC
ARC
zone: 表示空间,分配对象是需要内存空间的,如果指定了zone,就可以指定 新建对象对应的内存空间。但是:zone是一个非常古老的技术,为了避免在堆中出现内存碎片而使用的。在今天的开发中,zone几乎可以忽略
在-(id)copyWithZone(NSZone *)zone方法中创建一个副本对象,然后将当前对象的值赋值给副本对象即可
无父类实现
-(id)copyWithZone(NSZone *)zone{ //class用于获取一个类 CustomMode *custom = [[[self class] copyWithZone:zone] init]; Custom ->_a = [_a copyWithZone:zone]; Custom -> _c = _c;//不是对象的 直接赋值 Return custom; }
-(id)copyWithZone(NSZone *)zone{ CustomModel *custom = [super copyWithZone:zone]; //只调用子类特有的成员变量的setter方法即可 …. Return custom; }
什么是单例模式:(Singleton)
什么情况下使用单例?
单例设计模式的要点:
类
方法static 类名 *_instance = nil; + (instancetype)allowWithZone:(struct _NSZone *)zone { //由于所有的创建方法都会调用该方法,所以只需要在该方法中控制当前对象只创建一次即可 if (_instance == nil) { NSLog(@"创建了一个对象); _instance = [[super allowWithZone:zone] init; } return _instance; //以下代码在多线程中也能保证只执行一次 static dispatch_once_ onceToken; dispatch_once(&onceToken, ^{ _instance = [[super allowWithZone:zone] init]; } ;) return _instance; } //copyWithZone方法用什么调用?对象 - (id)copyWithZone:(NSZone *)zone { return _instance; } - (id)mutableCopyWithZone:(NSZone *)zone { return _instance; } - (oneway void) release { //为保证整个程序过程中只有一份实例,在这个方法中什么都不做 } - (instancetype)retain { return _instance; } - (NSUInteger)retainCount { //注意:为了方便程序员之间沟通,一般情况下不会在单例中返回retainCount = 1,而是返回一个比较大的值 return MAXFLOAT; }
// 如何判断当前是ARC还是MRC? // 可以在编译的时候判断当前是否是ARC #if __has_feature(objc_arc) NSLog(@"ARC"); #else NSLog(@"MRC"); #endif
/*********Singleton.h**********/
// 以后就可以使用interfaceSingleton来替代后面的方法声明
#define interfaceSingleton(name) +(instancetype)share##name
#if __has_feature(objc_arc) // ARC #define implementationSingleton(name) + (instancetype)share##name { name *instance = [[self alloc] init]; return instance; } static name *_instance = nil; + (instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[super allocWithZone:zone] init]; }); return _instance; } - (id)copyWithZone:(NSZone *)zone{ return _instance; } - (id)mutableCopyWithZone:(NSZone *)zone { return _instance; } #else // MRC #define implementationSingleton(name) + (instancetype)share##name { name *instance = [[self alloc] init]; return instance; } static name *_instance = nil; + (instancetype)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[super allocWithZone:zone] init]; }); return _instance; } - (id)copyWithZone:(NSZone *)zone{ return _instance; } - (id)mutableCopyWithZone:(NSZone *)zone { return _instance; } - (oneway void)release { } - (instancetype)retain { return _instance; } - (NSUInteger)retainCount { return MAXFLOAT; } #endif /********自定义类.h中********/ #import "Singleton.h" interfaceSingleton(类名); /********自定义类.m中********/ implementationSingleton(类名)
标签:
原文地址:http://www.cnblogs.com/HMJ-29/p/4713325.html