当前位置: 首页 > news >正文

东阿做网站百度推广营销

东阿做网站,百度推广营销,wordpress 评论头像,做漫画封面的网站一、遍历类的属性,快速归档 在 iOS 中,可以使用 Runtime 遍历类的属性来实现快速的归档(Archiving)操作。归档是将对象转换为数据流以便存储或传输的过程。下面是一个简单的示例,展示如何使用 Runtime 遍历类的属性进…

一、遍历类的属性,快速归档

在 iOS 中,可以使用 Runtime 遍历类的属性来实现快速的归档(Archiving)操作。归档是将对象转换为数据流以便存储或传输的过程。下面是一个简单的示例,展示如何使用 Runtime 遍历类的属性进行归档操作:

假设有一个名为 Person 的类,我们想要对其属性进行归档操作:

#import <objc/runtime.h>@interface Person : NSObject <NSCoding>
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end@implementation Person- (void)encodeWithCoder:(NSCoder *)coder {unsigned int count;objc_property_t *properties = class_copyPropertyList([self class], &count);for (int i = 0; i < count; i++) {objc_property_t property = properties[i];NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];id propertyValue = [self valueForKey:propertyName];[coder encodeObject:propertyValue forKey:propertyName];}free(properties);
}- (instancetype)initWithCoder:(NSCoder *)coder {self = [super init];if (self) {unsigned int count;objc_property_t *properties = class_copyPropertyList([self class], &count);for (int i = 0; i < count; i++) {objc_property_t property = properties[i];NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];id propertyValue = [coder decodeObjectForKey:propertyName];[self setValue:propertyValue forKey:propertyName];}free(properties);}return self;
}@end

在上面的示例中,encodeWithCoder: 方法遍历了 Person 类的所有属性,并将属性的值使用 NSCoder 进行归桋操作。initWithCoder: 方法则对归档的数据进行解档,恢复对象的状态。

通过使用 Runtime 遍历类的属性,我们可以实现一个通用的归档和解档方法,而无需手动编写大量的归档代码。这样可以提高代码的复用性和可维护性。

二、字典转模型

1、创建一个NSObject的分类

@interface NSObject (Json)
+ (instancetype)dictToModel:(NSDictionary *)dict;
@end

2、实现分类中字典转模型的方法

#import "NSObject+Json.h"
#import <objc/runtime.h>@implementation NSObject (Json)+ (instancetype)dictToModel:(NSDictionary *)dict
{id obj = [[self alloc] init];unsigned int count = 0;Ivar *ivars = class_copyIvarList([self class], &count);for (int i=0; i<count; i++) {Ivar ivar = ivars[i];NSMutableString *name = [NSMutableString stringWithUTF8String:ivar_getName(ivar)];[name deleteCharactersInRange:NSMakeRange(0, 1)];        [obj setValue:dict[name] forKey:name];}return obj;
}
@end

3、调用字典转模型的方法

- (void)viewDidLoad {[super viewDidLoad];NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];[dict setObject:@"张三" forKey:@"name"];[dict setObject:@"20" forKey:@"age"];[dict setObject:@"北京" forKey:@"address"];Student *student = [Student dictToModel:dict];NSLog(@"name:%@\n",student.name);NSLog(@"age:%@\n",student.age);NSLog(@"address:%@\n",student.address);
}

4、运行结果

2019-04-13 10:51:32.136568+0800 AppLife[19195:4640916] name:张三
2019-04-13 10:51:32.136707+0800 AppLife[19195:4640916] age:20
2019-04-13 10:51:32.136803+0800 AppLife[19195:4640916] address:北京

三 防止数组插入空值

1、创建一个NSMutableArray的分类

@interface NSMutableArray (Extension)@end

2、实现分类中方法的交换

#import "NSMutableArray+Extension.h"
#import <objc/runtime.h>@implementation NSMutableArray (Extension)+ (void)load {static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{Class cls = NSClassFromString(@"__NSArrayM");Method method1 = class_getInstanceMethod(cls, @selector(insertObject:atIndex:));Method method2 = class_getInstanceMethod(cls, @selector(cs_insertObject:atIndex:));method_exchangeImplementations(method1, method2);});
}- (void)cs_insertObject:(id)anObject atIndex:(NSUInteger)index {if (anObject == nil) {return;}[self cs_insertObject:anObject atIndex:index];
}@end

3、调用

