[英]How to store Value of NSDictionary to NSMutableArray in loop?
我正在从数据库中获取数据,并且我将获得数据库中最后的相同数据。 如何在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;
}
)
实际上,返回类型应该是数组,并且不需要计数器变量,请尝试以下操作:
- (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;
}
您需要在每次迭代中创建一个新的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 * ?? 也许那里也有错误...
如果您的NSDictionary中存储了一些值,请尝试使用此语句。 这将自动将特定的键值放入该数组。
arrayName = [dictionaryName valueForKey:@"keyName"];
或者你可以尝试一下
[arrayName addObject:[dictionaryName objectForKey:@"keyName"];
首先全部创建一个名为的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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.