English 中文(简体)
添加字典中的物体
原标题:add objects to the dictionary using for loop

in my program i want to add 20 objects to dictionary using for loop so i did

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    self.responseData = nil;

    NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:@"loans"];
    NSLog(@"%i",[latestLoans count]);

parsedDict = [[NSMutableDictionary alloc] init];


for (int i=0; i<[latestLoans count]; i++) {


    //get latest loan
    NSDictionary* loan = [latestLoans objectAtIndex:i];

    //fetch the data
    NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
    NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
    float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];

    NSString* name = [loan objectForKey:@"name"];
    NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];

    [parsedDict setObject:fundedAmount forKey:@"funded_amount"];
    [parsedDict setObject:loanAmount forKey:@"loan_amount"];
    [parsedDict setObject:name forKey:@"name"];
    [parsedDict setObject:country forKey:@"location"];
        }
nslog(@"%@",parsedDict);

but when i log it out of the loop i am getting only the last added values..not all the values..

最佳回答

利用阵列储存记录......

  NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        self.responseData = nil;

        NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:@"loans"];
        NSLog(@"%i",[latestLoans count]);


     NSMutableArray someArray=[[NSMutableArray alloc]init];

    for (int i=0; i<[latestLoans count]; i++) {

    parsedDict = [[NSMutableDictionary alloc] init];
        //get latest loan
        NSDictionary* loan = [latestLoans objectAtIndex:i];

        //fetch the data
        NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
        NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
        float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];

        NSString* name = [loan objectForKey:@"name"];
        NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];

        [parsedDict setObject:fundedAmount forKey:@"funded_amount"];
        [parsedDict setObject:loanAmount forKey:@"loan_amount"];
        [parsedDict setObject:name forKey:@"name"];
        [parsedDict setObject:country forKey:@"location"];
        [someArray addObject:parsedDict];
            }
    NSLog(@"%@",parsedDict);

//Read from Array


for(NSDictionary *tempDict in someArray) 
{
   //use [tempDict objectForKey:@"keyName"] to get values from each dictionary
} 
问题回答

如果你增加2个具有相同关键意义的数值,那么最后的数值将显示为其他数值都过高。 你们可能希望(即使不是这样的话)为每个排位/字典添加一个字典。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签