How do I access a member inside another member of NSDictionary while keeping a link between members

StackOverflow https://stackoverflow.com/questions/22750571

Pregunta

NSDictionary confuses me. It is somewhat like a hybrid of a struct and an array from what I gather. I'm going through the iOS Programming book and it has us put a JSON object into an NSDictionary. All is fine for objects with the key "titles" to return, but for upcoming classes there are variables inside upcoming. In my head this is how the NSDictionary would be built.

struct classes {
    NSString *title;
    struct upcoming[] {
        NSString *end_date;
        NSString *start_date;
        NSString *instructor;
        }
    }
}

I've been trying to retrieve the start_date's inside a for loop by using variations of:

NSLog(@"%@", [course objectForKey:@"upcoming.start_date"]);

The purpose of me doing this is to append the string of title's that have a value inside upcoming classes with their start_date. Something like...

NSString *value = course[@"title"];
if ([course objectForKey@"upcoming"] [value stringByAppendingString:
                                         @" course[@"upcoming.start_date"];
NSLog(@"%@", value);

How do I make it so that IF there IS one or more values for upcoming then it will display those start_date values by appending the string displayed ELSE it won't do anything? Obviously upcoming.start_date is not the way to do it! and start_date by itself has no link to the relevant title. I need them both to be linked to append the appropriate string.

Solution:

NSString *title = [course objectForKey:@"title"];
NSArray *upcomingCourses = [course objectForKey:@"upcoming"];
if (upcomingCourses.count > 0
    NSString *showDate = [NSString stringWithFormat:@"%@. Next Class Date: %@"
    title, [[upcomingCourses objectAtIndex:0] objectForKey:@"start_date"]];
    NSLog(@"%@", showDate);
}
else NSLog(@"%@", title); 

Posted NSDictionary from NSLog:

courses =     (
            {
        title = "Advanced iOS Bootcamp";
        upcoming =             (
                            {
                "end_date" = "2014-04-04";
                instructors = Blocksom;
                location = "BNR West, California";
                "start_date" = "2014-03-31";
            },
                            {
                "end_date" = "2014-05-16";
                instructors = Dalrymple;
                location = "BNR West, California";
                "start_date" = "2014-05-12";
            },
                            {
                "end_date" = "2014-06-20";
                instructors = Ward;
                location = "Atlanta, Georgia";
                "start_date" = "2014-06-16";
            }
        );
        url = "https://training.bignerdranch.com/classes/advanced-ios-bootcamp";
    },
            {
        title = "Beginning iOS (iPhone/iPad)";
        upcoming =             (
                            {
                "end_date" = "2014-04-18";
                instructors = Mathias;
                location = "BNR West, California";
                "start_date" = "2014-04-12";
            },
                            {
                "end_date" = "2014-05-02";
                instructors = "Keur, Ward";
                location = "Atlanta, Georgia";
                "start_date" = "2014-04-26";
            },
                            {
                "end_date" = "2014-05-23";
                instructors = Christopher;
                location = "Atlanta, Georgia";
                "start_date" = "2014-05-17";
            },
                            {
                "end_date" = "2014-06-06";
                instructors = Christopher;
                location = "BNR West, California";
                "start_date" = "2014-05-31";
            },
                            {
                "end_date" = "2014-07-25";
                instructors = Ritchie;
                location = "BNR West, California";
                "start_date" = "2014-07-19";
            },
                            {
                "end_date" = "2014-08-01";
                instructors = "Usiyan, Keur";
                location = "Atlanta, Georgia";
                "start_date" = "2014-07-26";
            },
                            {
                "end_date" = "2014-09-19";
                instructors = Ritchie;
                location = "BNR West, California";
                "start_date" = "2014-09-13";
            }
        );
        url = "https://training.bignerdranch.com/classes/beginning-ios";
    },
            {
        title = "Mobile Design Bootcamp";
        upcoming =             (
                            {
                "end_date" = "2014-06-13";
                instructors = "Harper, Porter";
                location = "BNR West, California";
                "start_date" = "2014-06-09";
            }
        );
        url = "https://training.bignerdranch.com/classes/mobile-design-bootcamp";
    },
            {
        title = "Python Programming";
        upcoming =             (
                            {
                "end_date" = "2014-04-24";
                instructors = Cassell;
                location = "Atlanta, Georgia";
                "start_date" = "2014-04-21";
            },
                            {
                "end_date" = "2014-06-19";
                instructors = Cassell;
                location = "BNR West, California";
                "start_date" = "2014-06-16";
            }
        );
        url = "https://training.bignerdranch.com/classes/python-programming";
    },
            {
        title = "Beginning iOS Game Development";
        upcoming =             (
                            {
                "end_date" = "2014-06-20";
                instructors = Strougo;
                location = "BNR West, California";
                "start_date" = "2014-06-16";
            },
                            {
                "end_date" = "2014-09-26";
                instructors = Strougo;
                location = "BNR West, California";
                "start_date" = "2014-09-22";
            }
        );
        url = "https://training.bignerdranch.com/classes/beginning-ios-game-development";
    },
            {
        title = "Android Bootcamp";
        upcoming =             (
                            {
                "end_date" = "2014-05-16";
                instructors = Phillips;
                location = "Atlanta, Georgia";
                "start_date" = "2014-05-12";
            },
                            {
                "end_date" = "2014-06-20";
                instructors = Marsicano;
                location = "Atlanta, Georgia";
                "start_date" = "2014-06-16";
            },
                            {
                "end_date" = "2014-07-11";
                instructors = Stewart;
                location = "BNR West, California";
                "start_date" = "2014-07-07";
            },
                            {
                "end_date" = "2014-10-03";
                instructors = Phillips;
                location = "BNR West, California";
                "start_date" = "2014-09-29";
            }
        );
        url = "https://training.bignerdranch.com/classes/android-bootcamp";
    },
            {
        title = "HTML5 Apps with jQuery";
        upcoming =             (
                            {
                "end_date" = "2014-05-16";
                instructors = Aquino;
                location = "Atlanta, Georgia";
                "start_date" = "2014-05-12";
            },
                            {
                "end_date" = "2014-06-13";
                instructors = Gandee;
                location = "Atlanta, Georgia";
                "start_date" = "2014-06-09";
            },
                            {
                "end_date" = "2014-08-01";
                instructors = Aquino;
                location = "BNR West, California";
                "start_date" = "2014-07-28";
            }
        );
        url = "https://training.bignerdranch.com/classes/html5-apps-with-jquery";
    },
            {
        title = "Ruby on the Server";
        upcoming =             (
                            {
                "end_date" = "2014-06-13";
                instructors = Stewart;
                location = "Atlanta, Georgia";
                "start_date" = "2014-06-09";
            }
        );
        url = "https://training.bignerdranch.com/classes/ruby-on-the-server";
    },
            {
        title = "iOS Bootcamp - Fast Track";
        upcoming =             (
                            {
                "end_date" = "2014-07-11";
                instructors = Usiyan;
                location = "Atlanta, Georgia";
                "start_date" = "2014-07-07";
            }
        );
        url = "https://training.bignerdranch.com/classes/ios-bootcamp-fast-track";
    },
            {
        title = "Cocoa I Bootcamp";
        upcoming =             (
                            {
                "end_date" = "2014-07-25";
                instructors = Preble;
                location = "Atlanta, Georgia";
                "start_date" = "2014-07-21";
            }
        );
        url = "https://training.bignerdranch.com/classes/cocoa-i-bootcamp";
    },
            {
        title = "OpenGL ES Bootcamp";
        upcoming =             (
                            {
                "end_date" = "2014-07-25";
                instructors = Blocksom;
                location = "Atlanta, Georgia";
                "start_date" = "2014-07-21";
            }
        );
        url = "https://training.bignerdranch.com/classes/opengl-es-bootcamp";
    }
);

}

¿Fue útil?

Solución

NSDictionary allows you to retrieve objects by key. The key is just another object, but commonly NSStrings are used (the restriction being that keys must be unique, values do not need to be). Any object can be stored under the key, so in your case 'upcoming' is an array of NSDictionaries. So you get

dictionary {
   title: someString
   upcoming: array {
       0: dictionary {
          end_date: somestring
          start_date: someString
          instructor: someString
       }
       1: dictionary {
          end_date: somestring
          start_date: someString
          instructor: someString
       }
   }

}

Note that the above isn't any particular language or syntax, just a representation of the 'structure' of your data.

To retrieve the course details you would access the "upcoming" object just like any other and assign it to a variable.

Assuming course is your root NSDictionary:

NSString *title=(NSString *)[course objectForKey:@"title"];
NSArray *upcomingCourses=(NSArray *)[course objectForKey:@"upcoming"];

NSMutableString *startDates=[[NSMutablestring alloc]init];

for (int i=0;i<[upcomingCourses count];i++)
{  
    NSDictionary *upcomingCourse = (NSDictionary *)[upcomingCourses objectAtIndex:i];
    NSString *startDate = (NSString *)[upcomingCourse objectForKey:@"start_date"];
    NSString *endDate = (NSString *)[upcomingCourse objectForKey:@"end_date"];
    NSString *instructor = (NSString *)[upcomingCourse objectForKey:@"instructor"];

    [startDates appendFormat:@"%@, ",startDate];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top