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";
}
);
}