IOS ID生成器

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

//
//  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