-3

以前は、サーバーからの JSON データがありました。次のようにフォーマットされました。

(
        {
        comment = 1;
        "comment_id" = 41;
        "commenter_id" = 1;
        date = "2013-04-02";
        "first_name" = Alex;
        "plan_id" = 27;
        privacy = 0;
        "solution_part" = 1;
    }
)

そして、私はそれを次のように解析しました:

-(void)loadTitleStrings
{
    theArray = [NSMutableArray array];

    if(!standardUserDefaults)
        standardUserDefaults = [NSUserDefaults standardUserDefaults];

    NSLog(@"Arrayyy: %@", items_array);

    NSString *is_private = [standardUserDefaults objectForKey:@"is_private"];
    NSMutableArray *titleStrings = [NSMutableArray array];
    for(NSDictionary *dictionary in items_array)
    {
        //NOT SURE WHAT THIS IS FOR, SEEMS WEIRD
        NSString *tcid = [dictionary objectForKey:@"comment_id"];
        [theArray addObject:tcid];
        //NSLog(@"Arrayyy: %@", theArray);

        NSString *string;
        if(!is_private || [is_private isEqualToString:@"0"])
        {
            string = [NSString stringWithFormat:@"%@: %@", [dictionary objectForKey:@"first_name"], [dictionary objectForKey:@"comment"]];
        }
        else
        {
            string = [NSString stringWithFormat:@"%@", [dictionary objectForKey:@"comment"]];
        }

        [titleStrings addObject:string];
    }

    cellTitleArray = titleStrings;
}

しかし、サーバーに変更があり、データは次のような形式になります。

{
    data =     (
                {
            comment = 1;
            "comment_id" = 41;
            "commenter_id" = 1;
            date = "2013-04-02";
            "first_name" = Alex;
            "plan_id" = 27;
            privacy = 0;
            "solution_part" = 1;
        },
                {
            comment = 2;
            "comment_id" = 42;
            "commenter_id" = 1;
            date = "2013-04-02";
            "first_name" = Alex;
            "plan_id" = 27;
            privacy = 0;
            "solution_part" = 1;
        }
    );

そして、それを解析する元のコードがクラッシュします。形式の違いが何であるかは正確にはわかりません。新しいデータ形式で動作するように元のコードを変更するにはどうすればよいですか?

これは私が得るエラーです:

2013-04-02 12:31:28.798 Funding[70605:11303] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x72b9a10
2013-04-02 12:31:28.800 Funding[70605:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x72b9a10'
*** First throw call stack:
(0x1dd5012 0x126ce7e 0x1e604bd 0x1dc4bbc 0x1dc494e 0x14a52 0x1695b 0x17cb731 0x17da014 0x17ca7d5 0x1d7baf5 0x1d7af44 0x1d7ae1b 0x21687e3 0x2168668 0x1b0ffc 0x26dd 0x2605)
libc++abi.dylib: terminate called throwing an exception

ありがとう、アレックス

4

1 に答える 1

2

これは私がそれを行う方法です..

-(void)loadTitleStrings
{
    theArray = [NSMutableArray array];

    if(!standardUserDefaults)
        standardUserDefaults = [NSUserDefaults standardUserDefaults];

    NSLog(@"Arrayyy: %@", items_array);

    NSString *is_private = [standardUserDefaults objectForKey:@"is_private"];
    NSMutableArray *titleStrings = [NSMutableArray array];
    for(NSDictionary *dictionary in items_array)
    {
        NSArray *comments = [dictionary objectForKey:@"data"];
        for (NSDictionary * comment in comments)
        {
          NSString *tcid = [comment objectForKey:@"comment_id"];
           [theArray addObject:tcid];
           //NSLog(@"Arrayyy: %@", theArray);

          NSString *string;
          if(!is_private || [is_private isEqualToString:@"0"])
          {
              string = [NSString stringWithFormat:@"%@: %@", [comment objectForKey:@"first_name"], [dictionary objectForKey:@"comment"]];
        }
          else
        {
              string = [NSString stringWithFormat:@"%@", [comment objectForKey:@"comment"]];
        }

           [titleStrings addObject:string];
    }

    cellTitleArray = titleStrings;
}

基本的に、データ ディクショナリを取得し、データ ディクショナリ内でコメントごとに繰り返します...

これが役に立てば幸いです...完全なストーリーがわからないため、状況に応じて変更する必要があるかもしれません...

于 2013-04-02T16:38:21.447 に答える