澳门至尊网站-首页

您的位置:澳门至尊网站 > 软件综合 > 并让字段的首字母小写,Runtime脾性之提到对象

并让字段的首字母小写,Runtime脾性之提到对象

2019-10-24 02:44

引言:方今在专门的工作中碰着与一些API对接的post的数据须求将指标的字段首字母小写。
消除办法有三种:
先是种:使用对象的字段属性设置JsonProperty来兑现(不推荐,因为急需手动的改造各种字段的天性)

前言

前几日您准备用二个系统的类依然是你写的类,可是那一个类并不可能满意你的需求,你需求额外增加多个天性。平日消除办法要么是extends,要么使用category。而作者并不推荐使用extends,紧假诺耦合性太强,平常笔者动用category。大家都明白,分类中是心余力绌设置属性的,借使在分拣的注明中写@property 只可以为其生成get 和 set 方法的扬言,然而一时使用项目也亟需追加贰个额外属性,那么如何是好呢?那个时候,runtime的涉嫌属性就能够表明它的效果与利益了。常常都以key value 的存在。

public class UserInfo
{
    [JsonProperty("id")]
    public int Id{ set; get; }
    [JsonProperty("userName")]
    public string UserName{ set; get; }
}

有关的法子

objc_setAssociatedObject 设置关联对象使用objc_getAssociatedObject 获得涉及对象使用objc_removeAssociatedObjects 移除关联对象使用

其次种:使用newtonsoft.json来设置格式化的方法(推荐使用)

用法

相通自身用在category里,合理施用它能让category公布更加大的功用。

  • UIViewcategory

.h文件

#import <UIKit/UIKit.h>@interface UIView typedef void (^GestureActionBlock)(UIGestureRecognizer *ges);/** 单点击手势 */- tapGesture:(GestureActionBlock)block;/** 长按手势 */- longPressGestrue:(GestureActionBlock)block;@end

.m文件

#import "UIView+WT.h"#import <objc/runtime.h>@implementation UIView static char kActionHandlerTapBlockKey;static char kActionHandlerTapGestureKey;static char kActionHandlerLongPressBlockKey;static char kActionHandlerLongPressGestureKey;//单点击手势- tapGesture:(GestureActionBlock)block { self.userInteractionEnabled = YES; UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerTapGestureKey); if  { gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForTapGesture:)]; [self addGestureRecognizer:gesture]; objc_setAssociatedObject(self, &kActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN); } objc_setAssociatedObject(self, &kActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);}- handleActionForTapGesture:(UITapGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateRecognized) { GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerTapBlockKey); if  { block; } }}//长按手势- longPressGestrue:(GestureActionBlock)block { self.userInteractionEnabled = YES; UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerLongPressGestureKey); if  { gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForLongPressGesture:)]; [self addGestureRecognizer:gesture]; objc_setAssociatedObject(self, &kActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN); } objc_setAssociatedObject(self, &kActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);}- handleActionForLongPressGesture:(UITapGestureRecognizer *)gesture { if (gesture.state == UIGestureRecognizerStateBegan) { GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerLongPressBlockKey); if  { block; } }}@end

自家表达下里面包车型客车片段主要字段,比如OBJC_ASSOCIATION_RETAIN以此字段实际上是个枚举来的

typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) { OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */ OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object. * The association is not made atomically. */ OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied. * The association is not made atomically. */ OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object. * The association is made atomically. */ OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied. * The association is made atomically. */};

用法跟@property中的strong 、weak、copy 、assign 、retain等申明属性的修饰符相同,作者下边用到了block就对应OBJC_ASSOCIATION_COPY,而UITapGestureRecognizerUILongPressGestureRecognizer则对应OBJC_ASSOCIATION_RETAIN进展修饰。当然实际上笔者的UIViewcategory不断这个,能够参照作者付出品种总计的生机勃勃套库WTSDK也许有一些地方描述得不是很好,大概描述不当了,希望您们能给自身留言,thank!

 var user = new { Name = "john", Age = 19 }; 
 var serializerSettings = new JsonSerializerSettings
            {
                // 设置为驼峰命名
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };
var userStr = JsonConvert.SerializeObject(user, Formatting.None, serializerSettings);

上述正是衰亡办法,看见这几个布局的资料少之又少,做下记录

本文由澳门至尊网站发布于软件综合,转载请注明出处:并让字段的首字母小写,Runtime脾性之提到对象

关键词: