Giter Site home page Giter Site logo

stv-extensions's Issues

isScrollEnd()をまとめる

@stv-ekushida
isScrollEnd()UIScrollViewにまとめられそうです。

  • 修正内容
    • isScrollEnd()UIScrollViewextensionにする
    • NITS: CGRectのプロパティに直接アクセス self.bounds.size.height -> self.bounds.height
  • 効果
    • サブクラスのUICollectionViewからもUITableViewからも利用できる
    • UIScrollViewからもisScrollEnd()が使えるようになる
    • 定義場所がひとつでOK
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")
}

【Collection】Arrayなどでfatal error: Index out of rangeを発生させず、nilを返す

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

【String】MD5ハッシュ化したString

頻出ではないかもしれませんが、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】Date→曜日の文字列

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())”) // 火

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.