//最喜欢的DDLog
func DDLog(_ msgs: Any..., fileName: String = #file, methodName: String = #function, lineNumber: Int = #line){
#if DEBUG
let params = msgs.compactMap{ "\($0)" }.joined(separator: "\n__");
let formatter = DateFormatter.dateFormat(formatStr: "yyyy-MM-dd HH:mm:ss.SSS");
print(formatter.string(from: Date()),"\((fileName as NSString).lastPathComponent).\(methodName)[\(lineNumber)]:\n__\(params)")
#endif
}
extension DateFormatter{
//日期转换中保证一种类型只生成一个DateFormatter对象
static func dateFormat(formatStr:String) -> DateFormatter {
let dic = Thread.current.threadDictionary;
if dic.object(forKey: formatStr) != nil {
return dic.object(forKey: formatStr) as! DateFormatter;
}
let fmt = DateFormatter();
fmt.dateFormat = formatStr;
fmt.locale = NSLocale.current;
fmt.timeZone = NSTimeZone.local;
dic.setObject(fmt, forKey: formatStr as NSCopying)
return fmt;
}
}
//(三参数)打印示例:
2018-09-07 15:35:08.841 SecondViewController.swift.requestWithRank(rank:handler:)[112]:
__Optional(<SwiftTemplet.RootClass: 0x6040002bab80>)
2018-09-07 15:35:20.924 FourthViewController.swift.viewDidLoad()[32]:
__Optional(<UITapGestureRecognizer: 0x6040001f1800; state = Ended; view = <SwiftTemplet.BN_ViewZero 0x7f8791c63f00>; target= <(action=handActionTapWithTap:, target=<SwiftTemplet.BN_ViewZero 0x7f8791c63f00>)>>)
__<SwiftTemplet.BN_ViewZero: 0x7f8791c63f00; frame = (20 20; 160 160); gestureRecognizers = <NSArray: 0x604000a457c0>; layer = <CALayer: 0x604000821c40>>
__0
//MARK:数组
//随机
var random: Element? {
if self.count == 0 {
return nil;
}
let idx = Int(arc4random_uniform(UInt32(self.count)));
return self[idx];
}
//乱序
var shuffle: Array! {
if self.count == 0 {
return self;
}
var list = self;
for index in 0..<list.count {
let newIndex = Int(arc4random_uniform(UInt32(list.count-index))) + index;
if index != newIndex {
list.swapAt(index, newIndex);
}
}
return list;
}
//子数组
func subarray(_ loc: Int, _ len: Int) -> Array {
assert(loc < self.count);
return Array(self[loc...len]);
}
备注:
let list = [1,2,4,3,9,8];
let b = list[2...5];// ArraySlice
ArraySlice不能长期保存,可能会有内存泄漏,附上官方api的截图
Important
Long-term storage of ArraySlice instances is discouraged. A slice holds a reference to the entire storage of a larger array, not just to the portion it presents, even after the original array’s lifetime ends. Long-term storage of a slice may therefore prolong the lifetime of elements that are no longer otherwise accessible, which can appear to be memory and object leakage.
警告:不鼓励/建议长时间的存储ArraySlice实例
由于一个ArraySlice实例呈现的是某个比较大的数组上的一个视图。如果这个原始数组已经结束了其生命周期,存储这个切片实例可能会延长那些不能再被访问的数组元素的存活时间,这就造成了明显的内存和对象泄露。为了防止这个影响的发生,只在临时性的计算时,使用ArraySlice。
===
//MARK:字典排序
func sortDictiony() -> Void {
//字典排序
/*
字典中$0 来表示闭包的第一个参数,$1 来表示第二个,以此类推,in 也可以省略
元组 **(key: String, value: String) 第0个是 key,第一个是 value **
dict.sorted { (<#(key: String, value: String)#>, <#(key: String, value: String)#>) -> Bool in
<#code#>
}
*/
let dict = ["27":"w","15":"t","36":"b"]
let dicSortedByKey = dict.sorted(by: {$0.0 < $1.0})
let dicSortedByValue = dict.sorted(by: {$0.1 < $1.1})
print(dicSortedByKey)
print(dicSortedByValue)
let dicSortedByKeyNew = dict.sorted(by:<)
let dicSortedByValueNew = dict.sorted(by:>)
print(dicSortedByKeyNew)
print(dicSortedByValueNew)
}
网友评论