gpt4 book ai didi

ios - 从 NSMutableArray 中获取数据

转载 作者:行者123 更新时间:2023-11-29 03:01:28 25 4
gpt4 key购买 nike

我只是从 IOS 开发开始,并为我的计算机科学类(class)创建一个 IOS 应用程序(最终项目),它使用 JSON 从远程 MySql 数据库获取数据。

这是我从 ViewDidLoad 调用的方法:

- (void)loadJSON
{
//-- Make URL request with server
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:@"http://vito-service.ru/studentsconnect/profile.php"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

//-- Get request and response though URL
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

//-- JSON Parsing
NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Result = %@",result);

}

NSLog 打印正确的数据:

Result = {
users = (
{
IdUser = 1;
facebook = "";
fullname = "";
instagram = "";
phonenumber = "";
username = moderas;
},
{
IdUser = 2;
facebook = "fb.com/petroffmichael";
fullname = "Michael Perov";
instagram = "@petroffmichael";
phonenumber = "(650)557-6168";
username = petroffmichael;
},
{
IdUser = 3;
facebook = "fb.com/testuser";
fullname = "Test User";
instagram = "@testuser";
phonenumber = "111 111-1111";
username = testuser;
}
);
}

但是我怎样才能将这些数据检索为字符串,然后将它们放入标签中呢?我尝试了很多创建数据对象的不同选项,我在堆栈溢出时发现了这些对象,但我总是收到“无法识别的选择器发送到实例”错误。我还尝试创建一个单独的 NSObject

@synthesize IdUser, username, fullname, phonenumber, facebook, instagram;

- (id) initWithUsername: (NSString *)uUsername andUserFullName: (NSString *)uFullname andUserPhonenumber: (NSString *)uPhonenumber andUserFacebook: (NSString *)uFacebook andUserInstagram: (NSString *)uInstagram andUserId: (NSString *)uId
{
self = [super init];
if (self) {
username = uUsername;
fullname = uFullname;
phonenumber = uPhonenumber;
facebook = uFacebook;
instagram = uInstagram;
IdUser = uId;
}

return self;
}

然后使用此方法检索数据:

-(void)retrieveData
{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

usersArray = [[NSMutableArray alloc] init];

for (int i=0; i<jsonArray.count; i++) {
NSString * uId = [[jsonArray objectAtIndex: i] objectForKey:@"IdUser"];
NSString * uUsername = [[jsonArray objectAtIndex: i] objectForKey:@"username"];
NSString * uFullname = [[jsonArray objectAtIndex: i] objectForKey:@"fullname"];
NSString * uPhonenumber = [[jsonArray objectAtIndex: i] objectForKey:@"phonenumber"];
NSString * uFacebook = [[jsonArray objectAtIndex: i] objectForKey:@"facebook"];
NSString * uInstagram = [[jsonArray objectAtIndex: i] objectForKey:@"instagram"];

[usersArray addObject:[[User alloc] initWithUsername:uUsername andUserFullName:uFullname andUserPhonenumber:uPhonenumber andUserFacebook:uFacebook andUserInstagram:uInstagram andUserId:uId]];
}

}

我有所有重要的#import,但我仍然遇到同样的错误:

2014-04-23 18:35:26.612 testimage[47155:70b] -[__NSCFDictionary objectAtIndex:]:     unrecognized selector sent to instance 0xa72a8b0
2014-04-23 18:35:26.813 testimage[47155:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa72a8b0'

任何人都可以帮助找到正确的解决方案吗?谢谢!

听了多条评论后,我更改了代码,但仍然收到错误..

我想我还是做错了什么

jsonArray 不再是数组:

@property (nonatomic, strong) NSDictionary *jsonArray;
@property (nonatomic, strong) NSMutableArray *usersArray;

然后是方法:

-(void)retrieveData
{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
usersArray = [[NSMutableArray alloc] init];

for (NSDictionary *dict in jsonArray)
{
NSString * uId = [dict objectForKey:@"IdUser"];
NSString * uUsername = [dict objectForKey:@"username"];
NSString * uFullname = [dict objectForKey:@"fullname"];
NSString * uPhonenumber = [dict objectForKey:@"phonenumber"];
NSString * uFacebook = [dict objectForKey:@"facebook"];
NSString * uInstagram = [dict objectForKey:@"instagram"];

[usersArray addObject:[[User alloc] initWithUsername:uUsername andUserFullName:uFullname andUserPhonenumber:uPhonenumber andUserFacebook:uFacebook andUserInstagram:uInstagram andUserId:uId]];
}

}

我仍然遇到同样的错误:

2014-04-23 20:44:43.971 testimage[48285:70b] -[__NSCFString objectForKey:]:  unrecognized selector sent to instance 0xa5547d0
2014-04-23 20:44:44.053 testimage[48285:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa5547d0'

最佳答案

仔细查看该输出。这不是你说的。你所拥有的不是一个数组——它是一个带有一个键的字典,@"users",它的值是一个数组。这就是它以 { users = … 开头时告诉您的内容。如果您需要数组,则需要从字典中检索它。

关于ios - 从 NSMutableArray 中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23258201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com