Objective-C 常用代码
这里会归纳一些在Objective-C开发中常用的代码 代码会慢慢增多 =)
生成一定范围的随机数
-(float)RandomFloatStart:(float)a end:(float)b { srand((unsigned)time(0)); float random = ((float) rand()) / (float) RAND_MAX; float diff = b - a; float r = random * diff; return a + r; }
验证邮箱是否合法
- (BOOL) validEmail:(NSString*) emailString { if([emailString length]==0){ return NO; } NSString *regExPattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil]; NSUInteger regExMatches = [regEx numberOfMatchesInString:emailString options:0 range:NSMakeRange(0, [emailString length])]; NSLog(@"%i", regExMatches); if (regExMatches == 0) { return NO; } else { return YES; } }
判断手机是否越狱
+(BOOL)isJailbroken { NSString *filePath = @"/Applications/Cydia.app"; if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { return YES; } else { return NO; } }
or