简体   繁体   中英

How to read a NSMutableArray out of a NSMutableDictionary

I am having problems reading out a mutable array out of a mutable dictionary. I am trying to read it out, and then put it into a label. Here is how my data is organized. I have a mutable array of dictionaries. Then in my tableview I take the object at index path and set it equal to a dictionary in the app delegate. Then in the details view I load the dictionary that I took out of the array and I read from it and put the values into the labels. I have all the strings in the dictionary working, but the arrays won't read out, can you help me

RootViewController.m

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SurveyAppAppDelegate *surveyAppAppDelegate = (SurveyAppAppDelegate*)[[UIApplication sharedApplication] delegate];

    SurveyViewer *surveyViewController = [[SurveyViewer alloc] initWithNibName:@"SurveyViewer" bundle:nil]; 
    surveyAppAppDelegate.arrayCount = [[self surveyArray] objectAtIndex:indexPath.row];


    [self.navigationController pushViewController:surveyViewController animated:YES];
    [surveyViewController release];
}

In my App Delegate it is set as NSDictionary.

Then in my detail view controller I do this

  injuredArray = [surveyAppAppDelegate.arrayCount objectForKey:@"Injured Part"];

    if ([injuredArray count] >= 1) {
        for (int i = 0; i<=([injuredArray count]-1); i++) {
            myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 105+shift, 139, 15)];
            myLabel.text = [injuredArray objectAtIndex:i];
            myLabel.backgroundColor = [UIColor clearColor];
            myLabel.font = [UIFont fontWithName:@"Helvetica" size: 14.0];
            myLabel.textColor = [UIColor blackColor];
            myLabel.textAlignment = UITextAlignmentLeft;
            [self.theScroller addSubview:myLabel];
            shift = shift+20;
        }
    }

In my App Delegate I set up arrayCount like this

.h

#import <UIKit/UIKit.h>

    @class RootViewController;

    @interface SurveyAppAppDelegate : NSObject <UIApplicationDelegate> {
        NSDictionary *arrayCount;
    }

    @property (nonatomic, assign) NSDictionary *arrayCount;

    @end

and .m

#import "SurveyAppAppDelegate.h"
@implementation SurveyAppAppDelegate
    @synthesize arrayCount;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {


        arrayCount = [[NSDictionary alloc] init];

Then in the dealloc of the app delegate I release it

Oh, and the objects in the NSMutableArray that I set to injuredArray are all strings

EDIT: So I added a loop to NSLog the array in the root view controller right after I set it, and it isn't working. So it looks for some reason that it isn't being saved in arrayCount correctly

Thanks for your help

You might consider creating an init method in your detail view controller that accepts the NSDictionary that your are interested in and retains it in a property. I cannot tell where this reference in coming from: surveyAppAppDelegate.arrayCount

In your appDelegate header, try retaining the dictionary instead of assigning:

@property (nonatomic, retain) NSDictionary *arrayCount;

then in implementation:

NSDictionary *anArrayCount = [[NSDictionary alloc] init];
self.arrayCount = anArrayCount;
[anArrayCount release];

Is the injuredArray object you're assigning already allocated and initialized? That could be the problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM