Needing to get an object from an NSDictionary - objective-c

I have an NSDictionary that contains an array. What I am trying to do is get the object inside of the first part of my array photoDictionary:
-(NSString*) getImageAtIndex:(NSInteger *)index andObject:(PhotoPXArray *)photoArray {
NSDictionary *photosDictionary = [MTLJSONAdapter JSONDictionaryFromModel:photoArray];
NSArray = [[photosDictionary objectAtIndex:0];
//NSArray *photoCollection = [MTLJSONAdapter JSONArrayFromModels:self.photoPXArray];
return #"ff";
}
This should be really simple but I am banging my head against a wall.
My ultimate goal is to get the 'image_url' as shown in this object model:

Hot Licks answer was PERFECT!!
NSString* url = photosDictionary[#"photos"][0][#"image_url"];

Related

Search through data Multidimensional array

i've have this NSMutableArray called finalArray where i add the name and a image.
[finalArray addObject:#[name, image]];
in the following method its searching through the finalarray, but because its not expecting a multidimensional array it seem to give an error. How can i in this code:
self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];
get all the name objects from the finalArray? since that is what this code expect:
[[finalArray filteredArrayUsingPredicate:predicate]
the method:
-(void) searchThroughdata {
self.filteredArray = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains [c] %#",self.searchBar.text];
self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];
[tableViewData reloadData];
}
How do i add the image that belong to the name in the finalarray and still only search through the names like it do now with this?
[[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];
It looks like you are trying to associate names and images with each other by using the same index of an array or something? Am I right?
If so then you should really be using a custom class to do this...
#interface MyNameImageClass : NSObject
#property NSString *name;
#property UIImage *image;
#end
Now you can create the object...
MyNameImageClass *object = [MyNameImageClass new];
object.name = theName;
object.image = theImage;
Then just add this to an array...
[finalArray addObject:object];
Having said that though, you need to clarify what you are asking. The question is very confusing.
By doing this if the image is updated on the object you don't need to reinsert it into the array. The array stores a reference to the object so the array will have the updated value too.

Values changing automatically

Hi together I have a Problem:
I read out the actual Values of the Playing media song, and add it to a dictionary.
And then into an Array. If I add a new entry pressing the Button again, all Values of all entrys changing to the same.
Do you have an Idea why?
Here my Code:
- (IBAction)ActionButtonLU:(id)sender
{
MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
NSString *titleString = [currentItem valueForProperty:MPMediaItemPropertyTitle];
NSString *artistString = [currentItem valueForProperty:MPMediaItemPropertyArtist];
NSString *albumString = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];
if (titleString == nil) {
titleString = #"";
}
if (artistString == nil) {
artistString = #"";
}
` `if (albumString == nil) {
albumString = #"";
}
[_dictCat1 setObject:titleString forKey:#"Titel"];
[_dictCat1 setObject:artistString forKey:#"Artist"];
[_dictCat1 setObject:albumString forKey:#"Album"];
[_countCat1 addObject:_dictCat1];
[musicPlayer skipToNextItem];
}
You are modifying the same dictionary, _dictCat1, in every call. You need to create a dictionary locally and add that to _countCat1.
[_countCat1 addObject:#{ #"Title": titleString,
#"Artist": artistString,
#"Album": albumString }];
When you add an object to a collection (NSArray, NSDictionary, NSSet etc, including mutable counterparts), the collection doesn't copy it. With [_countCat1 addObject:_dictCat1]; you keep adding the same object to _countCat1 over and over. You need to create new NSDictionary and add that to _countCat1 collection.

Having trouble taking an index of an array and making it an NSString

I get an array from a JSON and I parse it into an NSMutableArray (this part is correct and working). I now want to take that array and print the first object to a Label. Here is my code:
NSDictionary *title = [[dictionary objectForKey:#"title"] objectAtIndex:2];
arrayLabel = [title objectForKey:#"label"];
NSLog(#"arrayLabel = %#", arrayLabel); // Returns correct
//Here is where I need help
string = [arrayLabel objectAtIndex:1]; //I do not get the first label (App crashes)
NSLog(#"string = %#", string);
other things that I have already tried are as follows:
string = [NSString stringWithFormat:#"%#", [arrayImage objectAtIndex:1]];
and
string = [[NSString alloc] initWithFormat:#"%#", [arrayImage objectAtIndex:1]];
Any help is greatly appriciated!
EDIT: The app does not return a single value and crashes.
Your code doesn't match the structure of your JSON. In your comment on the deleted answer, you said you got an exception when sending objectAtIndex: to an NSString. In your case, arrayLabel isn't an array when you think it is.
If your JSON has an object, your code needs to treat it as an NSDictionary. Likewise for arrays and NSArray and strings and NSString.
In addition to whatever else was going on, you repeatedly refer to "first" but use the index 1. In most C-based programming languages (and others, as well) the convention is that indexes into arrays are 0-based. So, use index 0 to get the first element.

Parsing a JSON of unknown Structure Objective-C

I am trying to parse this json and am not sure of parsing this because the keys like "vis-progress-control" might change and I am trying to build a general code which parses this type of a json. I am sure that "type" key will be present in the json structure.
NSDictionary *dict = [sData JSONValue];
NSArray *items = [dict valueForKeyPath:#"assets"];
NSLog(#"%#", items);
for (NSString *key in [[dict objectForKey:#"assets"]allKeys]) {
for (NSString *subKey in [[[dict objectForKey:#"assets"] objectForKey:key]allKeys]) {
NSLog(#"Value at subkey:%# is %#\n",subKey,[[[dict objectForKey:#"assets"]objectForKey:key]objectForKey:subKey]);
}
}
I am using the SBJson Library on Github. My actual issue is How do I access "direction", "degrees" etc when I do not know the "vjs-progress-holder" key?
I have also a widgets array nested within a widgets array. How do I get these values as well?
Read about tree traversal. Sounds like you want to traverse the "tree" of nodes and when you find a particular leaf value you will then traverse back up one level and know the parent is the container you want.
So the idea is that once it's parsed from the JSON, forget it's JSON because now it's just a tree in arrays and dictionaries. Traverse it by getting all the keys in the dictionary via allKeys (returns an array of keys) and then iterate through them getting the associated values (using something like (psuedo code):
for ( NSString * key in allkeysarray) {
NSString * val = [dict objectForKey: key];
if ( [val isEqualToString: #"gradient"] )
{
// now you know that this dictionary is the one you're looking for so you can maybe break out of this loop and just use the keys you know reference these values.
break;
}
}
hopefully that's enough to get you going.
Assuming I'm understanding your objective here, it seems like you want to do something like this?
NSDictionary *outerDict = [sData JSONValue];
NSDictionary *assets = outerDict[#"assets"];
for (NSDictionary *asset in [assets allValues]) {
NSString *type = asset[#"type"];
if ([type isEqualToString:#"gradient"]) {
float degrees = [asset[#"degrees"] floatValue];
// and read whatever other values you need for gradients
}
if ([type isEqualToString:#"image"]) {
// and read the appropriate values for images here...
}
}
So I'll make a different assumption here, which is that you want an array of gradients. So then that looks basically like this:
NSDictionary *outerDict = [sData JSONValue];
NSDictionary *assets = outerDict[#"assets"];
NSMutableArray *gradients = [NSMutableArray array];
for (NSDictionary *asset in [assets allValues]) {
NSString *type = asset[#"type"];
if ([type isEqualToString:#"gradient"]) {
// Add this asset to the list of gradients:
[gradients addObject:asset];
}
if ([type isEqualToString:#"image"]) {
// do something similar for images
}
}
Then, after having done that, if you would like to read the "degrees" field for the 4th gradient, for example, you will find it at gradients[3][#"degrees"]

# char as a key in NSDictionary

I tried using the # char as a key in a NSDictionary and my application simply crashes. I searched for "invalid" key names, could not find the #"#" anywhere. If I use something else than #"#" it all works fine.
I have a list of comanies, I get the first letter of each company and then I am creating a NSMutableDictionary entry containing the first letter as the key and a NSMutableArray as the value.
NSMutableDictionary indexDictionary = [[NSMutableDictionary alloc] init];
// here we have a loop on companyName
{
NSString *fristLetter = [[companyName substringToIndex:1] uppercaseString];
NSMutableArray *arrayOfIndexedCompanies = [indexDictionary valueForKey:firstLetter];
if (arrayOfIndexedCompanies) {
[arrayOfIndexedCompanies addObject:companyName]
}
else {
NSMutableArray *newArray = [NSMutableArray array];
[indexDictionary setObject:newArray forKey:firstLetter];
[newArray addObject:companyName];
}
}
I enabled breakpoint break on throw and it stops at the [indexDictionary valueForKey:firstLetter]... only when firstLetter is a #"#".
I had an if saying:
if ([firstLetter isEqualToString:#"#"]) {
firstLetter = #"A";
}
and this works fine, it places the # starting companies in the A section correctly. If I am letting the firstLetter unchanged (leaving it as #"#") the application will crash.
Also, this is not really my code, I am just trying to fix it, I am not quite familiar with ObjC and Foundation so please be gentle.
Read the documentation :)
Specifically, this bit :
If key does not start with “#”, invokes objectForKey:. If key does start with “#”, strips the “#” and invokes [super valueForKey:] with the rest of the key.
I guess that super valueForKey: isn't happy being called :)
To fix it, just call objectForKey: instead of valueForKey:
Using the string #"#" as a dictionary key works just fine:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:#"Test 1", #"#", #"Test 2", #"Another #", nil];
NSString *result = [dict objectForKey:#"#"];
NSLog(#"%#", result);
// --> Test 1
You could give a try with #"\u0040"