海泉的博客

开发Tips

Word count: 460Reading time: 2 min
2019/10/22 Share

Swift

对象数据转字典

通讯录中UITableView的数据源.

1
2
let arr = [ContactsModel]
let res = Dictionary(grouping: arr, by: { $0.name.prefix(1).uppercased()}) // [String:[ContactsModel]]
汉字转拼音
1
2
3
4
5
6
7
8
9
10
11
extension String {

func transformToPinYin() -> String {

let mutableString = NSMutableString(string: self)
CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false)
let string = String(mutableString)
return string.replacingOccurrences(of: " ", with: "")
}
}
Hex to byte
1
2
3
4
5
6
extension StringProtocol {
var hexa2Bytes: [UInt8] {
let hexa = Array(self)
return stride(from: 0, to: count, by: 2).compactMap { UInt8(String(hexa[$0..<$0.advanced(by: 2)]), radix: 16) }
}}
}
获取UICollectionView显示的中间的Cell

遵守Scrollview的代理方法

1
2
3
4
5
6
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

let centerCellIndexPath = dateCollectView!.getMidVisibleIndexPath()

let horDateSelectorCollectionViewCell = dateCollectView!.cellForItem(at: centerCellIndexPath!) as? FDHorDateSelectorCollectionViewCell
}

对UICollectionViewCell的extension

1
2
3
4
5
6
7
8
9
10
11
12
13
protocol CollectionVisibleMidCell {}
extension CollectionVisibleMidCell where Self: UICollectionView {

func getMidVisibleIndexPath() -> IndexPath? {
var visibleRect = CGRect()
visibleRect.origin = self.contentOffset
visibleRect.size = self.bounds.size
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
guard let indexPath = self.indexPathForItem(at: visiblePoint) else { return nil }
return indexPath
}
}
extension UICollectionView: CollectionVisibleMidCell {}
UITableView无刷新动画的刷新
1
2
3
4
5
6
//Reload data withoutAnimation
DispatchQueue.main.async {
UIView.performWithoutAnimation {
self.tableView.reloadData()
}
}
在合适的的时机移除通知和定时器
1
2
@available(iOS 5.0, *)
open var isMovingFromParent: Bool { get }

viewWillDisappear判断当前VC是由Pop,调用的,还是由Push调用的,可以再合适的时机调用移除通知或定时器。

swift的forin循环遍历
1
2
3
4
5
//定义一个Array的数组
var items = ["Swift", "OC", "Javascript"]
for _ in items {
items.removeLast()
}

swift中的循环遍历删除不会崩溃的。(官方源码)原因是在它的迭代器进行循环的,上面的代码可转化成如下代码

1
2
3
4
5
var items = ["Swift", "OC", "Javascript"]
var itemsIterator = items.makeIterator()
while let item = itemsIterator.next() {
items.removeLast()//此处删除items的时候,itemsIterator并不会受到影响。
}
用Label显示Unicode
1
2
3
4
5
6
7
//用Label显示Unicode

let label = UILabel.init(frame: CGRect.init(x: 10, y: 86, width: 100, height: 100))

label.text = "\u{1F32D}"

self.view.addSubview(label)
在String通过下标获取字符
1
2
3
4
5
private extension String {
subscript (index: Int) -> Character {
return self[self.index(self.startIndex, offsetBy: index)]
}
}

OC

CATALOG
  1. 1. Swift
    1. 1.0.0.0.1. 对象数据转字典
    2. 1.0.0.0.2. 汉字转拼音
    3. 1.0.0.0.3. Hex to byte
    4. 1.0.0.0.4. 获取UICollectionView显示的中间的Cell
    5. 1.0.0.0.5. UITableView无刷新动画的刷新
    6. 1.0.0.0.6. 在合适的的时机移除通知和定时器
    7. 1.0.0.0.7. swift的forin循环遍历
    8. 1.0.0.0.8. 用Label显示Unicode
    9. 1.0.0.0.9. 在String通过下标获取字符
  • 2. OC