0

我正在编写一个应用程序,其中我有一个从服务器收到的 JSON 格式的用户列表。后来,我从该 JSON 中提取每个用户的数据,并创建了一个名为 UsersController 的对象,该对象具有四个字段(我不知道这是否是适当的词),称为 prod_id、userName、userThumb 和 createTime。在我的 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];

}

在这里我有两个问题。第一个是在 bucle for 中使用的i的声明。我不知道为什么,但是当我执行时,当它必须显示i的值时,显示 (null)。

第二个问题是当我使用 [userID count]、[userName count]、[userThumb count] 或 [userCreateTime count] 时。这个指令不起作用,因为如果我写这行:

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

执行崩溃并说 EXC_BAD_ACCESS。我证明了很多可能的解决方案,但总是失败,我需要你的帮助。谢谢。

ps:对不起我的英语!

4

4 回答 4

2

您必须在格式化字符串中使用%i%d包含整数。请参阅字符串格式说明符

于 2012-05-10T14:24:25.713 回答
1

count 将返回一个原始整数。

%@ 用于打印字符串或任何对象的字符串描述。

正如 DrummerB 所说,将 %i 或 %d 用于整数。

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

你得到 EXC_BAD_ACCESS 因为 NSLog 认为你已经传递了一个指向一个对象的指针,这对你来说是一个错误的访问。

于 2012-05-10T14:29:12.637 回答
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 回答