prayTimesDate(date: NSDate,latitIDe : Double,longitude : Double,timeZone : Double) -> NSMutableArray
我需要迭代一整年或者一个日期范围来获得一年中每一天的一系列时间.我在ruby和python中发现了很多关于如何做到这一点的引用,但我找不到任何关于swift或objective-c的东西. swift中是否有任何内置方法可以实现此目的?如果没有,有人可以帮助我,因为我仍然是编程新手.任何输入都非常感谢.
这是我链接到我的swift项目的方法的objective-c代码
- (NSMutableArray *)prayerTimesDate:(NSDate *)date latitude:(double)latitude longitude:(double)longitude andTimezone:(double)timezone{ unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:unitFlags fromDate:date]; NSInteger year = [components year]; NSInteger month = [components month]; NSInteger day = [components day]; return [self getDatePrayerTimesForYear:year month:month day:day latitude:latitude longitude:longitude andtimeZone:timezone];}解决方法 假设你的prayTimesDate:方法已经返回了预期的结果,你可以循环遍历一年中的每一天,同时反复调用prayTimesDate:获取包含每天祷告时间的数组,例如:
func yearlyPrayerDatesFromCurrentDate (latitude:Double,longitude:Double,timezone:Double) -> NSMutableArray { // Set "date" to equal the current day var date:NSDate! = NSDate() // Increment "date" by one year to calculate the ending // date for the loop let gregorian:NSCalendar! = NSCalendar(calendarIDentifIEr: NSCalendarIDentifIErGregorian) let dateComponents = NSDateComponents() dateComponents.year = 1 let endingDate:NSDate! = gregorian.dateByAddingComponents(dateComponents,toDate: date,options: nil) // Create an array to hold *all* the returned // results for the year var datesArray = NSMutableArray() // Loop through each date until the ending date is // reached while date.compare(endingDate) != NSComparisonResult.OrderedDescending { // Call your prayerTimesDate: method on the current // date to get that date's prayer times and add the // times from the returned array to the datesArray datesArray.addobjectsFromArray(prayerTimesDate(date,latitude: latitude,longitude: longitude,andTimezone: timezone)) // increment the date by 1 day let dateComponents = NSDateComponents() dateComponents.day = 1 date = gregorian.dateByAddingComponents(dateComponents,options: nil) } return datesArray}总结
以上是内存溢出为你收集整理的ios – 迭代一年以获得Swift中每天的NSDate对象全部内容,希望文章能够帮你解决ios – 迭代一年以获得Swift中每天的NSDate对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)