ios - 调用CSSearchableIndex indexSearchableItems时为BAD_ACCESS

标签 ios objective-c

我正在尝试在我的应用程序中实现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.hMobileCoreServices/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/

相关文章:

objective-c - NSString intValue 如果我从字节缓冲区中的字符串的开头对其进行子字符串化,则返回 0

ios - UIButton 的动画背景颜色从白色变为红色

iphone - 如何弄清楚使用的 SEL 名称是什么

javascript - 我怎样才能减少expo build ios的大小,它大约是188 MB,

ios - 是否可以将闭包传递给选择器?

iphone - 如何为我的第一个 View 隐藏 UINavigationBar

ios - 编辑时 UITextView 光标完全消失

ios - 让我知道这段代码的功能

iphone - Objective-C 和 C++ 中结构的大小

ios - 在 UIImageView 中水平翻转 UIImage