Cocoa 从数组生成objectForKey
当我使用此代码从名为“fileList”的文件名数组中获取字符串时,我获得了成功: 因此,我希望相同的代码生成相同的字符串作为我的密钥,如下所示:Cocoa 从数组生成objectForKey,cocoa,uilabel,nsdictionary,Cocoa,Uilabel,Nsdictionary,当我使用此代码从名为“fileList”的文件名数组中获取字符串时,我获得了成功: 因此,我希望相同的代码生成相同的字符串作为我的密钥,如下所示: NSDictionary *stats = [thisRecordingsStats objectForKey:[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]]; cell.durationLabel
NSDictionary *stats = [thisRecordingsStats objectForKey:[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
cell.durationLabel.text = [stats objectForKey:@"duration"];
NSDictionary *stats = [thisRecordingsStats objectForKey:[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
cell.durationLabel.text = [stats objectForKey:@"duration"];
或者这个:
NSDictionary *stats = [thisRecordingsStats objectForKey:@"%@",[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
NSDictionary *stats = [thisRecordingsStats objectForKey:@"%@",[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
这两个版本都没有错误,并且日志显示我的数据在那里:但是我得到了一个空白的UILabel。我没有正确编写动态密钥生成器吗? 当我使用此代码从名为“fileList”的文件名数组中获取字符串时,我获得了成功: 那么,这个消息表达式的结果就是你的密钥,对吗 也就是说,字典中的键是没有扩展名的文件名 因此,我希望相同的代码生成相同的字符串作为我的密钥,如下所示:
NSDictionary *stats = [thisRecordingsStats objectForKey:[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
cell.durationLabel.text = [stats objectForKey:@"duration"];
NSDictionary *stats = [thisRecordingsStats objectForKey:[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
cell.durationLabel.text = [stats objectForKey:@"duration"];
thisRecordingsStats
字典中查找该字符串的对象,从而获得另一个字典,您可以使用它初始化stats
变量stats
字典中查找对象的“duration”键,并将durationLabel
的文本设置为此对象
NSDictionary *stats = [thisRecordingsStats objectForKey:@"%@",[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
NSDictionary *stats = [thisRecordingsStats objectForKey:@"%@",[[[self.fileList objectAtIndex:[indexPath row]] lastPathComponent] stringByDeletingPathExtension]];
添加@“%@”,
部分没有意义,因为objectForKey:
不接受格式字符串。与…相比
代码“有效”,因为您作为参数传递给objectForKey:
的是逗号表达式。C的逗号运算符计算两侧,并计算右侧。然而,在这种情况下,就像在大多数其他情况下一样,它没有增加任何内容。出于这样的原因,逗号运算符很少被使用,甚至很少被故意使用
将@“%@”部分切下
回到问题上来:
这两个版本都没有错误,并且日志显示我的数据在那里:但是我得到了一个空白的UILabel
您可以说,您从fileList
数组中的字符串生成的密钥显示在UILabel中,因此问题在于:
此记录状态
为nil
thisRecordingStats
不包含从self.fileList
中的字符串生成的密钥的对象
thisRecordingStats
确实包含从self.fileList
中的字符串生成的键的对象,它是一个字典,但不包含键“duration”的值
thisRecordingStats
确实包含从self.fileList
中的字符串生成的键的对象,它是一个字典,包含键“duration”的值,但该值为空(零长度)字符串
您还应该检查调试器控制台中是否有提示其他问题的消息。例如,“不响应选择器”消息可能是因为thisRecordingStats
包含从self.fileList
中的字符串生成的密钥的对象,但它不是字典
最后,我建议构造一个或多个类,而不是像这样嵌套字典。它倾向于使代码更易于阅读和调试。特别是,表面上具有关键“duration”对象的字典应该是模型对象。您已经检查过thisRecordingStats和stats是否为非零?是的,NSLog显示thisRecordingStats的内容为{“4月25日下午4:37:13”={FILEPATH=“/Users/brian/Library/Application Support/iPhone Simulator/3.1.3/Applications/8287F6FC-3965-4E26-A214-E47C0C9B5687/Documents/4:37:13 PM,April 25.aif”;持续时间=“00:05”“etc.stats为零,但如果为key.wow!输入特定值,则不会。感谢您的慷慨响应。除了创建一个模型对象外,您是否可以建议一种从外部数组日志中获取内部数组objectAtIndex:1内容的方法,例如:thisRecordingsStats为{“4月26日下午5:45:02”=(“/Users/../Documents/5:45:02 PM,April 26.aif”,“00:04”等。我目前的最佳猜测是:NSArray*stats=[thisRecordingsStats objectForKey:key];cell.durationLabel.text=[stats objectAtIndex:0];它也返回空白。我没有看到内部数组。字典包含数组作为其值,但数组包含字符串(至少就你在那篇摘录中所展示的情况而言)记住Cocoa中的数组是严格序列的,而不是关联的;数组不是字典。