遇到一个问题是,在使用大量图片的app中,需要从存储里面读取数据,每次都从文件系统里面读取文件会造成卡顿现象。
解决办法就是把NSData对象缓存起来,先从NSCache里面读取数据,然后再从文件系统获取数据,提高效率。
-》id可以作为函数参数,instancetype则不能
->在init方法中两者等价,编译器会自动保护将id 转换成 instancetype
如:
- (id)initWithBar:(NSInteger)bar; - (instancetype)initWithBar:(NSInteger)bar;
-》在构造函数中则不一样,instancetype总是能返回正确的结果,id则不同。
以下是不等价的:
+ (id)fooWithBar:(NSInteger)bar; + (instancetype)fooWithBar:(NSInteger)bar;
使用第二种返回构造着时候,你每次都会得到正确的结果。
2.NSAttributedString
-》创建属性字符串
NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSStrokeColorAttributeName,[NSNumber numberWithFloat:5.0f],NSStrokeWidthAttributeName, nil];
NSAttributedString * str = [[NSAttributedString alloc]initWithString:@"hello world!" attributes:attributes];
NSRange rage = NSMakeRange(0, 4);
NSDictionary * dc = [str attributesAtIndex:0 effectiveRange:&rage];
3.NSAutoreleasePool 用于管理被声明为autorelease的实例。
原理:在NSAutoreleasePool中包含了一个可变数组,用来存储被声明为autorelease的对象。当NSAutoreleasePool自身被销毁的时候,它会遍历这个数组,release数组中的每一个成员(注意,这里只是release,并没有直接销毁对象)。若成员的retain count 大于1,那么对象没有被销毁,造成内存泄露。默认的NSAutoreleasePool 只有一个,你可以在你的程序中创建NSAutoreleasePool,被标记为autorelease的对象会跟最近的NSAutoreleasePool匹配。可以嵌套使用NSAutoreleasePool。
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
ClassA *a=[[[ClassA alloc] init] autorelease];
ClassB *b=[[[ClassB alloc] init] autorelease];
ClassC *c=[[[ClassC alloc] init] autorelease];
[pool release];
4.NSCache类,区别于文件系统下得caches文件夹。
遇到一个问题是,在使用大量图片的app中,需要从存储里面读取数据,每次都从文件系统里面读取文件会造成卡顿现象。
解决办法就是把NSData对象缓存起来,先从NSCache里面读取数据,然后再从文件系统获取数据,提高效率。
NSArray * arr = @[@"hello",@"world"];
NSCache * cache = [[NSCache alloc]init];
[cache setObject:arr forKey:@"hello"];
NSArray * tem = [cache objectForKey:@"hello"];
Printing description of tem:
<__NSArrayI 0x8ca4c00>(
hello,
world
)
Printing description of arr:
<__NSArrayI 0x8ca4c00>(
hello,
world
)
1557
250

被折叠的 条评论
为什么被折叠?