iOS手札

随机数

arc4random()
0-10之间的随机数
arc4random()%10
0-100之间的随机数
arc4random()%100
......

随机颜色

[UIColor colorWithRed: (arc4random()%256/256.0) green: (arc4random()%256/256.0) blue:(arc4random()% 256/256.0)  alpha:1]

字符串排序

NSArray * strArray = @[@"as",@"fd",@"bd",@"wd",@"ca"];
NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch|NSNumericSearch|NSWidthInsensitiveSearch|NSForcedOrderingSearch;
NSComparator sort = ^(NSString *obj1,NSString *obj2){
       NSRange range = NSMakeRange(0,obj1.length);
       return [obj1 compare:obj2 options:comparisonOptions range:range];
};
NSArray *resultArray = [strArray sortedArrayUsingComparator:sort];
NSLog(@"%@",resultArray);

你可能感兴趣的:(iOS手札)