NSCFNumber isEqualToString:在viewDidLoad和viewWillApear

时间:2017-10-12 12:00:37

标签: ios objective-c unrecognized-selector

我只在IOS 11上崩溃: [__ NSCFNumber isEqualToString:]:无法识别的选择器发送到实例

逐步调试表明在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];
}

1 个答案:

答案 0 :(得分:1)

这是罪魁祸首:

[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代码会检查每个Key,比较它们(调用isEqualToString:以了解需要应用哪些效果。但它会将@(0.66)与NSSomeAttributeName进行比较({{1不首先检查[@(0.66)isEqualToString:NSSomeAttributeName]的类。

修复:

@(0.66)

编写起来比较简单(更容易看一下什么是关键,什么是价值):

[self.buttonCancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [EATColor colorBlack],  NSForegroundColorAttributeName,[EATFont ElleNovaCExtraBold:12],NSFontAttributeName,@(0.66), NSKernAttributeName,  nil] forState:UIControlStateNormal];