草庐IT

ios - Objective-C NSArray、 block 、STAsertEquals

coder 2024-01-26 原文

我正在开发一个应用程序,列出一项运动的当前现场比赛。有关实时游戏的信息是使用 REST 从远程源获取的。第一个请求给出了一个实时游戏列表及其 ID 和相应的竞技场 ID。然后我必须从他们的 ID 中获取竞技场名称。当一切都完成后,我发回一个包含实时游戏列表的 NSArray。

在测试通过 block 传递 NSArray 的解析方法时,我在 SenTestCase 中发现了一个奇怪的行为。在我的测试中,我能够执行 [myArray count] 并将其结果显示在 NSLog 中,但是当我执行 STAsertEquals([myArray count], 1, @"Error description ")带有 EXC_BAD_ACCESS 的测试崩溃。

这里是我的代码,已减至最少并删除了所有网络方面的内容:

#import <SenTestingKit/SenTestingKit.h>


@interface LiveGameTests : SenTestCase
@end

@interface LiveGame : NSObject
@property (nonatomic) id gameID;
@property (strong, nonatomic) NSString *arenaName;
@end

@implementation LiveGame

// Create a live game object from a dictionary (this dictionary is the response of a first request to a remote server)
+(LiveGame *)gameWithData:(NSDictionary *)dict
{
    LiveGame *liveGame = [[LiveGame alloc] init];
    liveGame.gameID = dict[@"game_id"];    
    return liveGame;
}

// Complete a live game data with the element of a dictionary (those data are actualy fetched from a remote server in a different request.)
+(void)fetchRemainingData:(NSArray *)liveGameList completion:(void(^)(void))completion
{
    LiveGame *liveGame = liveGameList[0];
    liveGame.arenaName = @"arenaName";
    completion();
}

// Parse a NSArray of NSDictionaries representing live game
+(void)parseArrayOfDictionary:(NSArray *)arrayToParse success:(void(^)(NSArray *liveGameList))success
{
    NSMutableArray *liveGameList = [NSMutableArray arrayWithCapacity:[arrayToParse count]];

    // Iterate on all the NSDictionary of the NSArray and create live game from each NSDictionary 
    [arrayToParse enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop)
     {
         liveGameList[idx] = [LiveGame gameWithData:obj];
     }];

    [LiveGame fetchRemainingData:liveGameList completion:^
     {
         success(liveGameList);
     }];
}
@end

@implementation LiveGameTests

-(void)testParseArrayOfDictionary
{
    [LiveGame parseArrayOfDictionary: @[@{@"game_id": @1}]
        success:^(NSArray *liveGameList)
        {
            // This line work fine and print: 2013-03-08 13:39:35.288 ShotClock[55177:c07] liveGameList count = 1
            NSLog(@"liveGameList count = %d",[liveGameList count]);

            // This crash the test on a EXC_BAD_ACCESS. What's wrong?
            STAssertEquals([liveGameList count], 1, @"The list of game must contain one unique live game but contain %@",[liveGameList count]);
        }];
}
@end

NSLog(@"liveGameList 计数 = %d",[liveGameList 计数]); => 此行工作正常并打印:2013-03-08 13:39:35.288 ShotClock[55177:c07] liveGameList count = 1

STAsertEquals([liveGameList count], 1, @"游戏列表必须包含一个唯一的现场游戏但包含 %@",[liveGameList count]); => 这会使 EXC_BAD_ACCESS 上的测试崩溃。怎么了?

最佳答案

您的应用程序崩溃是因为

  STAssertEquals([liveGameList count], 1,  @"The list of game must contain one unique live game but contain %@",[liveGameList count])

尝试将 %@ 格式化程序应用于 [liveGameList count] 的结果。但是 %@ 需要一个 Objective C 对象,其中 [liveGameList count] 返回标量“1”。运行时将该标量转换为指向 0x00000001 的指针,并尝试使用它在那里找到的“对象”。但这不是有效指针,因此运行时会引发无效地址异常。

关于ios - Objective-C NSArray、 block 、STAsertEquals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15294556/

有关ios - Objective-C NSArray、 block 、STAsertEquals的更多相关文章

  1. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  2. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. ruby - 在匿名 block 中产生 - 2

    我没有理解以下行为(另请参阅inthisSOthread):defdef_testputs'def_test.in'yieldifblock_given?puts'def_test.out'enddef_testdoputs'def_testok'endblock_test=procdo|&block|puts'block_test.in'block.callifblockputs'block_test.out'endblock_test.calldoputs'block_test'endproc_test=procdoputs'proc_test.in'yieldifblock_gi

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby - Ruby 中的单 block AES 解密 - 2

    我需要尝试一些AES片段。我有一些密文c和一个keyk。密文已使用AES-CBC加密,并在前面加上IV。不存在填充,纯文本的长度是16的倍数。所以我这样做:aes=OpenSSL::Cipher::Cipher.new("AES-128-CCB")aes.decryptaes.key=kaes.iv=c[0..15]aes.update(c[16..63])+aes.final它工作得很好。现在我需要手动执行CBC模式,所以我需要单个block的“普通”AES解密。我正在尝试这个:aes=OpenSSL::Cipher::Cipher.new("AES-128-ECB")aes.dec

  8. objective-c - 在设置 Cocoa Pods 和安装 Ruby 更新时出错 - 2

    我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U

  9. ruby-on-rails - 无法在 Rails 助手中捕获 block 的输出 - 2

    我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e

  10. ruby - 具有两个参数的 block - 2

    我从用户Hirolau那里找到了这段代码:defsum_to_n?(a,n)a.combination(2).find{|x,y|x+y==n}enda=[1,2,3,4,5]sum_to_n?(a,9)#=>[4,5]sum_to_n?(a,11)#=>nil我如何知道何时可以将两个参数发送到预定义方法(如find)?我不清楚,因为有时它不起作用。这是重新定义的东西吗? 最佳答案 如果您查看Enumerable#find的文档,您会发现它只接受一个block参数。您可以将它发送两次的原因是因为Ruby可以方便地让您根据它的“并行赋

随机推荐