1. 程式人生 > >『幹貨』分享你最喜歡的技巧和提示(Xcode,objective-c,swift,c...等等)

『幹貨』分享你最喜歡的技巧和提示(Xcode,objective-c,swift,c...等等)

%d 維護 函數名 self med interface oci pla track

親愛的讀者們,你們好 !年底將近,分享從過去一年你最喜歡的技巧和建議作為禮物送給新手們。提交你的最喜歡的迅速或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類型 打印


open terminal (打開終端輸入例如以下三條命令,然後重新啟動Xcode裏app就可以):

  1. touch ~/.lldbinit
  2. echo display @import UIKit >> ~/.lldbinit
  3. echo target stop-hook add -o \”target stop-hook disable\” >> ~/.lldbinit

博文地址,具體教程點此

@() 來包括C字符串 或者OC對象



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

AmIBeingDebugged(from mattt)


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

#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頭文件加權限


假設您是從DMG安裝Xcode的,看看這個技術通過Joar Wingfors,以避免通過保留全部權,權限和硬鏈接意外改動SDK頭:

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

檢查void *實例變量(from mattt)


對於逆向project的目的,可是這是能夠看的對象實例變量。它通常非常easy用valueForKey這樣獲取。

另一個情況下,它不能用valueForKey獲取。盡管:當這個變量是void *類型。

@interface MPMoviePlayerController : NSObject <MPMediaPlayback>
{
    void *_internal;    // 4 = 0x4
    BOOL _readyForDisplay;  // 8 = 0x8
}

用底層方式來訪問

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

不要使用這段代碼,它的非常危急的。僅使用於逆向project!

使用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

import這個類 , 就能用block, 參數都是通用的本身,狀態碼。字典.(灰神提供)

iOS圖片內存優化(博文)內存優化經驗(from 灰灰)


解決步驟:instrument調試後,發現沒被釋放的全是imageIO,差點兒相同就知道了。把讀圖的方式,從[UIImage imageNamed:@”“],改成imageWithContentsOfFile。就能夠了。

技術分享

問題原因:imageNamed讀取圖片的方法,會緩存在內存中,所以較大的圖片,還是用imageWithContentsOfFile。
TIPs1:.xcassets裏的圖片無法用imageWithContentsOfFile讀取;
TIPs2:imageWithContentsOfFile讀取圖片須要加文件後綴名如png。jpg等;
灰神內存優化鏈接地址點此

自己定義弱關聯對象(weak associated objects)


不幸的是,關聯對象不支持弱引用.

幸運的是, 非常easy實現

你僅僅須要一個簡單的類包裝與弱引用一個對象.

@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的層級


在控制臺裏使用po [UIViewController _printHierarchy]命令就可以打印出controller的層級,一目了然.大家都去玩玩吧~~1

技術分享

在控制臺裏打印view的層級


在控制臺裏使用po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]命令就可以打印出view的層級,一目了然.大家都去玩玩吧~~1

當然,可能對於某一些人來說打印window下的全部view層級,會認為眼花繚亂.

可是,也能夠打印指定某一個view的層級.

po [view recursiveDescription]

debug模式下的控制臺裏使用po命令打印對象的屬性和值


加入分類,加上代碼就可以.不用導入頭文件,就可以在控制臺裏使用po命令打印出model的屬性和值

#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)


iOS原生的自己主動布局(NSLayoutConstraint)非常繁瑣, 影響開發進度和可讀性也不利於維護, 正所謂工欲善其事必先利其器 , 有一個良好的自己主動布局框架, 則會讓我們事半功倍. 而ZXPAutoLayout則是解決這一問題和誕生 . 採用新穎的鏈式語法, 擴展性,可讀性,維護成本也較低.並致力打造最好用,最簡潔,最方便,最輕巧的自己主動布局.

以下一個簡單演示樣例. ZXPAutoLayout具體教程點此 – github地址點此

//設置一個背景為半透明紅色的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


在控制臺裏輸入br s -r . -s yourProjectName命令, 然後在app裏點擊一下button, 在讓斷點往下運行就可以.

ps:yourProjectName是你的project名的名字哦. 經筆者在Xcode7下使用這條命令的時候, 響應非常慢. Xcode6和Xcode5不會有這個問題, 可能是Xcode7的一個小小問題, 也不排除是我環境配置的太復雜而造成的影響~!

這條命令的具體贅述地址附上:點我就對了

自己定義並強化NSLog, 能查找LOG所打印的函數和類


//打印log
#ifdef DEBUG
#define ZXPLog(format, ...) NSLog((@"[函數名:%s]" "[行號:%d]  " format), __FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define ZXPLog(format, ...);
#endif

『幹貨』分享你最喜歡的技巧和提示(Xcode,objective-c,swift,c...等等)