在Python中字典根据多项规则排序的方法

yipeiwu_com6年前Python基础

我们做登录的时候经常会使用到,验证手机号是否正确、向手机发送验证码倒计时60s的问题,我们改如何解决呢?让我们一起来探讨一下吧。如下图:

首先,我们先说说判断手机号码是否正确的问题吧,我的想法是给字符串添加一个分类,然后写上这样的代码:
+ (BOOL)valiMobile:(NSString *)mobile{
        if (mobile.length != 11){
            //判断手机号码是否为11位
            return NO;
            }else{
                //使用正则表达式的方法来判断手机号
/**
* 移动号段正则表达式
  */
                NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 联通号段正则表达式
*/
                 NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 电信号段正则表达式
*/
                NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
//初始化NSPredicate对象
                NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
//与具体对象进行筛选判断, 返回为BOOL值
                BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
                NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
                BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
                NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
            BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
                if (isMatch1 || isMatch2 || isMatch3) {
                    return YES;
                    }else{
                return NO;
            }
        }
}
如果大家对于NSPredicate的用法有些疑问的话可以看看这篇文章:http://www.jianshu.com/p/d4098bc9488d下面再来说一说验证码倒计时的问题,1、我给button创建了一个分类2、设定button上的文字,并记录倒计时的总时长,然后开一个定时器,并且关闭button的点击事件3、定时器中将总时间缩减,并且设置button的文字,然后做一个判断,判断时间是否归为0,如果为0 就释放定时器,然后设置button上的文字,然后打开用户交互。代码如下:.h文件中
#import@interface UIButton (BtnTime)
/**
按钮倒计时的问题
@param countDownTime 倒计时的时间(分钟)
*/
- (void)buttonWithTime:(CGFloat)countDownTime;
@end
.m文件中
#import "UIButton+BtnTime.h"
/** 倒计时的显示时间 */
static NSInteger secondsCountDown;
/** 记录总共的时间 */
static NSInteger allTime;
@implementation UIButton (BtnTime)
- (void)buttonWithTime:(CGFloat)countDownTime {
self.userInteractionEnabled = NO;
secondsCountDown = 60 * countDownTime;
allTime = 60 * countDownTime;
[self setTitle:[NSString stringWithFormat:@"%lds后重新获取",secondsCountDown] forState:UIControlStateNormal];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod:) userInfo:nil repeats:YES];
}
-(void)timeFireMethod:(NSTimer *)countDownTimer{
//倒计时-1
secondsCountDown--;
//修改倒计时标签现实内容
[self setTitle:[NSString stringWithFormat:@"%lds后重新获取",secondsCountDown] forState:UIControlStateNormal];
//当倒计时到0时,做需要的操作,比如验证码过期不能提交
if(secondsCountDown == 0){
[countDownTimer invalidate];
[self setTitle:@"重新获取" forState:UIControlStateNormal];
secondsCountDown = allTime;
self.userInteractionEnabled = YES;
}
}
@end

代码已经上传到github上去了,地址:https://github.com/zhangyqyx/Countdown

作者:谁遇而安
链接:https://www.jianshu.com/p/d9fbfd8bff75
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

相关文章

python 自动批量打开网页的示例

如下所示: import webbrowser import codecs import time with open("test.txt") as fp: for ebayno...

Python 下载及安装详细步骤

Python 下载及安装详细步骤

安装python分三个步骤: *下载python *安装python *检查是否安装成功 1、下载Python (1)python下载地址https://www.python.org/d...

python创建学生成绩管理系统

python创建学生成绩管理系统

python学生成绩管理系统创建,供大家参考,具体内容如下 要求编写学生类,班级类,并在电脑运行生成表单,输入一个数字,得到对应的结果。 输出样式 代码如下 学生类 class S...

Python中的 ansible 动态Inventory 脚本

Python中的 ansible 动态Inventory 脚本

1.Ansible Inventory  介绍; Ansible Inventory 是包含静态 Inventory 和动态 Inventory 两部分的,静态 Invento...

Python基于pandas实现json格式转换成dataframe的方法

本文实例讲述了Python基于pandas实现json格式转换成dataframe的方法。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #!pyth...