stv-ekushida / stv-extensions Goto Github PK
View Code? Open in Web Editor NEWiOS Swift3版のExtension集です。
License: MIT License
iOS Swift3版のExtension集です。
License: MIT License
@stv-ekushida
isScrollEnd()
をUIScrollView
にまとめられそうです。
isScrollEnd()
をUIScrollView
のextension
にするCGRect
のプロパティに直接アクセス self.bounds.size.height
-> self.bounds.height
UICollectionView
からもUITableView
からも利用できるUIScrollView
からもisScrollEnd()
が使えるようになるimport UIKit
extension UIScrollView {
/// 最下セルまでスクロールしたか?
func isScrollEnd() -> Bool {
return self.contentOffset.y >= self.contentSize.height - self.bounds.height
}
}
// MARK: - demo
let collectionView = UICollectionView(frame: .zero)
if collectionView.isScrollEnd() {
print("scrollEnd")
}
let tableView = UITableView(frame: .zero)
if tableView.isScrollEnd() {
print("scrollEnd")
}
Arrayのfatal error: Index out of rangeでクラッシュしないよう、nilを返すExtensionです。
extension Collection {
subscript(safe index: Index) -> _Element? {
return index >= startIndex && index < endIndex ? self[index] : nil
}
}
使い方
let array = ["foo", "bar"]
print(array[0]) // foo
print(array[1]) // bar
print(array[2]) // fatal error: Index out of rangeでクラッシュする
print(array[safe: 0]) // Optional("foo")
print(array[safe: 1]) // Optional("bar")
print(array[safe: 2]) // nil
頻出ではないかもしれませんが、MD5でハッシュ化するextensionです。
定義
extension String {
/// MD5ハッシュ化したString
var md5: String {
var md5String = ""
let digestLength = Int(CC_MD5_DIGEST_LENGTH)
let md5Buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: digestLength)
if let data = self.data(using: .utf8) {
data.withUnsafeBytes({ (bytes: UnsafePointer<CChar>) -> Void in
CC_MD5(bytes, CC_LONG(data.count), md5Buffer)
md5String = (0..<digestLength).reduce("") { $0 + String(format:"%02x", md5Buffer[$1]) }
})
}
return md5String
}
}
使い方
let string = "demo"
let hashedString = string.md5
Dateから曜日の文字列を取得するものです。
import Foundation
public extension Date {
func shortWeekdayStr() -> String {
return weekDaySymbol(date: self, style: .short)
}
func weekdayStr() -> String {
return weekDaySymbol(date: self, style: .normal)
}
private enum WeekDayStyle {
case normal
case short
}
private func weekDaySymbol(date: Date, style: WeekDayStyle) -> String {
let calendar = Calendar.init(identifier: .gregorian)
let weekday = calendar.component(.weekday, from: date)
let weekdaySymbolIndex = weekday - 1
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "ja")
switch style {
case .normal:
return formatter.weekdaySymbols[weekdaySymbolIndex]
case .short:
return formatter.shortWeekdaySymbols[weekdaySymbolIndex]
}
}
}
使い方
let date = Date()
print("\(date.weekdayStr())”) // 火曜日
print("\(date.shortWeekdayStr())”) // 火
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.