美文网首页
iOS 笔记

iOS 笔记

作者: 追着公车的少年_4934 | 来源:发表于2018-07-11 16:26 被阅读0次

    1. 精简代码 优点:代码在代码块中只在代码块中有效,减少对其他作用域命名污染。 缺点:可读性差。

    NSURL *url = ({

    [NSURL URLWithString:@"http://www.baidu.com"];

    });

    UILabel *testLabel = ({

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

    label.text = @"test";

    label.textAlignment = NSTextAlignmentCenter;

    testLabel = label;

    });

    2. NSDictionary 另一种写法 (根据对象名字为key,值为value)

    NSString *name = @"test";

    NSNumber *userid = @(000111);

    NSString *pwd = @"12345678";

    NSDictionary *parm = NSDictionaryOfVariableBindings(name, userid, pwd);

    {

    name = test;

    pwd = 12345678;

    userid = 73;

    }

    3. 把tableview里cell的小对勾的颜色改成别的颜色

    _mTableView.tintColor = [UIColor redColor];

    4.navigationbar弄成透明的而不是带模糊的效果

    [self.navigationBar setBackgroundImage:[UIImage new]

    forBarMetrics:UIBarMetricsDefault];

    self.navigationBar.shadowImage = [UIImage new];

    self.navigationBar.translucent = YES;

    5.改变uitextfield placeholder的颜色和位置

    继承uitextfield,重写这个方法

    - (void) drawPlaceholderInRect:(CGRect)rect {

    [[UIColor blueColor] setFill];

    [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];

    }

    6.在继承中,凡是要求子类重写父类的方法必须先调用父类的方法的,可以在父类方法名后面加上 NS_REQUIRES_SUPER. 子类重写这个


    方法会自动警告提示要调用父类方法。

    - (void)test NS_REQUIRES_SUPER;

    7.判断VC是否已经加载过

    if (self.isViewLoaded) {

    <#statements#>

    }

    8. dictionary 追加 dictionary 

    [mutableDict addEntriesFromDictionary:appendDict];

    9. 获取当前屏幕显示的viewcontroller

    - (UIViewController *)currentVC {

    UIViewController *result = nil;

    UIWindow * window = [[UIApplication sharedApplication] keyWindow];

    if (window.windowLevel != UIWindowLevelNormal) {

    NSArray *windows = [[UIApplication sharedApplication] windows];

    for(UIWindow * tmpWin in windows) {

    if (tmpWin.windowLevel == UIWindowLevelNormal) {

    window = tmpWin;

    break;

    }

    }

    }

    UIView *frontView = [[window subviews] objectAtIndex:0];

    id nextResponder = [frontView nextResponder];

    if ([nextResponder isKindOfClass:[UIViewController class]]) {

    result = nextResponder;

    }else {

    result = window.rootViewController;

    }

    return result;

    }

    10. 捕获APP异常

    NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);

    void UncaughtExceptionHandler(NSException *exception) {

    NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息

    NSString *reason = [exception reason];//非常重要,就是崩溃的原因

    NSString *name = [exception name];//异常类型

    #if DEBUG

    NSString *msg = [NSString stringWithFormat:@"栈信息%@\n崩溃原因%@\n异常类型%@",arr[0], reason, name];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"崩溃日志" message:msg delegate:nil cancelButtonTitle:@"了解" otherButtonTitles:nil, nil];

    [alert show];

    #endif

    DDLogCrash(@"崩溃日志: exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr);

    }

    11. iOS 写文件

    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    NSString *h5LogFile = [documentPath stringByAppendingPathComponent:@"h5Log"];

    NSFileManager *filemanager = [NSFileManager defaultManager];

    // 判断文件大小

    long long size = [[filemanager attributesOfItemAtPath:h5LogFile error:nil] fileSize] / (1024.0 * 1024);

    if (size > 10) {

    [filemanager removeItemAtPath:h5LogFile error:nil];

    }

    if (![filemanager fileExistsAtPath:h5LogFile])

    {

    [filemanager createFileAtPath:h5LogFile contents:nil attributes:nil];

    }

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:h5LogFile];

    [fileHandle seekToEndOfFile];

    NSDateFormatter *formatter;

    if (formatter == nil) {

    formatter = [[NSDateFormatter alloc] init];

    }

    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

    NSString *logStr = [NSString stringWithFormat:@"[%@ html5]:%@%@",[formatter stringFromDate:[NSDate date]],tag,txt];

    logStr = [logStr stringByAppendingString:@"\r\n"];

    NSData *data = [logStr dataUsingEncoding:NSUTF8StringEncoding];

    NSData *aesData = [ZYAES256 AES256ParmEncryptWithKey:@"pamit" Encrypttext:data];

    [fileHandle writeData:aesData];

    [fileHandle closeFile];

    logStr = nil;

    12. NSDictionary 转换为model 系统方法 key需要和model属性对应

    - (void)setValuesForKeysWithDictionary:(NSDictionary*)keyedValues;

    [self.model setValuesForKeysWithDictionary:dict];

    13.使用self.view.layer.borderWidth更好的方式圆角

    CAShapeLayer *bordLayer = [CAShapeLayer layer];

    bordLayer.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));

    bordLayer.lineWidth = 1.f;

    bordLayer.strokeColor = [UIColor lightGrayColor].CGColor;

    bordLayer.fillColor = [UIColor clearColor].CGColor;

    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) cornerRadius:3.0];

    bordLayer.path = bezierPath.CGPath;

    [self.view.layer insertSublayer:bordLayer atIndex:0];

    14. @autoreleasepool  这段代码在每次遍历后释放所有autorelease对象

    NSArray *urls = @[@"http://baidu.com",@"http://jd.com"];

    for (NSURL *url in urls) {

    @autoreleasepool {

    NSError *error;

    NSString *fileContents = [NSString stringWithContentsOfURL:url

    encoding:NSUTF8StringEncoding error:&error];

    // 处理

    }

    }

    相关文章

      网友评论

          本文标题:iOS 笔记

          本文链接:https://www.haomeiwen.com/subject/wplvottx.html