Object-C知识点 (六) 开发中的技巧

本文主要介绍开发中的一些实用技巧

#pragma mark - 代码控制Home按键

 [[UIApplication sharedApplication] performSelector:@selector(suspend)]; // 私有API

 #pragma mark - 获取UIWebView的高度

- (void)webViewDidFinishLoad:(UIWebView *)webView
{ CGFloat height
= [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];    CGRect frame = webView.frame;    webView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height); }

 #pragma mark - 是否自动锁屏 

[UIApplication sharedApplication].idleTimerDisabled=YES; //不自动锁屏

[UIApplication sharedApplication].idleTimerDisabled=NO;//自动锁屏(默认)

#pragma mark - NSUserDefaults处理布尔的默认值

if([[NSUserDefaults standardUserDefaults] objectForKey:ID] == nil){
    NSLog(@"没有设置");
}

#pragma mark - 归档、解档的统一实现

Person.h 遵守 <NSSecureCoding>

Person.m 中, 增加如下代码

//导入 runtime头文件
#import <objc/runtime.h>

// 实现下面三个方法
+ (BOOL)supportsSecureCoding {
    return YES;
}

//如何归档 如何存值
- (void)encodeWithCoder:(NSCoder *)aCoder {
    unsigned int outCount = 0;
    Ivar * ivars = class_copyIvarList([self class], &outCount);

    for (int i = 0; i < outCount; i++) {
        Ivar ivar = ivars[i];
        const char * name = ivar_getName(ivar);
        NSString * key = [NSString stringWithUTF8String:name];
        [aCoder encodeObject:[self valueForKey:key] forKey:key];
    }
    free(ivars);
}

//如何解档 如何取值
- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder {
    if (self) {
        unsigned int outCount = 0;
        Ivar * ivars = class_copyIvarList([self class], &outCount);

        for (int i = 0; i < outCount; i++) {
            Ivar ivar = ivars[i];
            const char * name = ivar_getName(ivar);
            NSString * key = [NSString stringWithUTF8String:name];
            [self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
        }
        free(ivars);
    }
    return self;
}

 PS: 更简单的方法是给 NSObject 增加一个分类, 实现上面的三个方法, 这样所有继承自 NSObject 的对象就都有了解档归档的功能
 iOS 开发常用分类 GitHub 直达链接:  https://github.com/lieryang/Category

 #pragma mark - 自定义对象的copy 和 mutableCopy功能统一实现

 Person.h 中 遵守 <NSCopying, NSMutableCopying>

 Perosn.m 中 增加如下代码

// 导入 runtime 头文件
#import <objc/runtime.h>

// copy最后会调用这个方法
- (id)copyWithZone:(NSZone *)zone {
    id mode = [[[self class] allocWithZone:zone] init];

    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([self class], &count);
    //ivars[100]不会崩溃但是数据不对,类似数组的东西
    for (int i = 0; i<count; i++) {
        // 取出i位置对应的成员变量
        Ivar ivar = ivars[i];

        // 查看成员变量
        const char *name = ivar_getName(ivar);
        // 设置到成员变量身上
        NSString *key = [NSString stringWithUTF8String:name];

        id value = [self valueForKey:key];
        [mode setValue:value forKey:key];
    }

    free(ivars);

    return mode;
}

//mutableCopy 最后会调用这个方法(自定义的对象的 copy 方法复制出来的对象就是深复制,写这个方法是为了安全起见)
- (id)mutableCopyWithZone:(NSZone *)zone {
    return [self copyWithZone:zone];
}

PS: 更简单的方法是给 NSObject 增加一个分类, 实现上面的两个方法, 这样所有继承自 NSObject 的对象就都有了copy 和 mutableCopy的功能

iOS 开发常用分类 GitHub 直达链接:  https://github.com/lieryang/Category 

更多内容--> 博客导航 每周一篇哟!!!

有任何关于iOS开发的问题!欢迎下方留言!!!或者邮件lieryangios@126.com 虽然我不一定能够解答出来,但是我会请教iOS开发高手!!!解答您的问题!!!

原文地址:https://www.cnblogs.com/CoderEYLee/p/Object-C-0033.html