サーバーから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: 私の英語でごめんなさい!