Xcode -[__NSArrayM isEqualToString:]:无法识别的选择器发送到实例 0x7a1248d0 - 如何修复?
Posted
技术标签:
【中文标题】Xcode -[__NSArrayM isEqualToString:]:无法识别的选择器发送到实例 0x7a1248d0 - 如何修复?【英文标题】:Xcode -[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x7a1248d0 - How to fix? 【发布时间】:2014-10-14 09:45:47 【问题描述】:在展示我的代码之前,请耐心等待,我是 *** 和 ObjectiveC 的新手。
我的问题出现了,当我尝试这个时:
NSLog(@"%@",self.side.article.categories);
NSLog(@"%@",side_.article.categories);
[NSString stringWithFormat:@"%@",self.side.article.categories];
if ([self.side.article.categories isEqualToString:@"4"]) //here
[[IOLSession defaultSession] logEventWithType:IOLEventTypeView
state:IOLViewAppeared
category:@"art_meldungen-des-tages"
comment:nil];
我尝试的是从 JSON 中获取类别的值:
"articles": ["id":xxx,"date":xxx,"lastChanged":xxx,"weblink":
"xxx","annotation":"xxx","imageURL": "xxx",
"thumbnailurl": "xxx","categories": [3], ...
现在我如何获取它们:
- (void)updateArticlesWithParsedContent:(NSDictionary *)parsedContent
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
NSFetchRequest *existingArticlesFetchRequest = [[[NSFetchRequest alloc] init] autorelease];
existingArticlesFetchRequest.entity = [NSEntityDescription entityForName:@"Article" inManagedObjectContext:managedObjectContext];
NSArray *existingArticlesFetchResult = [managedObjectContext executeFetchRequest:existingArticlesFetchRequest error:NULL];
NSMutableDictionary *existingArticles = [NSMutableDictionary dictionaryWithObjects:existingArticlesFetchResult forKeys:[existingArticlesFetchResult valueForKey:@"articleID"]];
NSMutableSet *articlesInParsedContentSet = [NSMutableSet set];
NSArray *parsedArticles = [parsedContent objectForKey:@"articles"];
for (NSDictionary *parsedArticle in parsedArticles)
NSNumber *articleID = [parsedArticle objectForKey:@"id"];
Article *article = [existingArticles objectForKey:articleID];
if (!article)
article = [NSEntityDescription insertNewObjectForEntityForName:@"Article" inManagedObjectContext:managedObjectContext];
article.articleID = articleID;
[existingArticles setObject:article forKey:articleID];
[articlesInParsedContentSet addObject:article];
NSDate *parsedLastChanged = [NSDate dateWithTimeIntervalSince1970:[[parsedArticle objectForKey:@"lastChanged"] doubleValue]];
if (!article.lastChanged || ([article.lastChanged compare:parsedLastChanged] == NSOrderedAscending))
article.date = [NSDate dateWithTimeIntervalSince1970:[[parsedArticle objectForKey:@"date"] doubleValue]];
article.annotation = [parsedArticle objectForKey:@"annotation"];
article.weblink = [self baselineURLStringAppendedWithString:[parsedArticle valueForKey:@"weblink"]];
article.imageURL = [self baselineURLStringAppendedWithString:[parsedArticle valueForKey:@"imageURL"]];
article.thumbnailURL = [self baselineURLStringAppendedWithString:[parsedArticle valueForKey:@"thumbnailURL"]];
--> article.categories=[parsedArticle objectForKey:@"categories"];
还有我的 Articles.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Side;
@interface Article : NSManagedObject
@property (nonatomic, retain) NSString * annotation;
@property (nonatomic, retain) NSNumber * articleID;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSString * imageURL;
@property (nonatomic, retain) NSDate * lastChanged;
@property (nonatomic, retain) NSString * thumbnailURL;
@property (nonatomic, retain) NSString * weblink;
@property (nonatomic, retain) NSNumber * hasBeenRead;
@property (nonatomic, retain) NSSet *sides;
@property (nonatomic, retain) NSString *categories;
@end
@interface Article (CoreDataGeneratedAccessors)
- (void)addSidesObject:(Side *)value;
- (void)removeSidesObject:(Side *)value;
- (void)addSides:(NSSet *)values;
- (void)removeSides:(NSSet *)values;
@end
更新: 我照你说的做了,但还是不行。
NSLog(@"%@",self.side.article.categories); // it shows the value of categories
NSNumber *num = [self.side.article.categories objectAtIndex:0 ];
NSLog (@"%@", num); // it shows the value of categories
if ([[self.side.article.categories objectAtIndex:0 ]isEqualToString:@"4"]) //here
[[IOLSession defaultSession] logEventWithType:IOLEventTypeView
state:IOLViewAppeared
category:@"art_meldungen-des-tages"
comment:nil];
我刚刚得到:(lldb) EXT_BAD_ACCESS (Code=2 , Adress= 0x4)!
PS:我还更改了@property (nonatomic, retain) NSString *categories; @property (nonatomic, 保留) NSNumber *categories; .
解决办法是:
if ([NSString stringWithFormat:@"%@", [self.side.article.categories objectAtIndex:0]] isEqual:@"4") ...
【问题讨论】:
categories 似乎包含数组对象。尝试使用 objectAtIndex。 "categories": [3] 尝试使用 objectAtIndex 和数字值而不是字符串值 【参考方案1】:categories 对象是数组类型。所以你需要从数组中获取对象。类别对象不是NSString
类型。您应该使用以下代码:
[NSString stringWithFormat:@"%@", [self.side.article.categories objectAtIndex:0]];
【讨论】:
好的,我先转储了解,但这解决了我的问题:) 我做到了: if ([NSString stringWithFormat:@"%@", [self.side.article.categories objectAtIndex:0]] isEqual:@"4") ...【参考方案2】:正如@Unnati 所说,在您的 JSON 中,类别对象不是字符串。您的对象“文章”模型中的类别类型错误。
【讨论】:
我将类别类型更改为 NSNumber,现在我只得到 (lldb) EXT_BAD_ACCESS (Code=2 , Adress= 0x4) Categories 是一个 NSArray,[categories objectAtIndex[i]] 返回一个 NSInteger 值而不是 NSString,就像 @Huy Nghia 在评论中所说的那样。以上是关于Xcode -[__NSArrayM isEqualToString:]:无法识别的选择器发送到实例 0x7a1248d0 - 如何修复?的主要内容,如果未能解决你的问题,请参考以下文章
NSArrayM objectAtIndex:索引 6 超出范围
NSArrayM insertObject:atIndex:]: 对象不能为 nil
NSArrayM replaceObjectAtIndex:withObject: 对象不能为 nil'