• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 IOS 中遍历 NSDictionary 和计时事件

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:14:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我是 IOS/Objective C 的新手,我正在尝试找出创建集合、迭代它以及为事件计时的最佳方法。

我有一首歌曲的一系列歌词,当音乐在歌曲的正确位置播放时,我希望一首歌曲的单独一行出现在屏幕上。因此,我开始执行以下操作:我将各个行放入 Dictionary 以及该行应该出现的毫秒值。

   NSDictionary *bualadhBos = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithInt:2875], @"Muid uilig ag bualadh bos, ",
                             [NSNumber numberWithInt:3407], @"Muid uilig ag tógáil cos, ",
                             [NSNumber numberWithInt:3889], @"Muid ag déanamh fead ghlaice, ",
                             [NSNumber numberWithInt:4401], @"Muid uilig ag geaibíneacht. ",
                             [NSNumber numberWithInt:4900], @"Buail do ghlúine 1, 2, 3, ",
                             [NSNumber numberWithInt:5383], @"Buail do bholg mór buí, ",
                             [NSNumber numberWithInt:5910], @"Léim suas, ansin suigh síos, ",
                             [NSNumber numberWithInt:6435], @"Seasaigh suas go hard arís. ",
                             [NSNumber numberWithInt:6942], @"Sín amach do dhá lamh, ",
                             [NSNumber numberWithInt:7430], @"Anois lig ort go bhfuil tú ' snámh. ",
                             [NSNumber numberWithInt:7934], @"Amharc ar dheis, ansin ar chlé, ",
                             [NSNumber numberWithInt:8436], @"Tóg do shúile go dtí an spéir. ",
                             [NSNumber numberWithInt:8940], @"Tiontaigh thart is thart arís, ",
                             [NSNumber numberWithInt:9436], @"Cuir síos do dhá lámh le do thaobh, ",
                             [NSNumber numberWithInt:9942], @"Lámha suas is lúb do ghlúin, ",
                             [NSNumber numberWithInt:10456], @"Suigí síos anois go ciúin. ", nil
                             ];

然后我想遍历字典,创建一个 Timer,它会调用一个负责更改 textLayer 中文本的方法

for (id key in bualadhBos ) {
    NSTimer *timer;
    timer = [[NSTimer scheduledTimerWithTimeInterval:bualadhBos[key] target:self selectorselector(changeText) userInfo:nil repeats:NO]];

}

-(void)changeText {
    // change the text of the textLayer
    textLayer.string = @"Some New Text";
}

但是当我开始调试它并检查它是如何工作的时,我注意到在调试器中项目出现在字典中的顺序都被打乱了。我还担心(我对此知之甚少)我正在创建多个计时器,并且可能有更有效的方法来解决这个问题。

任何方向将不胜感激。



Best Answer-推荐答案


字典以最适合哈希算法的方式按定义排序,因此您永远不应依赖它们的顺序。

在您的情况下,最好构建一棵二叉树并拥有一个每秒触发一次的 NSTimer,执行二叉树搜索并返回提供的时间偏移量最接近的字符串。

如果您使用 AVFoundation 或 AVPlayer 进行播放。然后要将字幕与媒体播放同步,您可以使用 addPeriodicTimeObserverForInterval 之类的东西每秒触发一次计时器并在二叉树中执行搜索并更新 UI。

在伪代码中:

[player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time) {
    // get playback time
    NSTimeInterval seconds = CMTimeGetSeconds(time);

    // search b-tree
    NSString* subtitle = MyBtreeFindSubtitleForTimeInterval(seconds);

    // update UI
    myTextLabel.text = subtitle;
}];

关于ios - 在 IOS 中遍历 NSDictionary 和计时事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25996838/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap