分享你最喜欢的技巧和提示(Xcode,Objective-C,Swift,C...等等)

简介:

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

Objective-C

1.让Xcode的控制台支持LLDB类型的打印

这有什么用?

怎么说尼,笔者认为这个还是比较有用滴,为什么有用尼?

因为在Xcode断点调试的时候, 在控制台输入 po self.view.frame 或者 po id 类型的时候就死翘翘了。
不信? 看如下图 :

1.png

进入正题

打开终端输入三条命令:

1. touch ~/.lldbinit

2. echo display @import UIKit >> ~/.lldbinit

3. echo target stop-hook add -o \"target stop-hook disable\" >> ~/.lldbinit

输完命令后没有任何提示? 好吧, 那恭喜你成功了~! 然后, 最关键的一步来了, 那就是…

重新运行项目(不用重启Xcode也可以),看如下图~~

QQ截图20151231145404.png

就代表成功啦

那么现在我们继续在控制台输入po self.view.frame

QQ截图20151231145414.png

成功了!如果po指令是一个id类型也可以正常打印。是不是感觉方便很多呀? 反正我是这么觉得。至于有没有用就看个人需要咯~~!

如何删除?

好吧, 那么问题来了, 我用命令创建, 如果不想玩了怎么办尼??

其实很简答, 看第一条命令touch ~/.lldbinit,就是在根目录下创建了一个隐藏文件.lldbinit,然后删除这个文件就搞定啦。
打开终端然后,在终端输入 :?
rm ~/.lldbinit 命令即可.

2.用宏定义检测block是否可用!

1
2
3
4
5
6
7
8
9
10
11
12
#define BLOCK_EXEC(block, ...) if (block) { block(__VA_ARGS__); };    
 
// 宏定义之前的用法  
/* 
if (completionBlock)   
{   
     completionBlock(arg1, arg2);   
}   
   */  
   
// 宏定义之后的用法  
BLOCK_EXEC(completionBlock, arg1, arg2);

3.用@() 来包含C字符串 或者非OC对象

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

4.AmIBeingDebugged(from mattt)

Nolan O’Brien brings the AmIBeingDebugged function to our attention from from this Technical Q&A document:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;
}</sys></unistd.h></sys></stdbool.h></assert.h>

5.给SDK头文件加权限

如果您是从DMG安装Xcode的,看看这个技术通过Joar Wingfors,以避免通过保留所有权,权限和硬链接意外修改SDK头:

$ sudo ditto /Volumes/Xcode/Xcode.app /Applications/Xcode.app

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

对于逆向工程的目的,但是这是可以看的对象实例变量。它通常很容易用valueForKey这样获取。

还有一个情况下,它不能用valueForKey获取,虽然:当这个变量是void *类型。

1
2
3
4
5
@interface MPMoviePlayerController : NSObject <mpmediaplayback>
{
     void *_internal;     // 4 = 0x4
     BOOL _readyForDisplay;   // 8 = 0x8
}</mpmediaplayback>

用底层方式来访问

1
id internal = *((const id*)(void*)((uintptr_t)moviePlayerController + sizeof(Class)));

不要使用这段代码,它的非常危险的。仅使用于逆向工程!

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

1
2
3
4
5
6
//使用ARC和不使用ARC
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif

8.读取本地图片(from 夏夏)

1
2
3
4
#define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[NSBundle mainBundle]pathForResource:file ofType:ext]
 
//定义UIImage对象
#define IMAGE(A) [UIImage imageWithContentsOfFile:[NSBundle mainBundle] pathForResource:A ofType:nil]

9.一个通用回调的简单示例(from 灰灰)

.h文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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</uikit>

.m 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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</objc>

import这个类 , 就能用block, 参数都是通用的本身,状态码,字典.(灰神提供)

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

解决步骤:instrument调试后,发现没被释放的全是imageIO,差不多就知道了,把读图的方式,从[UIImage imageNamed:@”“],改成imageWithContentsOfFile,就可以了。

256780-803deeacec833571.png

问题原因:imageNamed读取图片的方法,会缓存在内存中,所以较大的图片,还是用imageWithContentsOfFile。?

Tip1:.xcassets里的图片无法用imageWithContentsOfFile读取;?

