2015-09-11 16 views
5

tôi chỉ muốn một mảng chứa tất cả các ngày thứ Hai trong năm dưới dạng NSDate nhưng nhanh chóng. tôi đang sử dụng mã folowing trong mục tiêu -c nhưng không biết cách sử dụng nó nhanh chóng.Làm thế nào để có được Tất cả các chủ nhật trong Mảng ngày ios

NSDate *pickerDate = [NSDate date]; 
    NSLog(@"pickerDate: %@", pickerDate); 

    NSDateComponents *dateComponents; 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 

    dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:pickerDate]; 
    NSInteger firstMondayOrdinal = 9 - [dateComponents weekday]; 
    dateComponents = [[NSDateComponents alloc] init]; 
    [dateComponents setDay:firstMondayOrdinal]; 
    NSDate *firstMondayDate = [calendar dateByAddingComponents:dateComponents toDate:pickerDate options:0]; 

    dateComponents = [[NSDateComponents alloc] init]; 
    [dateComponents setWeek:1]; 

    for (int i=0; i<64; i++) { 
     [dateComponents setWeek:i]; 
     NSDate *mondayDate = [calendar dateByAddingComponents:dateComponents toDate:firstMondayDate options:0]; 
     NSLog(@"week#: %i, mondayDate: %@", i, mondayDate); 
    } 
+0

Có một phần cụ thể nào của mã này mà bạn đang gặp sự cố khi dịch không? –

+0

Có tôi chỉ muốn chuyển đổi mã này để tôi có thể nhận được tất cả các chủ nhật trong một mảng –

Trả lời

2

Ở đây bạn đi,

var pickerDate = NSDate() 
println(pickerDate) 

var dateComponents: NSDateComponents? = nil 
var calendar = NSCalendar.currentCalendar() 

dateComponents = calendar.components(NSCalendarUnit.CalendarUnitWeekday, fromDate: pickerDate) 
var firstMondayOrdinal = 9 - dateComponents!.weekday 
dateComponents = NSDateComponents() 
dateComponents!.day = firstMondayOrdinal 
var firstMondayDate = calendar.dateByAddingComponents(dateComponents!, toDate: pickerDate, options: NSCalendarOptions(0)) 

dateComponents = NSDateComponents() 
dateComponents?.weekdayOrdinal = 1 

for (var i=0; i<64; i++){ 
    dateComponents?.weekdayOrdinal = i 
    var mondayDate = calendar.dateByAddingComponents(dateComponents!, toDate: firstMondayDate!, options: NSCalendarOptions.MatchFirst) 
    println("\(i)" + "\(mondayDate!)"); 
} 
+0

Cảm ơn bạn đã lưu ngày của tôi ... Làm việc của nó hoàn toàn tốt cho tôi .... –

+2

sau đó, vui lòng chấp nhận câu trả lời của tôi :) (bởi đánh dấu vào dấu kiểm được hiển thị ở phía trên cùng bên trái của câu trả lời được cung cấp) –

1

Ở đây bạn đi. Nó khá đơn giản, nhưng có một vài gotchas, đặc biệt là optionSets cho NSCalendarOptions, thực tế là dateByAddingComponents trả về một tùy chọn, những thay đổi trong tên enum cho NSCalendarUnit và sự phản đối của NSDateComponents.week, nhưng nếu không nó đơn giản. Tôi đã không kiểm tra logic ...

//NSDate *pickerDate = [NSDate date]; 
let pickerDate = NSDate() /* I just used today for playground */ 

//NSLog(@"pickerDate: %@", pickerDate); 
print("pickerDate: \(pickerDate)") 

//NSDateComponents *dateComponents; 
var dateComponents: NSDateComponents /* var because you keep reallocating it */ 

//NSCalendar *calendar = [NSCalendar currentCalendar]; 
let calendar = NSCalendar.currentCalendar() 

//dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:pickerDate]; 
dateComponents = calendar.components(NSCalendarUnit.Weekday, fromDate: pickerDate) 

//NSInteger firstMondayOrdinal = 9 - [dateComponents weekday]; 
let firstMondayOrdinal = 9 - dateComponents.weekday 

//dateComponents = [[NSDateComponents alloc] init]; 
dateComponents = NSDateComponents() 

//[dateComponents setDay:firstMondayOrdinal]; 
dateComponents.day = firstMondayOrdinal 

//NSDate *firstMondayDate = [calendar dateByAddingComponents:dateComponents toDate:pickerDate options:0]; 
if let firstMondayDate = calendar.dateByAddingComponents(dateComponents, toDate: pickerDate, options: NSCalendarOptions(rawValue: 0)) { 
    /* this returns an optional so test for it */ 

    //dateComponents = [[NSDateComponents alloc] init]; 
    dateComponents = NSDateComponents() 

    //[dateComponents setWeek:1]; /* week deprecated */ 
    dateComponents.weekOfYear = 1 /* this line is redundant, re-done inside for */ 

    //for (int i=0; i<64; i++) { 
    for i in 0 ..< 64 { 
     //[dateComponents setWeek:i]; 
     dateComponents.weekOfYear = i 
     // NSDate *mondayDate = [calendar dateByAddingComponents:dateComponents toDate:firstMondayDate options:0]; 
     let mondayDate = calendar.dateByAddingComponents(dateComponents, toDate: firstMondayDate, options: NSCalendarOptions(rawValue: 0)) 
     /* no need to test for nil if just printing it, but beware */ 

     //NSLog(@"week#: %i, mondayDate: %@", i, mondayDate); 
     print("week#: \(i), mondayDate: \(mondayDate)") 
    } 
} 
3

