0

サーバーからJSON形式で受信したユーザーのリストを持つアプリケーションをプログラミングしています。その後、その JSON から各ユーザーのデータを抽出し、prod_id、userName、userThumb、createTime という 4 つのフィールド (これが適切な言葉かどうかはわかりません) を持つ UsersController というオブジェクトを作成しました。私のViewControllerには、UsersControllerのオブジェクトと、すべてのユーザーを格納するための配列があります。クラス UsersController のコードは次のとおりです。

//UsersController.h
#import <UIKit/UIKit.h>

@interface UsersController : NSObject{
    NSInteger *prof_id;
    NSString *createTime;
    NSString *fullName;
    NSString *thumb;
}

@property (nonatomic) NSInteger *prof_id;
@property (nonatomic, retain) IBOutlet NSString *createTime;
@property (nonatomic, retain) IBOutlet NSString *fullName;
@property (nonatomic, retain) IBOutlet NSString *thumb;


@end

//UsersController.m
#import "UsersController.h"

@implementation UsersController

@synthesize prof_id;
@synthesize fullName;
@synthesize createTime;
@synthesize thumb;

@end

ViewController.m には次のコードがあります。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


 NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];

NSArray *array_webdata=[[NSArray array] init];

NSString *searchStatus = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];    

array_webdata = [parsedata objectWithString:searchStatus error:nil];

//String with all data of each user
NSString *usersList = [array_webdata valueForKey:@"results"];    
NSLog(@"\n usersList =\n %@ \n", usersList);

//extract data from the JSON data received from the server
NSArray *userName = [usersList valueForKey:@"fullname"];    
NSArray *userID = [usersList valueForKey:@"prof_id"];
NSArray *userThumb = [usersList valueForKey:@"thumb"];
NSArray *userCreateTime = [usersList valueForKey:@"createTime"];    

NSMutableArray *arrayUsuarios = [[NSMutableArray alloc] initWithCapacity: [userID count]];
int i;
UsersController *usuarioSimple;
UsersController *usuarioAuxiliar;
for (int i=0; i<[userName count]; i++) {
    usuarioSimple = [[UsersController alloc] init];
    usuarioAuxiliar= [[UsersController alloc] init];

    //I store in the object called usuario the extracted data from JSON userName, userID, userThumb y userCreateTime
    usuarioSimple.prof_id = [userID objectAtIndex:i];
    usuarioSimple.fullName = [userName objectAtIndex:i];
    usuarioSimple.thumb = [userThumb objectAtIndex:i];
    usuarioSimple.createTime = [userCreateTime objectAtIndex:i];

    [arrayUsuarios addObject:usuarioSimple];

    usuarioAuxiliar = [arrayUsuarios objectAtIndex:i];

    NSLog(@"pruebaConsulta.prof_id[%@]:    %@",i, usuarioAuxiliar.prof_id);
    NSLog(@"pruebaConsulta.fullName[%@]:   %@",i, usuarioAuxiliar.fullName);
    NSLog(@"pruebaConsulta.thumb[%@]:      %@",i, usuarioAuxiliar.thumb);
    NSLog(@"pruebaConsulta.createTime[%@]: %@\n",i, usuarioAuxiliar.createTime);
    [usuarioSimple release];
    [usuarioAuxiliar release];

}

[searchStatus release];
[connection release];
[webData release];
[pool drain];

}

ここで、2 つの問題があります。1 つ目は、 bucle for で使用されるiの宣言です。理由はわかりませんが、実行すると、iの値を表示する必要があるときに(null) が表示されます。

2 つ目の問題は、[userID カウント]、[userName カウント]、[userThumb カウント]、または [userCreateTime カウント] を使用する場合です。次の行を書くと、この指示は機能しません。

NSLog(@"userid count: %@", [userID count]);

実行がクラッシュし、EXC_BAD_ACCESS と表示されます。多くの可能な解決策を証明しましたが、常に失敗します。助けが必要です。ありがとう。

ps: 私の英語でごめんなさい!

4

4 に答える 4

2

書式設定された文字列に整数を使用する%iか、整数を含める必要があります。文字列フォーマット指定子%dを参照してください。

于 2012-05-10T14:24:25.713 に答える
1

NSIntegerはオブジェクトではなく、型であり、prof_idをNSInteger *として宣言します。これは、オブジェクトではなく整数へのポインターを意味します。

NSIntegerまたはそのポインタはオブジェクトではないため、メッセージを渡すことはできません。また、配列や辞書に追加することもできません。数値(整数など)をカプセル化するNSNumberを使用するとします。次に、NSNumberをオブジェクトとして渡すことができますが、integerValueメッセージを送信して整数値を取得します。

NSInteger myInt = myNumber.integerValue;
于 2012-05-10T14:36:18.483 に答える
1

いくつかのこと:

1.これを行う場合: NSLog(@"userid count: %@", [userID count]); %@ の代わりに %d を使用してください。

  1. これを行うと: [arrayUsuarios addObject:usuarioSimple]; //最後のインデックスにオブジェクトを追加します。

    usuarioAuxiliar = [arrayUsuarios objectAtIndex:i]; //ここで、インデックス i のオブジェクトにアクセスします。i> [arrayUsuarios count] の場合、オブジェクトはクラッシュします。

この状況では、次のメソッドを呼び出します: [arrayUsuarios lastObject];

于 2012-05-10T14:45:43.967 に答える