使用 NSDate 搜索 NSArray o 字符串
很难正确理解标题,因为有太多要讲的内容。 我得到了这个 NSArray (其中一部分): 周一 06:00、早间广播、周一 10:00、午餐广播、14:00、下午广播等。 NSArray 来自应用程序中的 plist。
我需要构建(但失败了)的是一个搜索功能,它向我显示具有这些搜索值的正确程序: searchDay 和 searchTime
这两个值都来自 NSDate。 我已经在谷歌上搜索了大约一个小时,但还没有找到任何有用的东西,所以我最后的希望是通过 stackoverflow 来告诉我我错过了什么。
我一直在尝试 dateFromString、compare、timeIntervalSinceNow,但由于某种原因未能实现我要做的事情。
非常感谢所提供的任何帮助。
TIA 马蒂亚斯
Hard to get the headline correct since there is so much to tell.
I got this NSArray (A portion of it):
Mon, 06:00, Radioshow Morning, Mon, 10:00, Lunch radio, 14:00, Afternoon Radio and so on.
The NSArray is from an plist in the app.
What I need to build (and failed with) is a search function that shows me the right program with these search values:
searchDay and searchTime
Both those values are from NSDate.
I've been Googling for about and hour now and hasn't come up with anything usefull so my last hope stands to stackoverflow to show me what I am missing.
I've been dabbling with dateFromString, compare, timeIntervalSinceNow but for some reason failed to achive what I am about to do.
Greatful for any help givven.
TIA
Mattias
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 NSDictionary 对象的 NSArray,而不是 NSArray 中的各种对象类型:
然后将其添加到您的数组中。然后,当您搜索时,您就有一个统一的数据结构可以在其中进行搜索。
You should use an NSArray of NSDictionary objects instead of various object types in your NSArray:
Then add that to your array. Then, when you are searching, you have a uniform data structure to search inside of.
我想我已经解决了。
但我必须说我对 phooze 的答案很感兴趣。
我现在的做法是从数组中读取值(NSString),将它们转换为 NSDate 对象,并将它们与 timeIntervalSinceDate 进行比较。
这样我就可以知道 currentTime 是否在数组中的两个时间之间,这样我就知道正在播放什么节目。我刚刚想出了解决方案,基本代码正在运行,但整个代码尚未完成,只是在纸上/在我的脑海中。
但感觉代码有点难看……做了很多小事情。
但我会将此坚持到底,如果它仍然看起来效率低下且丑陋,我会寻找其他解决方案。
I think I have solved it.
But I must say I am intrigued by phooze answer.
The way I am doing it now is reading the values (NSString) from the array converting them into an NSDate object and comparing those with timeIntervalSinceDate.
By that I can figure out if currentTime is between two times in the array and such I know what show is on. I've just come up with the solution, the base code is working but the whole code isn't done yet, just on paper/in my mind.
But it feels like a somewhat ugly code... lots of doing small things.
But I will pre sue this to the end and if it still looks inefficient and ugly I will look in to other solutions.