• 随机字符串 – 生成指定长度的字符串
  1. -(NSString *)randomStringWithLength:(NSInteger)len {
  2. NSString *letters = @”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″;
  3. NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
  4. for (NSInteger i = 0; i < len; i++) {
  5. [randomString appendFormat: @”%C”, [letters characterAtIndex: arc4random_uniform([letters length])]];
  6. }
  7. return randomString;
  8. }
  • 指定字符串随机生成指定长度的新字符串
  1. -(NSString *)randomStringWithLength:(NSInteger)len String:(NSString *)letters {
  2. NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
  3. for (NSInteger i = 0; i < len; i++) {
  4. [randomString appendFormat: @”%C”, [letters characterAtIndex: arc4random_uniform([letters length])]];
  5. }
  6. return randomString;
  7. }
  8. //调用方法
  9. -(NSString *)generateCard{
  10. //HXMF 123456789TJQK
  11. NSString *color =[self randomStringWithLength:1 String:@”HXMF”];
  12. NSString *number = [self randomStringWithLength:1 String:@”123456789TJQK”];
  13. return [NSString stringWithFormat:@”%@%@”,color,number];