Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从SQL数据库读入NSArray_Sql_Objective C_Nsarray - Fatal编程技术网

从SQL数据库读入NSArray

从SQL数据库读入NSArray,sql,objective-c,nsarray,Sql,Objective C,Nsarray,我有一台iPad,可以从SQL数据库读取数据。下面的代码工作正常,从每条记录中检索2个字段,并将它们读入NSArray 我现在需要阅读其中的5个字段,我忍不住认为有更好的方法,而不是通过php运行5个单独的查询(getinfo.php文件,其中choice参数设置为选择不同的字段) 有没有什么更好的方法 NSString *strURLClass = [NSString stringWithFormat:@"%@%@", @"http://wwwaddress/getinfo.php?choic

我有一台iPad,可以从
SQL
数据库读取数据。下面的代码工作正常,从每条记录中检索2个字段,并将它们读入
NSArray

我现在需要阅读其中的5个字段,我忍不住认为有更好的方法,而不是通过php运行5个单独的查询(getinfo.php文件,其中choice参数设置为选择不同的字段)

有没有什么更好的方法

NSString *strURLClass = [NSString stringWithFormat:@"%@%@", @"http://wwwaddress/getinfo.php?choice=1&schoolname=",obsSchoolName];
NSArray *observationsArrayClass = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsClass = [observationsArrayClass objectEnumerator];

NSString *strURLDate = [NSString stringWithFormat:@"%@%@", @"http://wwwaddress/getinfo.php?choice=5&schoolname=",obsSchoolName];
NSArray *observationsArrayDate = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLDate]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsDate = [observationsArrayDate objectEnumerator];

id className, dateOfObs;

while (className = [enumForObsClass nextObject])
{
    dateOfObs = [enumForObsDate nextObject];
    [observationListFromSQL addObject:[NSDictionary dictionaryWithObjectsAndKeys:className, @"obsClass", dateOfObs, @"obsDate",nil]];
}

是的,通过将语句“折叠”成循环并使用可变字典,您可以用更少的代码实现这一点:

