【问题标题】:How to store Value of NSDictionary to NSMutableArray in loop?如何将 NSDictionary 的值循环存储到 NSMutableArray?
【发布时间】:2014-07-17 15:38:38
【问题描述】:

我正在从数据库中获取数据,我将获得数据库中最后的相同数据。如何将字典值存储在 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;
}

)

【问题讨论】:

  • 您只创建了一本字典,因此您不断地一次又一次地存储它。 (了解您仅将 指针 存储到数组中的字典。字典不会“复制”到数组中。)

标签: ios objective-c sqlite nsmutablearray nsmutabledictionary


【解决方案1】:

您需要在每次迭代时创建一个新的 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】:

    实际上返回类型应该是数组,不需要计数器变量,试试这个:

    - (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;
     }
    

    【讨论】:

      【解决方案3】:

      如果你有一个存储了一些值的 NSDictionary,试试使用这个语句。这会自动将特定的键值放入该数组中。

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

      或者你可以试试这个,

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

      【讨论】:

        【解决方案4】:

        首先创建一个名为

        的 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;
        
         }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多