OC常用代码(非UI)

---------------多线程---------------

dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_enter(group);
dispatch_async(queue, ^{
  dispatch_group_leave(group);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{

});

---------------通知---------------

 //发送通知
NSNotification *notification = [NSNotification notificationWithName:KaddOthersNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotification:notification];
//接受通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hiddenGrayBackView) name:KOthersNotification object:nil];
//移除通知,谁创建,谁移除
[[NSNotificationCenter defaultCenter] removeObserver:self];

---------------枚举---------------

typedef NS_ENUM(NSInteger, XMGType)
{
    XMGTypeTop,
    XMGTypeBottom,
};

---------------NSString---------------

//从字符串中抽出数字(抽出价格)
NSString *totalStr = [[_amountLab.text componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet]] componentsJoinedByString:@""];

---------------NSDictionary---------------

//判断字典是否包含特定Key值
[[dict allKeys] containsObject:@"orderId"]
//字典转移
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:self.wayArray[indexPath.row]];
NSMutableDictionary *dict = [self.wayArray[indexPath.row] mutableCopy];

---------------NSArray---------------

//把数组倒序
[[self.listArr reverseObjectEnumerator] allObjects];

关于使用NSMutableArray,removeObjectAtIndex出现错误.
直接进行赋值,会让NSMutableArray转变为NSArray.这样执行 removeObjectAtIndex就会不成功,造成crash.
规范:
[self.historyArr addObjectsFromArray:data];

---------------待分类---------------

//多线程,同步。我用于,只有等网络请求完成,并且保存数据后,才能跳转页面
dispatch_group_t group = dispatch_group_create();//创建一个调度任务组group
dispatch_group_enter(group);
dispatch_group_leave(group);//有enter就必须要有leave
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
      //只有等group里的任务都执行完毕后才会来到该方法
});

//3秒后执行wait方法,用于SVProgressHUD
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(wait) userInfo:nil repeats:NO];

//虚线代码
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
        CGFloat lenghts[] = {4.0f,4.0f};
        UIImage *image = [UIImage imageWithColor:kPSThemeWeakColor10 size:CGSizeMake(SCREEN_WIDTH - 50, 0.5) dashLineLengths:lenghts counts:2];
        dispatch_async(dispatch_get_main_queue(), ^{
            _lineView.image = image;
        });
    });

Xcode头文件锁定
1, 打开终端
2, 前往Xcode.app, 命令: cd /Applications/Xcode.app
3, 把头文件修改为只读, 命令: sudo chown -hR root:wheel Contents
4, 缓存完成, 重启Xcode更新

显示隐藏文件defaults write com.apple.finder AppleShowAllFiles -bool true
隐藏隐藏文件defaults write com.apple.finder AppleShowAllFiles -bool false
命令行终端敲入命令:last | grep reboot (查看开机时间记录)
命令行终端敲入命令:last | grep shutdown (查看关机时间记录)

//将data数据转换成16进制的字符串
+ (NSString *)hexStringFromData:(NSData *)myD{  
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    NSLog(@"hex = %@",hexStr);
    return hexStr;
}

---------------加密---------------

//md5加密
- (NSString *) md5:(NSString *) input {
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
}

你可能感兴趣的:(OC常用代码(非UI))