Skip to content

Instantly share code, notes, and snippets.

@BasThomas
Created February 9, 2015 07:56
Show Gist options
  • Save BasThomas/59a736deab3d1466758d to your computer and use it in GitHub Desktop.
Save BasThomas/59a736deab3d1466758d to your computer and use it in GitHub Desktop.
//
// Extensions.swift
// Apostle Partnerapp
//
// Created by Bas on 15/01/2015.
// Copyright (c) 2015 Bas. All rights reserved.
//
import UIKit
/// String extensions.
extension String
{
/**
Checks if a given email is valid.
*/
func isEmail() -> Bool
{
let regex = NSRegularExpression(pattern: "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", options: .CaseInsensitive, error: nil)
return regex?.firstMatchInString(self, options: nil, range: NSMakeRange(0, countElements(self))) != nil
}
/**
Checks if a Twitter post doesn't exceed the platform's char limit.
:returns: If the message is a valid Twitter post.
*/
func isValidTwitterPost() -> Bool
{
return countElements(self) <= 140
}
/**
Gets the amount of Twitter chars left.
:returns: The amount of Twitter chars left.
*/
func twitterCharsLeft() -> Int
{
return 140 - countElements(self)
}
}
/// NSDate extensions
extension NSDate: Equatable, Comparable
{
/**
Checks if the date is today.
:returns: If NSDate is today or not.
*/
func isToday() -> Bool
{
return NSCalendar.currentCalendar().isDateInToday(self)
}
func inPast() -> Bool
{
return self < NSDate()
}
func format(dateStyle style: DateType) -> String
{
switch(style)
{
case .Today:
return NSDateFormatter.localizedStringFromDate(self, dateStyle: .NoStyle, timeStyle: .ShortStyle)
default:
return NSDateFormatter.localizedStringFromDate(self, dateStyle: .MediumStyle, timeStyle: .ShortStyle)
}
}
}
public func ==(lhs: NSDate, rhs: NSDate) -> Bool
{
return lhs.timeIntervalSince1970 == rhs.timeIntervalSince1970
}
public func <(lhs: NSDate, rhs: NSDate) -> Bool
{
return lhs.timeIntervalSince1970 < rhs.timeIntervalSince1970
}
/// UIColor extensions
extension UIColor
{
/**
Returns a color object whose RGB values are 0.0, 0.737, and 0.871 and whose alpha value is 1.0.
:returns: The UIColor object.
*/
class func apostleColor() -> UIColor
{
return UIColor(red: 0, green: 0.737, blue: 0.871, alpha: 1)
}
/**
Returns a color object whose RGB values are 0.012, 0.478, and 1.0 and whose alpha value is 1.0.
:returns: The UIColor object.
*/
class func lightBlueColor() -> UIColor
{
return UIColor(red: 0.012, green: 0.478, blue: 1, alpha: 1)
}
/**
Returns a color object whose RGB values are 0.349, 0.729, and 0.384 and whose alpha value is 1.0.
:returns: UIColor with darkGreenColor
*/
class func darkGreenColor() -> UIColor
{
return UIColor(red: 0.349, green: 0.729, blue: 0.384, alpha: 1.0)
}
}
/// UIDevice extensions
let deviceList = [
"i386": "Simulator",
"x86_64": "Simulator",
"iPod1,1": "iPod Touch",
"iPod2,1": "iPod Touch 2",
"iPod3,1": "iPod Touch 3",
"iPod4,1": "iPod Touch 4",
"iPhone1,1": "iPhone",
"iPhone1,2": "iPhone 3G",
"iPhone2,1": "iPhone 3GS",
"iPad1,1": "iPad",
"iPad2,1": "iPad 2",
"iPad3,1": "The new iPad", // iPad 3
"iPhone3,1": "iPhone 4",
"iPhone3,2": "iPhone 4",
"iPhone4,1": "iPhone 4S",
"iPhone5,1": "iPhone 5", // (model A1428, AT&T/Canada)
"iPhone5,2": "iPhone 5", // (model A1429, everything else)
"iPad3,4": "iPad with Retina Display", // iPad 4
"iPad2,5": "iPad mini",
"iPhone5,3": "iPhone 5c", // (model A1456, A1532 | GSM)
"iPhone5,4": "iPhone 5c", // (model A1507, A1516, A1526 (China), A1529 | Global)
"iPhone6,1": "iPhone 5s", // (model A1433, A1533 | GSM)
"iPhone6,2": "iPhone 5s", // (model A1457, A1518, A1528 (China), A1530 | Global)
"iPad4,1": "iPad Air (WiFi)",
"iPad4,2": "iPad Air (Cellular)",
"iPad4,4": "iPad mini 2 (WiFi)",
"iPad4,5": "iPad mini 2 (Cellular)",
"iPhone7,1": "iPhone 6 Plus",
"iPhone7,2": "iPhone 6",
"iPad4,7": "iPad mini 3 (WiFi)",
"iPad4,8": "iPad mini 3 (Cellular)",
"iPad4,9": "iPad mini 3 (China model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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
machine: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
)
uname(&systemInfo)
let machine = systemInfo.machine
var identifier = ""
let mirror = reflect(machine)
for i in 0 ..< reflect(machine).count
{
if mirror[i].1.value as Int8 == 0
{
break
}
identifier.append(UnicodeScalar(UInt8(mirror[i].1.value as Int8)))
}
if let readableIdentifier = deviceList[identifier]
{
return readableIdentifier
}
return identifier
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment