Skip to content

Instantly share code, notes, and snippets.

View bananafish911's full-sized avatar
👽

Victor Do. bananafish911

👽
View GitHub Profile
extension String {
///For placeholders
static func randomAlphaNumericString(length: Int) -> String {
let charactersString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let charactersArray : [Character] = Array(charactersString.characters)
var string = ""
for _ in 0..<length {
string.append(charactersArray[Int(arc4random()) % charactersArray.count])
extension UIViewController {
func showAlert(message: String) {
let alert = UIAlertController(title: "Message", message:message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler:{ (ACTION :UIAlertAction!)in
debugPrint("User click Ok button")
}))
self.present(alert, animated: true, completion: nil)
}
}
extension Date {
/// For application labels
func toStringAppFormatted() -> String {
let components = NSCalendar.current.dateComponents([Calendar.Component.day,
Calendar.Component.month,
Calendar.Component.year],
from: self)
return "\(components.day!).\(components.month!).\(components.year!)"
}
extension UIColor {
/// Format: 0xFFAACC
convenience init(hex: Int) {
let components = (
R: CGFloat((hex >> 16) & 0xff) / 255,
G: CGFloat((hex >> 08) & 0xff) / 255,
B: CGFloat((hex >> 00) & 0xff) / 255
)
self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
extension TimeInterval {
// builds string in app's labels format 00:00.0
func stringFormatted() -> String {
var miliseconds = self.roundTo(places: 1) * 10
miliseconds = miliseconds.truncatingRemainder(dividingBy: 10)
let interval = Int(self)
let seconds = interval % 60
let minutes = (interval / 60) % 60
return String(format: "%02d:%02d.%.f", minutes, seconds, miliseconds)
}
extension Int {
/// SwiftRandom extension
static func random(lower: Int = 0, _ upper: Int = 100) -> Int {
return lower + Int(arc4random_uniform(UInt32(upper - lower + 1)))
}
var array: [Int] {
return description.characters.map{Int(String($0)) ?? 0}
}
}
/// Truncates String to given langth
///
/// - Parameter length: Result string will be limited to the given number of characters.
/// If length is 0 or less - empty string will be returned
/// - Returns: truncated string.
func truncateToLength(_ length: Int) -> String {
if length <= 0 {
// returns empty string
return ""
} else if length < self.characters.count {
@bananafish911
bananafish911 / Xcode-RunScript.sh
Last active January 29, 2017 17:14
Xcode run script: Objective-C, build warnings for (TODO: | FIXME:)"
KEYWORDS="TODO|FIXME"
echo "searching ${SRCROOT} for ${KEYWORDS} in *.h and *.m files"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($KEYWORDS).*\$" | perl -p -e "s/($KEYWORDS)/ warning: \$1/"
@bananafish911
bananafish911 / Xcode-RunScript.sh
Created January 29, 2017 17:15
highlight Swift macro TODO/FIXME as warning
# more info: http://stackoverflow.com/questions/24183812/swift-warning-equivalent
TAGS="TODO:|FIXME:"
echo "searching ${SRCROOT} for ${TAGS}"
find "${SRCROOT}" \( -name "*.swift" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/"
//
// AVMetadataItem with utf8 string (eg. Ukrainian, Russian)
//
import Foundation
import AVFoundation
extension AVMetadataItem {
/// stringValue: ISO-8859-1 → UTF-8