您的位置:首页 > 编程语言

编程技巧 - 2

2015-07-27 09:59 363 查看
8.调试的输出的宏处理

遇到一个输出日志的方法,很强大:

/**
 *  是否输出平台日志(默认为NO不输出,如果需要输出传值YES) [2.0]
 *
 *  注意:日志只为调试用,正式出包,请设置为NO,或者不调用此接口
 *
 *  @param isLog  是否输出日志
 */
+(void)isOutputLog:(BOOL)isLog;


+(void)isOutputLog:(BOOL)isLog{
    [[SYSystemInfo shareSystemInfo] setIsLog:isLog];
}


尝试了解其实现原理,上面就是一个属性的set方法的实现,非常巧妙:

@property(nonatomic,assign)  BOOL isLog;


/**  若干代码块 **/
            LOG(@"activate37SDK error, error = %@",message);
            if (_isLog) {
                DLog(@"activate37SDK error, error = %@",message);
            }
/**  若干代码块 **/


进入LOG:

#ifdef DEBUG
#define LOG(...) NSLog(__VA_ARGS__);
#else
#define LOG(...)
#endif


这种LOG就是Debug调试模式下想输出调试,而在release发布模式下想去掉NSLog的常用写法

使用如下,和NSLOG没两样:

LOG(@"通知服务器的outOrderID 值是:%@",outOrderID);


进入DLog:

#define DLog(format,...){\
NSLog((@"< 37File:%@ ,Line:%d ,Function:%s >:" format),[[NSString stringWithUTF8String:__FILE__] lastPathComponent],\
__LINE__,__func__,##__VA_ARGS__);\
}
其实就是在可能想要输出调试结果的地方添加if判断语句根据isLog的值,然后适当输出罢了,非常巧妙吧!

9.布尔判断的0和1

[rotatebtn setTag:1];
[otherRotatebtn setTag:0];
/**  其他操作  **/
-(void)Rotate:(UIButton *)rotate
{
    if (rotate.tag)
    {

    }
    else
    {

    }
}
将tag设个0和1,IF判断前所未有的简洁嘿嘿

10.NSDictionary 转 NSMutalbDictionary

如果这样:

NSMutableDictionary* newDic = [dic copy];
浅复制,崩定了

要这样:深复制:

NSMutableDictionary* newDic = [dic mutableCopy];


11.UITableView底部多余的行数去掉:

UIView* footer = [[UIView alloc] initWithFrame:CGRectZero];
    tableview.tableFooterView = footer;


12.数组添加常量

我们都知道数组只可以添加对象,一般有两种方法来达到数据添加基本数据类型的效果:

(1)先转成NSNumber:

[tmpArray addObject:[NSNumber numberWithUnsignedInteger:section]];


(2)先转成NSString:

[tmpArray addObject:[NSString stringWithFormat:@"%ld", (long)indexPath.row]];


13.桥梁结构

渠道、运营、CP,如果运营想充当一个桥梁的作用(并且这个桥梁具备阻挡渠道和CP的直接对接的作用),这个时候就完美体现出封装的魅力了。

然后,渠道和CP需要的数据通讯,操作交互等那要怎么做? 不用担心,有回调和通知:

通知:渠道方初始化操作内部会自动帮你post,相当于执行

回调:要我们去实现delegate协议,然后自动调用

14.好看的注释格式

/**
 *  设置工程方向
 *
 *
 *  @param   portrait  为YES时支持默认竖屏方向,NO时不支持
 *  @param   portraitUpsideDown   为YES时支持向下竖屏方向,NO时不支持
 *  @param   landscapeLeft   为YES时支持向左横屏屏方向,NO时不支持
 *  @param   landscapeRight  为YES时支持向右横屏屏方向,NO时不支持
 */


15.优雅地判断屏幕方向

BOOL isLandscape = NO;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

isLandscape = ((orientation==UIInterfaceOrientationLandscapeRight)||(orientation==UIInterfaceOrientationLandscapeLeft));

if(isLandscape)
{        
    //横屏`
}
else
{              
    //纵屏
}


由这个枚举类型可以知道,用UIDevice效果是完全一样的:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};


BOOL isLandScape = NO;
UIDeviceOrientation oritation  = [UIDevice currentDevice].orientation;
isLandScape = ((oritation == UIDeviceOrientationLandscapeLeft) || (oritation == UIDeviceOrientationLandscapeRight));

if (isLandScape)
{
    //横屏
}
else
{
    //竖屏
}


15.用宏来获取key window:

UIWindow *keyWindow = GetKeyWindow;
#define GetKeyWindow  KEYWINDOW ? KEYWINDOW : [[UIApplication sharedApplication].windows objectAtIndex:0]


16.各类尺寸:

各类尺寸:http://www.zhihu.com/question/20248971

17.自定义UIView的初始化:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initialData];
    }
    return self;
}

- (void)initialData
{   
// Initialization code
}


直接继承initFrame,并且封装初始化操作

待续...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: