如何在循环中将NSDictionary的值存储到NSMutableArray?

时间:2014-05-28 12:08:14

标签: ios objective-c sqlite nsmutablearray nsmutabledictionary

我从数据库中获取数据,我将获得数据库中最后一个数据。如何在nsmutable数组中存储字典值。

这是我的代码!!

  - (NSDictionary *) getPreparedAllRow
 {

NSMutableDictionary *dRow=[[NSMutableDictionary alloc]init];
NSMutableArray *data=[[NSMutableArray alloc]init];

int counter=0;
while(sqlite3_step(statment) == SQLITE_ROW)
{
    int Column_Count = sqlite3_column_count(statment);
    if(Column_Count >= 1)
    {

        for(int count =0 ; count < Column_Count ; count++)
        {
            [dRow setObject:[self columnValue:count] forKey:@(sqlite3_column_name(statment, count))];
            //dRow [ @(sqlite3_column_name(statment, count))] = [self columnValue:count];
        }
    }
    NSLog(@"%@",dRow);
    [data insertObject:dRow atIndex:counter];
    counter++;
    NSLog(@"%@",data);
}
NSLog(@"%@",data);
return dRow;
 }

当计数所有数据记录时9计数器变量。所有数据都用最后一个数据覆盖。

这是我的日志。

 (
    {
     "Id" = 1;
    "Name" = Priyank;
},
    {
     "Id" = 1;
    "Name" = Priyank;
},
    {
     "Id" = 1;
    "Name" = Priyank;
},
    {
     "Id" = 1;
    "Name" = Priyank;
},
    {
     "Id" = 1;
    "Name" = Priyank;
},
    {
     "Id" = 1;
    "Name" = Priyank;
},
    {
     "Id" = 1;
    "Name" = Priyank;
},
    {
     "Id" = 1;
    "Name" = Priyank;
},
    {
    "Id" = 1;
    "Name" = Priyank;
}

4 个答案:

答案 0 :(得分:0)

实际上返回类型应该是数组,并且不需要计数器变量,请尝试:

- (NSArray *)getPreparedAllRow
 {


NSMutableArray *data=[[NSMutableArray alloc]init];

while(sqlite3_step(statment) == SQLITE_ROW)
{
    NSMutableDictionary *dRow=[[NSMutableDictionary alloc]init];
    int Column_Count = sqlite3_column_count(statment);
    if(Column_Count >= 1)
    {

        for(int count =0 ; count < Column_Count ; count++)
        {
            [dRow setObject:[self columnValue:count] forKey:@(sqlite3_column_name(statment, count))];
            //dRow [ @(sqlite3_column_name(statment, count))] = [self columnValue:count];
        }
    }
    NSLog(@"%@",dRow);
    [data addObject:dRow];
    NSLog(@"%@",data);
}
NSLog(@"%@",data);
return data;
 }

答案 1 :(得分:0)

您需要在每次迭代时创建一个新的NSMutableDictionary * dRow:

- (NSDictionary *) getPreparedAllRow
 {

//NSMutableDictionary *dRow=[[NSMutableDictionary alloc]init];
NSMutableArray *data=[[NSMutableArray alloc]init];

int counter=0;
while(sqlite3_step(statment) == SQLITE_ROW)
{
    int Column_Count = sqlite3_column_count(statment);

    NSMutableDictionary *dRow=[[NSMutableDictionary alloc]init];

    if(Column_Count >= 1)
    {

        for(int count =0 ; count < Column_Count ; count++)
        {
            [dRow setObject:[self columnValue:count] forKey:@(sqlite3_column_name(statment, count))];
            //dRow [ @(sqlite3_column_name(statment, count))] = [self columnValue:count];
        }
    }
    NSLog(@"%@",dRow);
    [data insertObject:dRow atIndex:counter];
    counter++;
    NSLog(@"%@",data);
}
NSLog(@"%@",data);
return dRow;
 }

我认为你想要返回NSMutableArray * ??也许你也有错误...

答案 2 :(得分:0)

如果您的NSDictionary中包含一些值,请尝试使用此语句。这将自动将特定键值带入该数组。

arrayName = [dictionaryName valueForKey:@"keyName"];

或者你可以试试这个,

[arrayName addObject:[dictionaryName objectForKey:@"keyName"];

答案 3 :(得分:0)

首先创建一个名为

的NSObject类

/ ** #import“StudentDTO.h”**** /

 @interface StudentDTO : NSObject
    {
        int ID;
        NSString *stu_name;
    }
    @property (nonatomic, readwrite) int ID;
    @property (nonatomic, retain) NSString *stu_name;

@end

/ ** #import“StudentDTO.m”**** /

@implementation StudentDTO

@synthesize ID,stu_name;

@end

////现在在您的代码中导入#import“StudentDTO.h”

-(NSDictionary *) getPreparedAllRow

 {

NSMutableDictionary *dRow=[[NSMutableDictionary alloc]init];
NSMutableArray *data=[[NSMutableArray alloc]init];
int counter=0;
while(sqlite3_step(statment) == SQLITE_ROW)
{
   int Column_Count = sqlite3_column_count(statment);
    if(Column_Count >= 1)
    {

        for(int count =0 ; count < Column_Count ; count++)
        {
            [dRow setObject:[self columnValue:count] forKey:@(sqlite3_column_name(statment, count))];
            //dRow [ @(sqlite3_column_name(statment, count))] = [self columnValue:count];
        }
    }
    NSLog(@"%@",dRow);
    StudentDTO *studto=[[StudentDTO alloc]init];
    studto.ID=[dRow objectforkey:@"Id"];
    studto.stu_name=[dRow objectforkey:@"Name"];[data addobject:studto];
    counter++;
    NSLog(@"%@",data);}NSLog(@"%@",data);return dRow;

 }