我正在尝试在我的应用程序中实现CoreSpotlight API,但似乎无法弄清为什么在实现过程中出现BAD_ACCESS异常:
CSSearchableItemAttributeSet * attributeSet = [CSSearchableItemAttributeSet new];
attributeSet.title = route.Options.name;
attributeSet.keywords = [route.Options.name componentsSeparatedByString:@" "];
UIImage *image = [UIImage imageNamed:@"pin_busstop.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
attributeSet.thumbnailData = imageData;
CSSearchableItem * item = [[CSSearchableItem alloc] initWithUniqueIdentifier:route.ObjectID domainIdentifier:@"com.whatever.itsmyappsname.loadwithroute" attributeSet:attributeSet];
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:[NSArray arrayWithObject:item] completionHandler:^(NSError * _Nullable error) {
NSLog(@"It worked");
}];
查看异常的调用堆栈,可以看到它发生在
CSSearchableIndex indexSearchableItems: completionHandler:
调用中。但是,我可以在没有异常触发的情况下跳过该调用,这可能与完成处理程序有关,但是无论是否有此调用,它都会发生。我在.h文件和目标文件中都导入了CoreSpotlight/CoreSpotlight.h
和MobileCoreServices/MobileCoreServices.h
。
最佳答案
您(我)错误地创建了CSSearchableItemAttributeSet对象。代替:
CSSearchableItemAttributeSet * attributeSet = [CSSearchableItemAttributeSet new];
用:
CSSearchableItemAttributeSet * attributeSet = [[CSSearchableItemAttributeSet alloc]
initWithItemContentType:(NSString *)kUTTypeImage];
关于ios - 调用CSSearchableIndex indexSearchableItems时为BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33060066/