Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:生成随机对象ID_Ios_Objective C_Iphone - Fatal编程技术网

iOS:生成随机对象ID

iOS:生成随机对象ID,ios,objective-c,iphone,Ios,Objective C,Iphone,我想使用此方法获取核心数据的随机实体: randomQuote = [managedObjectContext objectWithID:randomID]; 但是我不知道如何设置一个随机ID,它是NSManagedObjectID的一个实例。有人能帮我吗?用这个: NSFetchRequest *myRequest = [[NSFetchRequest alloc] init]; [myRequest setEntity: [NSEntityDescription entityForName

我想使用此方法获取核心数据的随机实体:

randomQuote = [managedObjectContext objectWithID:randomID];
但是我不知道如何设置一个随机ID,它是NSManagedObjectID的一个实例。有人能帮我吗?

用这个:

NSFetchRequest *myRequest = [[NSFetchRequest alloc] init];
[myRequest setEntity: [NSEntityDescription entityForName:myEntityName inManagedObjectContext: managedObjectContext]];
NSError *error = nil;
NSUInteger myEntityCount = [managedObjectContext countForFetchRequest:myRequest error:&error];    

NSUInteger offset = myEntityCount - (arc4random() % myEntityCount);
[myRequest setFetchOffset:offset];
[myRequest setFetchLimit:1];

NSArray* objects = [managedObjectContext executeFetchRequest error:&error];    
id randomQuote = [objects objectAtIndex:0];

有一种生成uuid的方法:


可能重复的谢谢!向前一步,我想实现一个名为“每日报价”的函数。这意味着报价是每天随机选择的,而不是每次打开应用程序时都更改。有什么建议吗?
    NSString *uuid = [[NSUUID UUID] UUIDString];