我試圖用Core Data創建一個隨機函數的新程序員。我想要的是標籤從我的實體中返回一個隨機的managedObject。這是我的代碼Core Data Xcode中的隨機函數
- (IBAction)randomize{
NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Restraunt" inManagedObjectContext:context ];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
NSString *textString;
int RandomNumber;
RandomNumber = arc4random() %[matchingData count];
textString = [matchingData objectAtIndex:RandomNumber];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"restraunt_name like %@",textString];
[request setPredicate:predicate];
if(matchingData.count <= 0){
self.label.text = @"doesnt exist";}
else {
NSString *string;
for (NSString *obj in matchingData){
string = [obj valueForKey:@"restraunt_name"];
}
self.label.text = [NSString stringWithFormat:@" %@", string] ;
}
}
你看到了什麼?你期望看到什麼? – Abizern
我希望看到字符串,但我看到在內存中的位置 – user3481875