【问题标题】:NSInvalidArgumentException [__NSCFConstantString objectForKeyedSubscript:]NSInvalidArgumentException [__NSCFConstantString objectForKeyedSubscript:]
【发布时间】:2014-10-26 13:24:03
【问题描述】:

我正在构建一个 XCTestCase 来检查某个初始化程序是否工作。我已经逐行检查了代码,确保为源字典中的每个键(在类 init 中使用)传递了正确的值。但我不断收到此错误:NSInvalidArgumentException [__NSCFConstantString objectForKeyedSubscript:]

这是测试代码:

NSDictionary *comments = @{@"data" : @{@"id": @"8675309",
                                @"text" : @"Sample Comment"}};

NSDictionary *user = @{@"id": @"8675309",
                      @"username" : @"d'oh",
                      @"full_name" : @"Homer Simpson",
                      @"profile_picture" : @"http://www.example.com/example.jpg"};

NSDictionary *sourceDictionary = @{@"id" : @"902810",
                                 @"user" : user,
                                 @"caption" : @{@"text" : @"a caption"},
                                 @"images" : @{@"standard_resolution" : @{@"url" : @"www.example.com.br/media"}},
                                 @"comments" : comments,
                                 @"likes" : @{@"count" : @"12342"},
                                 @"user_had_liked" : @"1"};

Media *testMedia = [[Media alloc] initWithDictionary:sourceDictionary];

这是我正在测试初始化​​程序的类

-(instancetype)initWithDictionary:(NSDictionary *)mediaDictionary {

  self = [super init];

  if (self) {
    self.mediaDictionary = mediaDictionary;

    self.idNumber = mediaDictionary[@"id"];

    self.user = [[User alloc] initWithDictionary:mediaDictionary[@"user"]];

    [self tryToFetchCaption];

    [self tryToFetchImage];

    [self tryToFetchComments];

    [self setLikesCounter];

    BOOL userHasLiked = [mediaDictionary[@"user_had_liked"] boolValue];
    self.likeState = userHasLiked ? LikeStateLiked : LikeStateNotLiked;
  }

  return self;
}

-(void)setLikesCounter {

  NSInteger likeCount = [self.mediaDictionary[@"likes"][@"count"] intValue];
  self.likesCounter = @(likeCount);
}

-(void)tryToFetchCaption {

  NSDictionary *captionDictionary = self.mediaDictionary[@"caption"];

  if ([captionDictionary isKindOfClass:[NSDictionary class]]) {
    self.caption = captionDictionary[@"text"];
  } else {
    self.caption = @"";
  }
}

-(void)tryToFetchImage {

  NSString *standardResolutionImageURLString = self.mediaDictionary[@"images"][@"standard_resolution"][@"url"];

  NSURL *standardResolutionImageURL = [NSURL URLWithString:standardResolutionImageURLString];

  if (standardResolutionImageURL) {
    self.mediaURL = standardResolutionImageURL;
    self.downloadState = MediaDownloadStateNeedsImage;
  } else {
    self.downloadState = MediaDownloadStateNonRecoverableError;
  }
}

-(void)tryToFetchComments {

  NSMutableArray *commentsArray = [NSMutableArray new];

  for (NSDictionary *commentDictionary in self.mediaDictionary[@"comments"][@"data"]) {
    Comment *tempComment = [[Comment alloc] initWithDictionary:commentDictionary];
    [commentsArray addObject:tempComment];
  }

  self.comments = commentsArray;
}

日志:

2014-10-26 11:39:08.295 Blocstagram[2128:21707]-[__NSCFConstantString objectForKeyedSubscript:]:无法识别的选择器发送到实例 0xc9d2074 MediaTests.m:47:错误:-[MediaTests testThatInitializationWorks]:失败:捕获“NSInvalidArgumentException”,“-[__NSCFConstantString objectForKeyedSubscript:]:无法识别的选择器发送到实例 0xc9d2074” ( 0 核心基础 0x0119a946 __exceptionPreprocess + 182 1 libobjc.A.dylib 0x00ad4a97 objc_exception_throw + 44 2核心基础0x011a25c5-[NSObject(NSObject)不识别选择器:]+277 3 核心基础 0x010eb3e7 ___转发___ + 1047 4 核心基础 0x010eafae_CF_forwarding_prep_0 + 14 5 Blocstagram 0x00060453 -[评论 initWithDictionary:] + 179 6 Blocstagram 0x00068ce4 -[媒体 tryToFetchComments] + 596 7 Blocstagram 0x00067db7 -[Media initWithDictionary:] + 519 8 BlocstagramTests 0x0c9cf74a -[MediaTests testThatInitializationWorks] + 1562 9 核心基础 0x0107d76d __invoking___ + 29 10 CoreFoundation 0x0107d618 -[NSInvocation 调用] + 360 11 XCTest 0x2010897b -[XCTestCase 调用测试] + 320 12 XCTest 0x20108bb9 -[XCTestCase 执行测试:] + 184 13 XCTest 0x20114162 -[XCTest 运行] + 314 14 XCTest 0x20107598 -[XCTestSuite 执行测试:] + 406 15 XCTest 0x20114162 -[XCTest 运行] + 314 16 XCTest 0x20107598 -[XCTestSuite 执行测试:] + 406 17 XCTest 0x20114162 -[XCTest 运行] + 314 18 XCTest 0x20107598 -[XCTestSuite 执行测试:] + 406 19 XCTest 0x20114162 -[XCTest 运行] + 314 20 XCTest 0x20103de2 __25-[XCTestDriver _runSuite]_block_invoke + 61 21 XCTest 0x20110c82 -[XCTestObservationCenter _observeTestExecutionForBlock:] + 184 22 XCTest 0x20103d06 -[XCTestDriver _runSuite] + 285 23 XCTest 0x20104951-[XCTestDriver_checkForTestManager] + 272 24 XCTest 0x20104c6b -[XCTestDriver runTestSuite:completionHandler:] + 378 25 XCTest 0x2011775c +[XCTestProbe runTests:] + 216 26 基础 0x006d7b57 __NSFireDelayedPerform + 423 27 核心基础 0x010f48d6 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22 28 核心基础 0x010f425d __CFRunLoopDoTimer + 1309 29 核心基础 0x010b36ba __CFRunLoopRun + 2090 30 核心基础 0x010b2bcb CFRunLoopRunSpecific + 443 31 核心基础 0x010b29fb CFRunLoopRunInMode + 123 32 图形服务 0x058ff24f GSEventRunModal + 192 33 图形服务 0x058ff08c GSEventRun + 104 34 UIKit 0x019f68b6 UIApplicationMain + 1526 35 Blocstagram 0x0008a32d 主要 + 141 36 libdyld.dylib 0x02deaac9 开始 + 1 ) 测试用例“-[MediaTests testThatInitializationWorks]”失败(0.039 秒)。 测试套件“MediaTests”在 2014-10-26 13:39:08 +0000 失败。 执行 1 次测试,1 次失败(1 次意外)在 0.039 (0.040) 秒内

【问题讨论】:

  • 哪个语句导致了异常?
  • 你能发布完整的堆栈跟踪吗?
  • @Zaph 当我尝试初始化 testMedia 时。
  • @AaronBrager 发布了它
  • testMedia 中的哪个方法导致了问题?作为最后的手段,注释掉 init 方法中的调用,直到不发生异常。

标签: objective-c xctest


【解决方案1】:

要找出 testMedia 中的哪个方法导致了问题,将 init 方法中的调用注释掉,直到不发生异常为止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多