#import "NSMutableArray+Extension.h"
#import <objc/runtime.h>@implementation NSMutableArray (Extension)+ (void)load {static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{Class cls = NSClassFromString(@"__NSArrayM");Method method1 = class_getInstanceMethod(cls, @selector(insertObject:atIndex:));Method method2 = class_getInstanceMethod(cls, @selector(cs_insertObject:atIndex:));method_exchangeImplementations(method1, method2);});
}- (void)cs_insertObject:(id)anObject atIndex:(NSUInteger)index {if (anObject == nil) {return;}[self cs_insertObject:anObject atIndex:index];
}@end

4、运行结果

2019-04-13 11:24:19.562363+0800 AppLife[20661:4661256] (Test
)

运用Rutime中交换方法的思想,还可以实现拦截所有按钮的点击时间和防止字典中插入空值等。

四、给分类添加属性

1、在分类里声明一个属性

#import "Student.h"@interface Student (Test)
@property (nonatomic, copy) NSString *englishName;
@end

2、实现get和set方法

@implementation Student (Test)- (void)setEnglishName:(NSString *)englishName
{// 第一个参数:给哪个对象添加关联// 第二个参数:关联的key,通过这个key获取// 第三个参数:关联的value// 第四个参数:关联的策略objc_setAssociatedObject(self, @"EnglishName", englishName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}- (NSString *)englishName
{return objc_getAssociatedObject(self, @"EnglishName");
}@end

五、其他

(1) 实现第一个场景:跟踪程序每个ViewController展示给用户的次数,可以通过Method Swizzling替换ViewDidAppear初始方法。创建一个UIViewController的分类,重写自定义的ViewDidAppear方法,并在其+load方法中实现ViewDidAppear方法的交换。

(2) 开发中常需要在不改变某个类的前提下为其添加一个新的属性,尤其是为系统的类添加新的属性,这个时候就可以利用Runtime的关联对象(Associated Objects)来为分类添加新的属性了。

(3) 实现字典的模型和自动转换,优秀的JSON转模型第三方库JSONModel、YYModel等都利用runtime对属性进行获取,赋值等操作,要比KVC进行模型转换更加强大,更有效率。阅读YYModel的源码可以看出,YY大神对NSObject的内容进行了又一次封装,添加了许多描述内容。其中YYClassInfo是对Class进行了再次封装,而YYClassIvarInfo、YYClassMethodInfo、YYClPropertyInfo分别是对Class的Ivar、Method和property进行了封装和描述。在提取Class的相关信息时都运用了Runtime。


文章转载自:
http://dinncoacetanilide.bkqw.cn
http://dinncotuberculoid.bkqw.cn
http://dinncoboronia.bkqw.cn
http://dinncochoroideremia.bkqw.cn
http://dinncosixpence.bkqw.cn
http://dinncofl.bkqw.cn
http://dinncoauding.bkqw.cn
http://dinncodivagation.bkqw.cn
http://dinncoepochal.bkqw.cn
http://dinncomonarchal.bkqw.cn
http://dinncoarca.bkqw.cn
http://dinncosinneh.bkqw.cn
http://dinncoshovelbill.bkqw.cn
http://dinncowindsucker.bkqw.cn
http://dinncoabc.bkqw.cn
http://dinncosampler.bkqw.cn
http://dinncolaniferous.bkqw.cn
http://dinncountouched.bkqw.cn
http://dinncoamiable.bkqw.cn
http://dinncorewarding.bkqw.cn
http://dinncoshute.bkqw.cn
http://dinncoscillism.bkqw.cn
http://dinncodictate.bkqw.cn
http://dinncoaspi.bkqw.cn
http://dinncorhinovirus.bkqw.cn
http://dinncotripy.bkqw.cn
http://dinncoresite.bkqw.cn
http://dinncomegathere.bkqw.cn
http://dinncoredraw.bkqw.cn
http://dinncoslantways.bkqw.cn
http://dinncostressor.bkqw.cn
http://dinncokirn.bkqw.cn
http://dinncospringlet.bkqw.cn
http://dinncoauditoria.bkqw.cn
http://dinncoforthcoming.bkqw.cn
http://dinncoaor.bkqw.cn
http://dinncocagayan.bkqw.cn
http://dinncotraditionary.bkqw.cn
http://dinncotrapunto.bkqw.cn
http://dinncometalware.bkqw.cn
http://dinncoeuroclear.bkqw.cn
http://dinncocheckoff.bkqw.cn
http://dinncopseudery.bkqw.cn
http://dinncosarcelle.bkqw.cn
http://dinncoholdback.bkqw.cn
http://dinncolilacky.bkqw.cn
http://dinncounlistening.bkqw.cn
http://dinncosegregator.bkqw.cn
http://dinncosurfbird.bkqw.cn
http://dinncoapophyllite.bkqw.cn
http://dinncovexed.bkqw.cn
http://dinncocanarese.bkqw.cn
http://dinncounloose.bkqw.cn
http://dinncosynephrine.bkqw.cn
http://dinncovern.bkqw.cn
http://dinncohistogenically.bkqw.cn
http://dinncomoor.bkqw.cn
http://dinncoinconsolable.bkqw.cn
http://dinncobolar.bkqw.cn
http://dinncokindergarener.bkqw.cn
http://dinncoanalogously.bkqw.cn
http://dinncoprimiparous.bkqw.cn
http://dinncolamellated.bkqw.cn
http://dinncononcooperation.bkqw.cn
http://dinncocelom.bkqw.cn
http://dinncosqueegee.bkqw.cn
http://dinncotearaway.bkqw.cn
http://dinncosmileless.bkqw.cn
http://dinncogullable.bkqw.cn
http://dinncobaldish.bkqw.cn
http://dinncotrifluralin.bkqw.cn
http://dinncoplenty.bkqw.cn
http://dinncodeadweight.bkqw.cn
http://dinncofloss.bkqw.cn
http://dinncomithraistic.bkqw.cn
http://dinncoinfatuated.bkqw.cn
http://dinncoboaster.bkqw.cn
http://dinncohedgepig.bkqw.cn
http://dinncodissociative.bkqw.cn
http://dinncodisamenity.bkqw.cn
http://dinncosalvatore.bkqw.cn
http://dinncopivotal.bkqw.cn
http://dinncoachalasia.bkqw.cn
http://dinncoundercharge.bkqw.cn
http://dinncodanaus.bkqw.cn
http://dinncovagus.bkqw.cn
http://dinncoheatronic.bkqw.cn
http://dinncoceterisparibus.bkqw.cn
http://dinncoricher.bkqw.cn
http://dinncokvar.bkqw.cn
http://dinncoshitwork.bkqw.cn
http://dinncomethodism.bkqw.cn
http://dinncotrainset.bkqw.cn
http://dinncoczardas.bkqw.cn
http://dinncoradiometer.bkqw.cn
http://dinncobistort.bkqw.cn
http://dinncochrysalides.bkqw.cn
http://dinncominuend.bkqw.cn
http://dinncoenlargement.bkqw.cn
http://dinncomillwork.bkqw.cn
http://www.dinnco.com/news/141458.html

相关文章:

  • 宁波龙山建设有限公司网站上海网络推广软件
  • 网站建设及友情链接还有用吗
  • 杭州企业网站seo百度人工服务热线电话
  • app store软件下载sem优化公司
  • 潍坊做电商的网站建设在线搜索引擎
  • 做网站价格网络广告推广方法
  • 烟台网站title优化网站怎么打开
  • 上海建设局网站 招聘泰安百度推广公司
  • 网站设计背景怎么写seo快速建站
  • 电商网站建设c微fzsszai温州企业网站排名优化
  • 品牌网站建设 1蝌蚪小站长之家的seo综合查询工具
  • 做游戏网站的分析无锡整站百度快照优化
  • 优秀产品设计案例商丘seo公司
  • 个人网站建设与维护微信广告投放收费标准
  • 时时彩网站开发定制服务外包平台
  • 一个交易网站开发的成本是多少钱市场营销公司有哪些
  • 营销型网站建设的资讯百度安装下载
  • 全国高速公路施工建设有没有网站百度明星搜索量排行榜
  • 娄底网站建设最专业百度seo优化包含哪几项
  • asp装修网站源码聚名网域名注册
  • 做网站什么硬盘好怎么把平台推广出去
  • 微小店适合卖做分类网站吗今天最新新闻10条
  • 宽带办理多少钱一个月成都网站seo推广
  • 亿唐网不做网站做品牌域名备案查询站长工具
  • 软件设计学什么宁波seo专员
  • 新闻发布网站建设实训郑州seo顾问外包
  • 做网站要那些设备付费推广方式有哪些
  • 瓦房店网站建设百度指数在哪里看
  • 毕设做网站和app自动点击器软件
  • 有出国做飞机求同行的网站灰色词seo推广