亲爱的读者们,你们好 !年底将近,分享从过去一年你最喜欢的技巧和建议作为礼物送给新手们。提交你的最喜欢的迅速或objc琐事,实用的提示,意外的发现,实用的解决方法,没用的迷恋,或不论什么其它你认为今年非常酷。

就在以下写下你的评论!

笔者分享总结例如以下(本篇会不定期进行更新) :

objective-c


用宏定义检測block是否可用~!



#define BLOCK_EXEC(block, ...) if (block) { block(__VA_ARGS__); }; // 宏定义之前的使用方法
/*
if (completionBlock)
{
completionBlock(arg1, arg2);
}
*/ // 宏定义之后的使用方法
BLOCK_EXEC(completionBlock, arg1, arg2);

在控制台里支持LLDB类型的打印 :po framepo id类型 打印


@() 来包括C字符串 或者OC对象



NSString *propertyAttributesString =
@(property_getAttributes(class_getProperty([NSObject class], "description")));
// T@"NSString",R,C

AmIBeingDebugged(from mattt)


#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h> static Bool AmIBeingDebugged(void) {
int mib[4];
struct kinfo_proc info;
size_t size = sizeof(info); info.kp_proc.p_flag = 0; mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid(); sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); return (info.kp_proc.p_flag & P_TRACED) != 0;
}

给SDK头文件加权限


检查void *实例变量(from mattt)


@interface MPMoviePlayerController : NSObject <MPMediaPlayback>
{
void *_internal; // 4 = 0x4
BOOL _readyForDisplay; // 8 = 0x8
}
id internal = *((const id*)(void*)((uintptr_t)moviePlayerController + sizeof(Class)));

使用ARC和不使用ARC(from 夏夏)


//使用ARC和不使用ARC
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif

读取本地图片(from 夏夏)


#define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[NSBundle mainBundle]pathForResource:file ofType:ext]

//定义UIImage对象
#define IMAGE(A) [UIImage imageWithContentsOfFile:[NSBundle mainBundle] pathForResource:A ofType:nil]

一个通用回调的简单演示样例(from 灰灰)


.h文件

#import <UIKit/UIKit.h>

@interface UIViewController (LHYBlock)

#pragma mark - block

@property (nonatomic, copy) void (^viewControllerActionBlock)(UIViewController *vc, NSUInteger type, NSDictionary *dict);

#pragma mark - viewControllerAction

/**
* View 事件的block回调
*
* @param viewControllerActionBlock block的參数有view本身,状态码,键值对。
*/
- (void)viewControllerAction:(void (^)(UIViewController *vc, NSUInteger type, NSDictionary *dict))viewControllerActionBlock; @end

.m 文件

#import "UIViewController+LHYBlock.h"
#import <objc/runtime.h>
@implementation UIViewController (LHYBlock)
#pragma mark - runtime associate - (void)setViewControllerActionBlock:(void (^)(UIViewController *vc, NSUInteger type, NSDictionary *dict))viewControllerActionBlock {
objc_setAssociatedObject(self, @selector(viewControllerActionBlock), viewControllerActionBlock, OBJC_ASSOCIATION_COPY);
} - (void (^)(UIViewController *, NSUInteger, NSDictionary *))viewControllerActionBlock {
return objc_getAssociatedObject(self, @selector(viewControllerActionBlock));
} #pragma mark - block - (void)viewControllerAction:(void (^)(UIViewController *vc, NSUInteger type, NSDictionary *dict))viewControllerActionBlock {
self.viewControllerActionBlock = nil;
self.viewControllerActionBlock = [viewControllerActionBlock copy];
} #pragma mark -
@end

iOS图片内存优化(博文)内存优化经验(from 灰灰)


『干货』分享你最喜欢的技巧和提示(Xcode,objective-c,swift,c...等等)-LMLPHP

自己定义弱关联对象(weak associated objects)


@interface WeakObjectContainter : NSObject
@property (nonatomic, readonly, weak) id object;
@end @implementation WeakObjectContainter
- (instancetype)initWithObject:(id)object {
self = [super init];
if (!self) {
return nil;
} _object = object; return self;
}
@end

