This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func listEntities(managedContext: NSManagedObjectContext, entityName: String, groupBy: Array<String>)->NSArray{ | |
var error: NSError? | |
var results : NSArray = NSArray() | |
let fetchRequest = NSFetchRequest(entityName: entityName) | |
if groupBy.count > 0{ | |
fetchRequest.propertiesToGroupBy = groupBy | |
} | |
if let results = managedContext.executeFetchRequest(fetchRequest, error: &error) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NSTimer { | |
class func scheduledTimerWithTimeInterval(interval: NSTimeInterval, repeats: Bool, handler: NSTimer! -> Void) -> NSTimer { | |
let fireDate = interval + CFAbsoluteTimeGetCurrent() | |
let repeatInterval = repeats ? interval : 0 | |
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, repeatInterval, 0, 0, handler) | |
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes) | |
return timer | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func uicolorFromHex(rgbValue:UInt32)->UIColor{ | |
let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0 | |
let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0 | |
let blue = CGFloat(rgbValue & 0xFF)/256.0 | |
return UIColor(red:red, green:green, blue:blue, alpha:1.0) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String{ | |
var length: Int{ | |
get{ | |
return self.utf16Count | |
} | |
} | |
func isValidEmail() -> Bool { | |
let emailRegex = NSRegularExpression(pattern: "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", options: .CaseInsensitive, error: nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIColor { | |
convenience init(red: Int, green: Int, blue: Int) { | |
assert(red >= 0 && red <= 255, "Invalid red component") | |
assert(green >= 0 && green <= 255, "Invalid green component") | |
assert(blue >= 0 && blue <= 255, "Invalid blue component") | |
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0) | |
} | |
convenience init(netHex:Int) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let text = [UInt8]("hello!!!".utf8) | |
let cipher = [UInt8]("good".utf8) | |
var encrypted = [UInt8]() | |
// encrypt bytes | |
for t in enumerate(text) { | |
encrypted.append(t.element ^ cipher[t.index%cipher.count]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a: AM/PM | |
A: 0~86399999 (Millisecond of Day) | |
c/cc: 1~7 (Day of Week) | |
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat | |
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday | |
d: 1~31 (0 padded Day of Month) | |
D: 1~366 (0 padded Day of Year) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private protocol _Optional { | |
func unwrappedString() -> String | |
} | |
extension Optional: _Optional { | |
private func unwrappedString() -> String { | |
switch self { | |
case .Some(let wrapped as _Optional): return wrapped.unwrappedString() | |
case .Some(let wrapped): return String(wrapped) | |
case .None: return String(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NSData { | |
func hexadecimal() -> String { | |
var string = "" | |
var byte: UInt8 = 0 | |
for i in 0 ..< length { | |
getBytes(&byte, range: NSMakeRange(i, 1)) | |
string += String(format: "%02x", byte) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private protocol _Optional { | |
func unwrappedString() -> String | |
} | |
extension Optional: _Optional { | |
fileprivate func unwrappedString() -> String { | |
switch self { | |
case .some(let wrapped as _Optional): return wrapped.unwrappedString() | |
case .some(let wrapped): return String(describing: wrapped) | |
case .none: return String(describing: self) |