在我展示我的代码之前,请耐心等待,我是stackoverflow和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(非原子,保留)NSString *类别;到@property(非原子,保留)NSNumber *类别;
解决方案是:
if ([NSString stringWithFormat:@"%@", [self.side.article.categories objectAtIndex:0]] isEqual:@"4") {...
答案 0 :(得分:2)
categories对象是数组类型。所以你需要从数组中获取对象。 categories对象不是NSString
类型。您应该使用代码:
[NSString stringWithFormat:@"%@", [self.side.article.categories objectAtIndex:0]];
答案 1 :(得分:0)
正如@Unnati所说,在你的JSON中,categories对象不是一个字符串。您的对象中的类别类型错误&#34;文章&#34;模型。