// 设置弱引用关联
objc_setAssociatedObject(self, &MyKey, [[WeakObjectContainter alloc] initWithObject:object], OBJC_ASSOCIATION_RETAIN_NONATOMIC); //获取弱引用关联
id object = [objc_getAssociatedObject(self, &MyKey) object];

在控制台里打印controller的层级


在控制台里打印view的层级


debug模式下的控制台里使用po命令打印对象的属性和值


#import "NSObject+ZXPDebugDescription.h"
#import <objc/runtime.h> @implementation NSObject (ZXPDebugDescription) + (void)load {
method_exchangeImplementations(class_getInstanceMethod([self class], @selector(debugDescription)), class_getInstanceMethod([self class], @selector(zxp_swizzleDebugDescription)));
} - (NSString *)zxp_swizzleDebugDescription { //一把情况下,假设不是entity或者model的子类就不须要打印属性, 比方系统的class.~. 这个依照个人需求而定
if (![self isKindOfClass:[ZXPBaseEntity class]] || ![self isKindOfClass:[ZXPBaseModel class]]) {
return [self zxp_swizzleDebugDescription];
}
// 以上代码是推断是否model或者entity NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; uint count;
objc_property_t *properties = class_copyPropertyList([self class], &count); for (int i = 0; i<count; i++) {
objc_property_t property = properties[i];
NSString *name = @(property_getName(property));
id value = [self valueForKey:name]? :@"nil";
[dictionary setObject:value forKey:name];
} free(properties); return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];
} @end

给category加入属性的小技巧


.h 文件

#import <Foundation/Foundation.h>

@interface NSObject (ZXPDebugDescription)

@property (copy,nonatomic) NSString *zxp_testString;

@end

.m 文件

#import "NSObject+ZXPDebugDescription.h"
#import <objc/runtime.h> @implementation NSObject (ZXPDebugDescription) - (void)setZxp_testString:(NSString *)zxp_testString {
objc_setAssociatedObject(self, @selector(zxp_testString), zxp_testString, OBJC_ASSOCIATION_COPY_NONATOMIC);
} - (NSString *)zxp_testString {
return objc_getAssociatedObject(self, @selector(zxp_testString));
}
@end

autolayout框架介绍(ZXPAutoLayout)


//设置一个背景为半透明红色的view,上下左右四边都距离superview的距离为10
UIView *bgView = [UIView new];
[self.view addSubview:bgView];
bgView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.5];
[bgView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
//上下左右四边都距离superview的距离为10
layout.edgeInsets(UIEdgeInsetsMake(10, 10, 10, 10)); //也能够例如以下这行代码来设置,但要同一时候设置top,left,bottom,right.推荐以上写法,比較简洁.
//layout.topSpace(10).leftSpace(10).bottomSpace(10).rightSpace(10);
}];

动态调用block(黑魔法)


//定义一个block
id (^testBlock)(NSString *string,NSArray *array) = ^id(NSString *string,NSArray *array) {
NSLog(@"param:--%@--%@",string,array);
return string;
}; // _Block_signature 是iOS的私有api
const void *_Block_signature(void *);
const void *signature = _Block_signature((__bridge void *)(testBlock)); NSMethodSignature *methodSignature = [NSMethodSignature signatureWithObjCTypes:signature];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:testBlock]; NSString *string = @"string";
[invocation setArgument:&string atIndex:1]; NSArray *array = @[@"xx",@"oo"];
[invocation setArgument:&array atIndex:2]; [invocation invoke]; id returnValue;
[invocation getReturnValue:&returnValue];
NSLog(@"returnValue : %@",returnValue);

利用断点查找button的action


自己定义并强化NSLog, 能查找LOG所打印的函数和类


//打印log
#ifdef DEBUG
#define ZXPLog(format, ...) NSLog((@"[函数名:%s]" "[行号:%d] " format), __FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define ZXPLog(format, ...);
#endif
05-21 08:34