Xcode 8 • Swift 3

extension Calendar { 
    static let gregorian = Calendar(identifier: .gregorian) 
} 

extension Date { 
    var startOfWeek: Date { 
     return Calendar.gregorian.date(from: Calendar.gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))! 
    } 
    var nextSunday: Date { 
     return Calendar.gregorian.date(byAdding: DateComponents(weekOfYear: 1), to: startOfWeek)! 
    } 
} 

let firstSunday = Date().nextSunday 
var result = [firstSunday] 
(1...52).forEach { _ in 
    guard let nextSunday = result.last?.nextSunday else { return } 
    result.append(nextSunday) 
} 
print(result) // "[2017-06-11 03:00:00 +0000, 2017-06-18 03:00:00 +0000, 2017-06-25 03:00:00 +0000, 2017-07-02 03:00:00 +0000, 2017-07-09 03:00:00 +0000, 2017-07-16 03:00:00 +0000, 2017-07-23 03:00:00 +0000, 2017-07-30 03:00:00 +0000, 2017-08-06 03:00:00 +0000, 2017-08-13 03:00:00 +0000, 2017-08-20 03:00:00 +0000, 2017-08-27 03:00:00 +0000, 2017-09-03 03:00:00 +0000, 2017-09-10 03:00:00 +0000, 2017-09-17 03:00:00 +0000, 2017-09-24 03:00:00 +0000, 2017-10-01 03:00:00 +0000, 2017-10-08 03:00:00 +0000, 2017-10-15 03:00:00 +0000, 2017-10-22 03:00:00 +0000, 2017-10-29 02:00:00 +0000, 2017-11-05 02:00:00 +0000, 2017-11-12 02:00:00 +0000, 2017-11-19 02:00:00 +0000, 2017-11-26 02:00:00 +0000, 2017-12-03 02:00:00 +0000, 2017-12-10 02:00:00 +0000, 2017-12-17 02:00:00 +0000, 2017-12-24 02:00:00 +0000, 2017-12-31 02:00:00 +0000, 2018-01-07 02:00:00 +0000, 2018-01-14 02:00:00 +0000, 2018-01-21 02:00:00 +0000, 2018-01-28 02:00:00 +0000, 2018-02-04 02:00:00 +0000, 2018-02-11 02:00:00 +0000, 2018-02-18 03:00:00 +0000, 2018-02-25 03:00:00 +0000, 2018-03-04 03:00:00 +0000, 2018-03-11 03:00:00 +0000, 2018-03-18 03:00:00 +0000, 2018-03-25 03:00:00 +0000, 2018-04-01 03:00:00 +0000, 2018-04-08 03:00:00 +0000, 2018-04-15 03:00:00 +0000, 2018-04-22 03:00:00 +0000, 2018-04-29 03:00:00 +0000, 2018-05-06 03:00:00 +0000, 2018-05-13 03:00:00 +0000, 2018-05-20 03:00:00 +0000, 2018-05-27 03:00:00 +0000, 2018-06-03 03:00:00 +0000, 2018-06-10 03:00:00 +0000]\n" 
1

Có một đánh giá thấp nhưng rất thuận tiện phương pháp enumerateDates(startingAfter:matching:matchingPolicy:) của Calendar

// Create the date component matching the weekday (Sunday = 1) 
let mondayComponent = DateComponents(weekday: 2) 

// Calculate Jan 1st of the current date 
let calendar = Calendar.current 
let currentDate = Date() 
var startOfYear = currentDate 
var interval : TimeInterval = 0.0 
_ = calendar.dateInterval(of:.year, start: &startOfYear, interval: &interval, for: currentDate) 

// Get the current year as integer 
let thisYear = calendar.component(.year, from: startOfYear) 

// Create an array for the result 
var allMondays = [Date]() 

// If Jan 1st is a Monday append it to the array 
if calendar.component(.weekday, from: startOfYear) == 2 { allMondays.append(startOfYear) } 

// Now enumerate all dates matching the weekday component within this year 
// If the year reaches thisYear + 1 the block will be exited.  
calendar.enumerateDates(startingAfter: startOfYear, matching: mondayComponent, matchingPolicy: .nextTime) { (date, strict, stop) in 
    guard let date = date else { return } 
    if calendar.component(.year, from: date) > thisYear { 
     stop = true 
    } else { 
     allMondays.append(date) 
    } 
} 
Các vấn đề liên quan