generating an objectForKey from an array
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.f开发者_C百科ileList 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?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, the result of that message expression is your key, right?
That is to say, the keys in your dictionary are filenames without extensions?
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"];
- You compute the filename without extension as before.
- You look up the object for this string in the
thisRecordingsStats
dictionary, thus obtaining another dictionary, with which you initialize thestats
variable. - You look up the object for the “duration” key in the
stats
dictionary, and set thedurationLabel
'stext
to this object.
or this:
NSDictionary *stats = [thisRecordingsStats objectForKey:@"%@",[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
Adding the @"%@",
part doesn't make sense, since objectForKey:
doesn't take a format string. Compare the documentation for NSString's stringWithFormat:
method to the documentation for NSDictionary's objectForKey:
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:
Both build without error, and the log shows my data is there: but I'm getting a blank UILabel.
Well, you say the key you're generating from the string in your fileList
array shows up in the UILabel, so the problem is one of these:
thisRecordingStats
isnil
.thisRecordingStats
does not contain an object for the key you generated from the string inself.fileList
.thisRecordingStats
does 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”.thisRecordingStats
does 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 thisRecordingStats
contains an object for the key you generated from the string in self.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.
精彩评论