我在swift中擴展Array
類。我需要排序的數組,但是當我嘗試把它給我的錯誤Immutable value of type 'Array<T>' only has mutating members named 'sort'
在Array擴展Swift上不能調用自己的函數
這裏是我的分機:
import UIKit
extension Array {
func sortedArrayByTimeUsingKey(key: String, ascending: Bool) {
sort { (firstObject, secondObject) -> Bool in
let firstDict = firstObject as [String: AnyObject]
let secondDict = secondObject as [String: AnyObject]
let firstDateString = firstDict[key]! as String
let secondDateString = secondDict[key]! as String
let dateFormatter = DateFormatter()
let firstDate = dateFormatter.dateFromString(firstDateString)!
let secondDate = dateFormatter.dateFromString(secondDateString)!
let order = firstDate.compare(secondDate)
switch order {
case .OrderedAscending:
return ascending ? true : false
case .OrderedDescending:
return ascending ? true : false
default:
return true
}
}
}
}
我怎麼能投self
是可變的或者我怎麼能檢查是否self
是可變的?