Tip2:imageWithContentsOfFile读取图片需要加文件后缀名如png,jpg等;?

灰神内存优化链接地址点此

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

不幸的是,关联对象不支持弱引用。幸运的是,很容易实现。

你只需要一个简单的类包装与弱引用一个对象.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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

设置与获取

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

12.在控制台里打印controller的层级

在控制台里使用po [UIViewController _printHierarchy]命令即可打印出controller的层级,一目了然.大家都去玩玩吧~~1

02.png
13.在控制台里打印view的层级

在控制台里使用po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]命令即可打印出view的层级,一目了然。

当然,可能对于某一些人来说打印window下的所有view层级,会觉得眼花缭乱。

但是,也可以打印指定某一个view的层级。

1
po?[view?recursiveDescription]

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

添加分类,加上代码即可。不用导入头文件,即可在控制台里使用po命令打印出model的属性和值
400.png

15.给category添加属性的小技巧

这是运用到了对象关联, 如果不会的请看这篇文章: 时空传送门

.h 文件

1
2
3
4
5
#import @interface NSObject (ZXPDebugDescription)
 
@property (copy,nonatomic) NSString *zxp_testString;
 
@end

.m 文件

1
2
3
4
5
6
7
8
9
10
#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</objc>

16.AutoLayout框架介绍(ZXPAutoLayout)

iOS原生的自动布局(NSLayoutConstraint)非常繁琐, 影响开发进度和可读性也不利于维护, 正所谓工欲善其事必先利其器 , 有一个良好的自动布局框架, 则会让我们事半功倍. 而ZXPAutoLayout则是解决这一问题和诞生 . 采用新颖的链式语法, 扩展性,可读性,维护成本也较低.并致力打造最好用,最简洁,最方便,最轻巧的自动布局。

以下一个简单示例。ZXPAutoLayout详细教程点此-github地址点此

1
2
3
4
5
6
7
8
9
10
//设置一个背景为半透明红色的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);
     }];

17.动态调用block(黑魔法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//定义一个block
id (^testBlock)(NSString *string,NSArray *array) = ^id(NSString *string,NSArray *array) {
             NSLog(@ "param:--%@--%@" ,string,array);
             return  string;
         };
         
         // _Block_signature  是iOS的私有api
         const char * _Block_signature(void *);
         const char * 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)


目录
相关文章
|
6月前
|
编译器 Swift iOS开发
44 Swift和Objective-C的运行时简介
Swift和Objective-C的运行时简介
45 0
|
2月前
|
Swift iOS开发 MacOS
解决新版xcode下swift .infinity报Invalid frame dimension (negative or non-finite)错误
解决新版xcode下swift .infinity报Invalid frame dimension (negative or non-finite)错误
|
6月前
|
安全 程序员 编译器
2 Swift 和 Objective-C的区别
Swift 和 Objective-C的区别
51 0
|
前端开发 JavaScript Go
2022 软件工程师状况报告:Go 最抢手|开发者最“嫌弃” PHP、Swift、Scala/R/Objective-C
2022 软件工程师状况报告:Go 最抢手|开发者最“嫌弃” PHP、Swift、Scala/R/Objective-C
129 0
2022 软件工程师状况报告:Go 最抢手|开发者最“嫌弃” PHP、Swift、Scala/R/Objective-C
|
SQL 前端开发 JavaScript
2021最受欢迎的编程语言排行:苹果编程语言Objective-C被Swift取代
苹果公司的Objective-C最终退出了Tiobe跟踪的流行编程语言的前20名,但是「恐龙」语言却获得了意想不到的推动,Swift语言取代Objective-C的时代要来了。
286 0
2021最受欢迎的编程语言排行:苹果编程语言Objective-C被Swift取代
|
自然语言处理 Go iOS开发
iOS开发者知识普及,Swift 挑战 Objective-C,谁会笑到最后?
如果你的公司也正计划跟随 iOS 软件开发,那么你需要作出一个至关重要的决定。你需要为你的产品选择合适的技术组合。你的目标是什么?如果是原生应用开发而不是跨平台软件,你会选用哪种编程语言?久经考验的 Objective-C 还是 Swift?

相关课程

更多