I’m having success when I use this code to get a string from an array of file names called “fileList”:
cell.timeBeganLabel.text = [[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension];
so I expected the same code to generate the same string as a key for me in this:
NSDictionary *stats = [thisRecordingsStats objectForKey:[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
cell.durationLabel.text = [stats objectForKey:@"duration"];
or this:
NSDictionary *stats = [thisRecordingsStats objectForKey:@"%@",[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
Both build without error, and the log shows my data is there: but I’m getting a blank UILabel.
Have I not written the dynamic key generator correctly?
So, the result of that message expression is your key, right?
That is to say, the keys in your dictionary are filenames without extensions?
thisRecordingsStatsdictionary, thus obtaining another dictionary, with which you initialize thestatsvariable.statsdictionary, and set thedurationLabel‘stextto this object.Adding the
@"%@",part doesn’t make sense, sinceobjectForKey:doesn’t take a format string. Compare the documentation for NSString’sstringWithFormat:method to the documentation for NSDictionary’sobjectForKey:method.The code “works” because what you have passed as the argument to
objectForKey:is a comma expression. C’s comma operator evaluates both sides and evaluates to the right-hand side. However, in this case as in most others, it adds nothing. For reasons like this, the comma operator is rarely used and even more rarely used on purpose.Cut the
@"%@",part out.Back to the problem:
Well, you say the key you’re generating from the string in your
fileListarray shows up in the UILabel, so the problem is one of these:thisRecordingStatsisnil.thisRecordingStatsdoes not contain an object for the key you generated from the string inself.fileList.thisRecordingStatsdoes contain an object for the key you generated from the string inself.fileList, and it is a dictionary, but it does not contain a value for the key “duration”.thisRecordingStatsdoes contain an object for the key you generated from the string inself.fileList, and it is a dictionary, and it contains a value for the key “duration”, but that value is an empty (zero-length) string.You should also check the Debugger Console for messages that suggest other problems. For example, a “does not respond to selector” message may be because
thisRecordingStatscontains an object for the key you generated from the string inself.fileList, but it is not a dictionary.Finally, I suggest constructing one or more model object classes instead of nesting dictionaries like this. It tends to make the code much easier to read and to debug. In particular, the dictionaries that ostensibly have objects for the key “duration” should be model objects.