代码之家  ›  专栏  ›  技术社区  ›  Tejaswi Yerukalapudi

appdelegate中的NSMutableArray-EXEC\u BAD\u访问

  •  0
  • Tejaswi Yerukalapudi  · 技术社区  · 14 年前

    .h文件包含声明

    {
    NSMUTABLEARRY*输入;
    }

    @属性(非原子,保留)NSMutableArray*trnEntered

    trnEntered=[[NSMutableArray alloc]init];
    NSLog(@“%@”,[trnEntered count]);//打印空。
    [输入的addObject:@“1”];

    不知道我哪里出错了。看起来很直截了当。

    谢谢你的帮助,
    特贾。

    4 回复  |  直到 4 年前
        1
  •  3
  •   Lyndsey Ferguson    11 年前

    您的代码似乎有问题:

    NSLog(@"%@",[trnEntered count]); // prints null.
    [trnEntered addObject:@"1"]; 
    NSLog(@"%@",[trnEntered count]); // exec bad access.
    

    对NSLog的两个调用都试图打印 NSUInteger 作为客观对象。这会引起一个问题。你应该使用 NSLog(@"%d", [trnEntered count]);

    我建议你多读一些关于 format specifiers ,有很多有用的信息。

    更多信息:第二 NSlog 正在打印 description 代码声明的NSObject位于内存位置0x00000001。

        2
  •  0
  •   Fabien    14 年前

    trnEntered = [[NSMutableArray alloc] initWithCapacity:1];
    
        3
  •  0
  •   futureelite7 Adam Rosenfield    14 年前
    trnEntered = [[NSMutableArray alloc] init];
    NSLog(@"%@",[trnEntered count]); // prints null.
    [trnEntered addObject:@"1"];
    NSLog(@"%@",[trnEntered count]); // exec bad access.
    

    原因是您使用的格式化程序字符串错误。您试图使用整数作为字符串指针,这当然会导致分段错误(或者用苹果的术语来说是EXC\u BAD\u ACCESS)。使用%d打印一个整数,[trnEntered count]返回该整数。

        4
  •  0
  •   theAlse    12 年前

    NSLog 默认情况下,以字符串格式打印ans,因此如果要打印整数值,则必须使用 %d