我仅在 IOS 11 上发生崩溃: [__NSCFNumber isEqualToString:]: 无法识别的选择器发送到实例
Debug step by step表示崩溃发生在viewDidLoad结束后,进入viewWillApear之前
如何调试?
我尝试使用异常断点,但没有更多信息。 崩溃很明显,但我没有任何代码,在崩溃之前或之后我不使用“isEqualToString”或“objectAtIndex”...
ViewDidLoad代码:
self.buttonCancel = [[UIBarButtonItem alloc] initWithTitle:@"ANNULER" style:UIBarButtonItemStylePlain target:self action:@selector(actionCancel)];
[self.buttonCancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [EATColor colorBlack], NSForegroundColorAttributeName,[EATFont ElleNovaCExtraBold:12],NSFontAttributeName,NSKernAttributeName, @(0.66), nil] forState:UIControlStateNormal];
[super viewDidLoad];
// Do any additional setup after loading the view.
[self buildCollectionViewListing];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor clearColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"ANNULER"];
[self.collectionView registerNib:[UINib nibWithNibName:@"EATSearchRecetteCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"idSearchRecette"];
[self.collectionView registerNib:[UINib nibWithNibName:@"EATSearchArticleCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"idSearchNews"];
[self.collectionView registerNib:[UINib nibWithNibName:@"EATSearchGalleryCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"idSearchGallery"];
[self.collectionView registerNib:[UINib nibWithNibName:@"EATSearchVideoCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"idSearchVideo"];
[self.collectionView registerNib:[UINib nibWithNibName:@"EATSearchHeaderCollectionViewCell" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:EATSearchHeaderCollectionViewIdentifier];
if (self.searchedText) {
if (self.searchBar) {
self.searchBar.text = self.searchedText;
}else {
self.title = [self.searchedText uppercaseString];
}
[self loadData];
}
最佳答案
那是罪魁祸首:
[self.buttonCancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [EATColor colorBlack], NSForegroundColorAttributeName,[EATFont ElleNovaCExtraBold:12],NSFontAttributeName,NSKernAttributeName, @(0.66), nil] forState:UIControlStateNormal];
发生了什么事?
[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance
这意味着在某个时候有一个 NSNumber
对象调用一个方法 isEqualToString:
。它不知道它(它是一个 NSString
方法),然后它崩溃了。
没有说的是您不必调用自己 isEqualToString:
,它可能是 Apple SDK 代码中的隐藏调用,这就是正在发生的事情。
NSAttributedString
的属性字典必须有 NSString
键,值必须是键文档中对应的类(有时是 NSNumber
,有时是 UIFont
,UIColor
) 等
在你的例子中,你倒置了最后一个键和对象,dictionaryWithObjectsAndKeys:
等待对象,然后是键。
但在最后一个填充值中,你将它们反转 (NSKernAttributeName, @(0.66)
)。它仍然是一个有效的字典,但不遵守属性规则。
所以在某些时候 Apple 代码会检查每个键,比较它们(调用 isEqualToString:
以了解需要应用哪些效果。但它会将 @(0.66) 与 NSSomeAttributeName
([@(0.66)isEqualToString:NSSomeAttributeName]
) 而不是首先检查 @(0.66)
的类。
修复:
[self.buttonCancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [EATColor colorBlack], NSForegroundColorAttributeName,[EATFont ElleNovaCExtraBold:12],NSFontAttributeName,@(0.66), NSKernAttributeName, nil] forState:UIControlStateNormal];
写起来更简单(而且更容易一眼看出什么是键,什么是值):
[self.buttonCancel setTitleTextAttributes:@{NSForegroundColorAttributeName: [EATColor colorBlack],
NSFontAttributeName: [EATFont ElleNovaCExtraBold:12],
NSKernAttributeName: @(0.66)}
forState:UIControlStateNormal];
关于ios - NSCFNumber isEqualToString : crash IOS11 between viewDidLoad and viewWillApear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46709085/