// Add other items that you wish to retrieve to the two arrays below:
NSArray *keys = @[@"obsClass", @"obsDate"]; // Key in the dictionary
NSArray *choices = @[@1, @5];               // Choice in the URL string
NSMutableArray *res = [NSMutableArray array];
NSMutableArray *observationListFromSQL = [NSMutableArray array];
for (int i = 0 ; i != keys.count ; i++) {
    NSNumber *choice = choices[i];
    NSString *strURLClass = [NSString stringWithFormat:@"http://wwwaddress/getinfo.php?choice=%@&schoolname=%@", choice, obsSchoolName];
    NSArray *observationsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
    NSEnumerator *objEnum = [observationsArrayClass objectEnumerator];
    NSString *key = keys[i];
    NSMutableDictionary *dict;
    if (res.count < i) {
        dict = res[i];
    } else {
        dict = [NSMutableDictionary dictionary];
        [res addObject:dict];
    }
    id item;
    while (item = [objEnum nextObject]) {
        [res setObject:item forKey:key];
    }
}
//将要检索的其他项添加到以下两个数组中:
NSArray*键=@[@“obsClass”,“obsDate”];//查字典
NSArray*choices=@[@1,@5];//URL字符串中的选项
NSMutableArray*res=[NSMutableArray];
NSMutableArray*observationListFromSQL=[NSMutableArray];
for(int i=0;i!=keys.count;i++){
NSNumber*choice=choices[i];
NSString*strURLClass=[NSString stringWithFormat:@”http://wwwaddress/getinfo.php?choice=%@&学校名称=%@“,选项,学校名称];
NSArray*observationsArray=[[NSMutableArray alloc]initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
N分子*对象=[observationsArrayClass对象枚举器];
NSString*key=key[i];
NSMutableDictionary*dict;
如果(分辨率计数
是的,您可以通过将语句“折叠”成一个循环,并使用可变字典,用更少的代码来实现这一点:

// Add other items that you wish to retrieve to the two arrays below:
NSArray *keys = @[@"obsClass", @"obsDate"]; // Key in the dictionary
NSArray *choices = @[@1, @5];               // Choice in the URL string
NSMutableArray *res = [NSMutableArray array];
NSMutableArray *observationListFromSQL = [NSMutableArray array];
for (int i = 0 ; i != keys.count ; i++) {
    NSNumber *choice = choices[i];
    NSString *strURLClass = [NSString stringWithFormat:@"http://wwwaddress/getinfo.php?choice=%@&schoolname=%@", choice, obsSchoolName];
    NSArray *observationsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
    NSEnumerator *objEnum = [observationsArrayClass objectEnumerator];
    NSString *key = keys[i];
    NSMutableDictionary *dict;
    if (res.count < i) {
        dict = res[i];
    } else {
        dict = [NSMutableDictionary dictionary];
        [res addObject:dict];
    }
    id item;
    while (item = [objEnum nextObject]) {
        [res setObject:item forKey:key];
    }
}
//将要检索的其他项添加到以下两个数组中:
NSArray*键=@[@“obsClass”,“obsDate”];//查字典
NSArray*choices=@[@1,@5];//URL字符串中的选项
NSMutableArray*res=[NSMutableArray];
NSMutableArray*observationListFromSQL=[NSMutableArray];
for(int i=0;i!=keys.count;i++){
NSNumber*choice=choices[i];
NSString*strURLClass=[NSString stringWithFormat:@”http://wwwaddress/getinfo.php?choice=%@&学校名称=%@“,选项,学校名称];
NSArray*observationsArray=[[NSMutableArray alloc]initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
N分子*对象=[observationsArrayClass对象枚举器];
NSString*key=key[i];
NSMutableDictionary*dict;
如果(分辨率计数
谢谢。这很简洁,意味着代码更少,但如果我想要5个字段,它仍然会运行php查询5次。数据不能从php文件的单个调用传回吗?@RichardGriffiths,这取决于提供数据的服务:如果它为您提供了一种在单个调用中提供多个选项的方法,并为您定义了一种解码返回数据的方法,那么答案是“是”。此外,如果有一种方法不提供任何选择,请返回所有字段,然后选择所需的五个字段,您可能会以接收更多数据为代价节省往返时间(通常是一种非常有益的折衷)。否则,您将需要执行多个调用。Brillant。非常有用,谢谢。尝试你的代码,我得到dict=res[I]上的错误;和res[i]=dict;“在类型为“NSMutableDictionary*”的对象上找不到读取数组元素的预期方法。”。行nsemulator*objEnum=[observationsArrayClass objectEnumerator];仅仅是“observationsArray”?@RichardGriffiths这应该是一个数组-我修正了这个问题,并将
addObject
放在
else
分支中。谢谢。这很简洁,意味着代码更少,但如果我想要5个字段,它仍然会运行php查询5次。数据不能从php文件的单个调用传回吗?@RichardGriffiths,这取决于提供数据的服务:如果它为您提供了一种在单个调用中提供多个选项的方法,并为您定义了一种解码返回数据的方法,那么答案是“是”。此外,如果有一种方法不提供任何选择,请返回所有字段,然后选择所需的五个字段,您可能会以接收更多数据为代价节省往返时间(通常是一种非常有益的折衷)。否则,您将需要执行多个调用。Brillant。非常有用,谢谢。尝试你的代码,我得到dict=res[I]上的错误;和res[i]=dict;“在类型为“NSMutableDictionary*”的对象上找不到读取数组元素的预期方法。”。行nsemulator*objEnum=[observationsArrayClass objectEnumerator];仅仅是“observationsArray”?@RichardGriffiths这应该是一个数组-我修正了这个问题,并将
addObject
放在
else
分支中。