![](https://img.haomeiwen.com/i619522/06b86c8b6ae99949.jpg)
1. 读取文件的大小
-
正确,省内存(建议)
long long length = 0; NSFileManager *fileMananger = [NSFileManager defaultManager]; if ([fileMananger fileExistsAtPath:filePath]) { NSDictionary *dic = [fileMananger attributesOfItemAtPath:filePath error:nil]; length = [dic[@"NSFileSize"] longLongValue]; }
-
会大量占内存,读取,不可取(不建议)
NSData *data = [NSData dataWithContentsOfFile:filePath]; length = data.length;
2. 读取手机的剩余内存空间
#include <sys/param.h>
#include <sys/mount.h>
+ (NSString *)freeDiskSpaceInBytes{
struct statfs buf;
unsigned long long freeSpace = -1;
if (statfs("/var", &buf) >= 0) {
freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
}
NSString *str = [NSString stringWithFormat:@"手机剩 余存储空间为:%0.2lld MB",freeSpace/1024/1024];
return str;
}
3. 构造方法(+alloc) 与 类方法的区别(MRC代码
)
id array = [NSMutableArray arrayWithCapacity:1];
equals
id array = [[[NSMutableArray alloc] initWithCapacity:1] autorelease];
- 内容牵涉到:MRC
objective-c的内存管理;alloc方法生成的对象自己生成并且自己持有
; 而上边的array...
类方法取得对象存在,但自己不持有
. - ARC
alloc方法生成的对象自己生成并且自己持有
; 而上边的array...
类方法取得对象存在,但自己持有
.
4. __strong & __weak & __unsafe_unretained & __autoreleasing 的相互牵扯
读<OC高级编程>-内存管理章节总结
-
__strong
{ id __strong obj = [NSObject alloc] init]; }
- __strong修饰符表示对对象的
强引用
, 持有强引用的变量在超出其作用域时被废弃,随着强引用的失效,引用的对象也会随之释放. - __strong修饰符是
id类型
和对象类型
默认的所有权修饰符.
- __strong修饰符表示对对象的
-
__weak
{ id __weak obj = [NSObject alloc] init]; }
该代码片段会引起编译器的警告:
error_weak_message.png
解决方案:
{ id __strong obj = [NSObject alloc] init]; id __weak obj1 = obj; }
- __weak修饰符表示对对象的
弱引用
,它的出现是为了解决__strong
引起的循环引用
而引出的.
2.__weak修饰符表示的对象,在释放之后,会自定赋值nil.
- __weak修饰符表示对对象的
-
__unsafe_unretained
{ id __unsafe_unretained obj = [NSObject alloc] init]; }
该代码片段会引起编译器的警告:
error_unsafe_message.png
- __unsafe_unretained修饰符表示对对象的
不安全的无主引用
, 它的功能和__weak类似,但是它的出现是为了适配iOS5之前的版本和OS X Leopard的应用程序; - __unsafe_unretained修饰符与__weak的主要区别是:__weak在自动释放对象后,会被自动的赋值为nil;而__unsafe_unretained在释放之后,仍旧访问的是该内存地址下对应的区域,因此可能访问的可能是其他的内容,从而造成访问出错而不得知.
- __unsafe_unretained修饰符在编译器的内存管理对象之外.只能作为C语言的指针类型来使用.
- __unsafe_unretained修饰符表示对对象的
-
__autoreleasing
/* ARC无效 */ { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; id obj = [NSObject alloc] init]; [obj autorelease]; [pool drain]; } /* ARC有效 */ @autoreleasepool{ id __autoreleasing obj = [NSObject alloc] init]; } @autoreleasepool{ id __strong obj = [NSMutableArray array]; }
- 使用alloc/new/copy/mutableCopy生成的对象不会自动注册到 autoreleasepool ;而除此之外的对象会被自动注册到autoreleasepool 中.
- 在访问__weak修饰符的变量时,必定要访问注册到autoreleasepool 的对象;
- id的指针或对象在没有显示指定的时候会被自动加上__autoreleasing修饰符;如同__strong一样;如:
-[NSString stringWithContentsOfFile:encoding:error]
5. 属性声明的属性和所有权修饰符的对应关系
![](https://img.haomeiwen.com/i619522/2bbd4bb1fa31695d.png)
6. 设置导航条的相关属性
- 主要是区分
navigationBar
和navigationItem
;-
navigationBar
用于设置导航条的外观:如导航条的背景颜色,背景图片,标题颜色等相关内容;tintColor
属性影响所有按钮标题和按钮图像的背景色; -
navigationItem
用于设置导航栏相关的数据模型:如导航栏的标题内容,左侧,右侧的按钮内容,自定义的相关数据等;
-
7. 给键盘添加上部的工具条,用于隐藏键盘
// 充当弹簧条的效果
UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneClk)];
UIToolbar *bar = [[UIToolbar alloc] init];
#warning 1.一定设置尺寸,否则点击事件将不会生效;
[bar sizeToFit];
bar.tintColor = [UIColor blackColor];
[bar setItems:@[item0, item]];
self.inputAccessoryView = bar;
- tip: 通常由于系统的输入框较多,不可能为每个输入框都设置一遍. 因此最好对系统的输入框,进行继承. 这样方便之后的维护,及添加其他与输入框相关的设置内容
8. UITextField的text超过宽度,中间显示...
- 关于文本内容超过指定控件宽度且只显示一行时,默认会在末尾显示
...
, 只是lable的特性(lineBreakMode
). - 但是UITextField并没有此属性,因此可以自定义UITextField , 内部添加一个lable进行相应的控制;
.h文件
#import <UIKit/UIKit.h>
typedef void(^TextFieldDoneClkBlock)(void);
@interface NATextField : UITextField
@property (nonatomic, strong) UILabel *contentLbl;
@property (nonatomic, copy) TextFieldDoneClkBlock doneBlock;
@end
.m文件
#import "NATextField.h"
@implementation NATextField
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
UILabel *lineView = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height - 1, frame.size.width, 1)];
lineView.backgroundColor = NARGB(227, 227, 227);
_contentLbl = [[UILabel alloc] init];
_contentLbl.frame = CGRectMake(0, 0, frame.size.width, frame.size.height - 1);
_contentLbl.lineBreakMode = NSLineBreakByTruncatingMiddle;
_contentLbl.hidden = YES;
[self addSubview:_contentLbl];
[self addSubview:lineView];
}
return self;
}
- (void)doneClk
{
[self resignFirstResponder];
if (self.doneBlock) {
self.doneBlock();
}
}
- (void)setLeftView:(UIView *)leftView
{
[super setLeftView:leftView];
_contentLbl.x = leftView.right;
_contentLbl.w = self.w - leftView.right;
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
_contentLbl.font = font;
}
- (void)setText:(NSString *)text
{
[super setText:text];
_contentLbl.text = text;
}
- (void)layoutSubviews {
[super layoutSubviews];
[self bringSubviewToFront:_contentLbl];
}
@end
use
_addressTF2.textColor = [UIColor clearColor];
_addressTF2.contentLbl.hidden = NO;
9. Block使用copy的情况
![](https://img.haomeiwen.com/i619522/89a0d5b24b4bdb9a.png)
10. NSTimer不执行的改进
dispatch_async(dispatch_get_global_queue(0, 0), ^{
if (!_timer) {
_timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(getNum) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];
}
});
网友评论