IOS ID生成器

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
//  IdGenerator.m
//  Copyright (c) 2014年 青岛拓宇网络科技有限公司. All rights reserved.
//
  
#import "IdGenerator.h"
  
staticlonglongtime_stamp = 0;
staticlonglongtime_stamp_now = 0;
staticNSMutableArray*temp =NULL;
staticNSNumber*random_n =NULL;
staticNSLock*theLock =NULL;
  
@implementationIdGenerator
  
/*
 *  获取下一个Id
 */
+ (longlong)next{
      
    if(theLock ==NULL)
        theLock = [[NSLockalloc]init];
      
    if(temp ==NULL)
        temp = [[NSMutableArrayalloc]init];
      
    @synchronized(theLock){
        time_stamp_now = [[NSDatedate] timeIntervalSince1970];
        if(time_stamp_now != time_stamp){
            //清空缓存,更新时间戳
            [temp removeAllObjects];
            time_stamp = time_stamp_now;
        }
          
        //判断缓存中是否存在当前随机数
        while([temp containsObject:(random_n = [NSNumbernumberWithLong:arc4random() % 8999 + 1000])])
            ;
          
        if([temp containsObject:random_n]) {
            return-1;
        }
          
        [temp addObject:[NSNumbernumberWithLong:[random_n longValue]]];
    }
      
    return(time_stamp * 10000) + [random_n longValue];
}
  
@end