现在我正在使用
SimpleDateFormat dateFormat = new SimpleDateFormat("d MMMM yyyy");
Date date = new Date(year,month,day);
dates.setText(dateFormat.format(date));
其中displayNames是
let array = (displayNames as NSArray).filteredArrayUsingPredicate(searchPredicate)
但我想用它:
var displayNames[String]()
如何在NSDictionary中使用var displayNames[String: UIImage]()
和displayNames字符串部分?
答案 0 :(得分:0)
使用allKeys属性从字典中获取密钥,然后对其执行过滤。试试吧。
重新编辑:尝试这样的事情
let theOriginalDictionary = [String : UIImage]()
let searchPredicate = NSPredicate(format: "", argumentArray: nil)
let otherDictionary = NSDictionary(dictionary: theOriginalDictionary)
let arrayOfKeys = NSArray(array: otherDictionary.allKeys)
let filteredArray = arrayOfKeys.filteredArrayUsingPredicate(searchPredicate)
答案 1 :(得分:0)
如果您使用Swift
,为什么不使用filter
等内置函数?
var displayNames = [String: UIImage]()
let result = displayNames.filter {key,image in
return true // here add your predicate to filter, true means returning all
}
答案 2 :(得分:0)
听起来你正试图过滤字典,而在过滤器后保留字典类型。有几种方法可以做到这一点,但也许最简单的方法是扩展字典:
extension Dictionary {
func filter(@noescape includeElement: (Key, Value) throws -> Bool) rethrows -> [Key:Value] {
var result: [Key:Value] = [:]
for (k,v) in self where try includeElement(k,v) {
result[k] = v
}
return result
}
}
然后,如果您想根据键过滤字典,可以执行以下操作:
let dict = ["a": 1, "b": 2, "c": 3]
let filtered = dict.filter { (k,_) in k != "a" }
// ["b": 2, "c": 3]