我试图用基于数组切片的节和行填充表。我一直得到“Cannot invoke initializer for type“Objects”with and argument list of type”(sectionName:String,sectionObjects:ArraySlice)。想知道是否有人能帮我弄清楚我遗漏了什么。谢谢。

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = " eee, MMM dd, yyyy "
let today = (NSDate())
let comps : NSCalendarUnit = NSCalendarUnit.Day
let cal = NSCalendar.currentCalendar()
let todaysDate = cal.startOfDayForDate(today)
var dateRange = [String]()

var dates = [String]()
var displayMe = [String]()

for i in 0 ... 179 {


let day = dateFormatter.stringFromDate(cal.dateByAddingUnit(comps, value: +i, toDate: todaysDate, options: [] )!)

dates += [day]

}

for (index, value) in dates.enumerate() {
var testMe = ("Day  \(index + 1)   - \( value)")
print(testMe)

displayMe += [testMe]


}



let datesOneRange = displayMe[0...13]
let datesFifteenRange = displayMe[14...28]
let datesThrityRange = displayMe[29...58]
let datesSixtyRange = displayMe[59...88]
let datesNinetyRange = displayMe[89...99]
let datesOneHunEighty = displayMe[100...179]




var dateDict = [ " 5 Days" : datesOneRange,
" 14 Days" : datesFifteenRange,
" 30 Days" : datesThrityRange,
" 60 Days" : datesSixtyRange,
" 90 Days" : datesNinetyRange,
"180 Days" : datesOneHunEighty]

struct Objects {

var sectionName : String!
var sectionObjects : [String]!
}

var objectArray = [Objects]()



for (key, value) in dateDict {
    print("\(key) -> \(value)")
    objectArray.append(Objects(sectionName: key, sectionObjects:       value))
}

最佳答案

可以使用Array(ArraySlice)
这是您的更新代码:

func test(){
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = " eee, MMM dd, yyyy "
        let today = (NSDate())
        let comps : NSCalendarUnit = NSCalendarUnit.Day
        let cal = NSCalendar.currentCalendar()
        let todaysDate = cal.startOfDayForDate(today)

        var dates = [String]()
        var displayMe = [String]()

        for i in 0 ... 179 {
            let day = dateFormatter.stringFromDate(cal.dateByAddingUnit(comps, value: +i, toDate: todaysDate, options: [] )!)
            dates += [day]
        }

        for (index, value) in dates.enumerate() {
            let testMe = ("Day  \(index + 1)   - \( value)")
            print(testMe)
            displayMe += [testMe]
        }



        let datesOneRange = displayMe[0...13]
        let datesFifteenRange = displayMe[14...28]
        let datesThrityRange = displayMe[29...58]
        let datesSixtyRange = displayMe[59...88]
        let datesNinetyRange = displayMe[89...99]
        let datesOneHunEighty = displayMe[100...179]




        let dateDict = [ " 5 Days" : datesOneRange,
            " 14 Days" : datesFifteenRange,
            " 30 Days" : datesThrityRange,
            " 60 Days" : datesSixtyRange,
            " 90 Days" : datesNinetyRange,
            "180 Days" : datesOneHunEighty]

        struct Objects {
            var sectionName : String!
            var sectionObjects : [String]!
        }

        var objectArray = [Objects]()

        for (key, value) in dateDict {
            print("\(key) -> \(value)")
            objectArray.append(Objects(sectionName: key, sectionObjects:Array(value)))
        }
    }

09-26 20:22