Skip to content

Instantly share code, notes, and snippets.

@bobgodwinx
bobgodwinx / ConcurrentQueue_Objc.m
Last active October 26, 2015 14:28
dispatch_queue_t for concurrent use.
@interface className()
//create a property as dispatch_queue_t
@property (nonatomic) dispatch_queue_t concurrentQueue;
@end
@implementation className
//implement a lazy initializer
- (dispatch_queue_t)concurrentQueue
@bobgodwinx
bobgodwinx / IndexPath.swift
Last active October 26, 2015 16:25
IndexPath For UITextField
protocol cellIndexPathForUITextFieldDelegate:class {
var cellIndexPath: NSIndexPath? {get set}
}
extension UITextField: cellIndexPathForUITextFieldDelegate {
private struct AssociatedKeys {
static var kNSIndexPath = "kNSIndexPath"
}
var cellIndexPath: NSIndexPath? {
@bobgodwinx
bobgodwinx / Enum.swift
Last active October 26, 2015 14:39
Enum
enum CommunicatorError: CustomStringConvertible, ErrorType {
/**
used to communicate unknown error to user. This is a rear case.
*/
case CommunicatorUnknownError
/**
used to communicate network errors
*/
case CommunicatorNetworkError
/**
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
func makeRequest(success:(response:AnyObject)->(), failure:(error:CommunicatorError)->()) {
/**
Play with resp api take inspiration
https://grokswift.com/simple-rest-with-swift/
*/
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
let URL = NSURL(string: "http://jsonplaceholder.typicode.com/posts")
//let URL = NSURL(string: "http://private-anon-827bc57e0-andresbrun.apiary-mock.com/api/v3/search_suggestions")
let request = NSMutableURLRequest(URL: URL!)
lazy var concurrentQueue:dispatch_queue_t = {
var __dispatchToken:dispatch_once_t = 0
var queue:dispatch_queue_t?
let qos = qos_class_t(QOS_CLASS_USER_INITIATED.rawValue)
let attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT, qos, 2);
dispatch_once(&__dispatchToken, {
queue = dispatch_queue_create("com.companyname.concurrent", attr);
});
return queue!
}()
lazy var dateFormatter:NSDateFormatter = {
var dateFormatter:NSDateFormatter?
var __dispatchToken: dispatch_once_t = 0
dispatch_once(&__dispatchToken) {
dateFormatter = NSDateFormatter()
/**
setting the default locale to Germany // for USA use us_US
*/
dateFormatter?.locale = NSLocale(localeIdentifier: "de_DE")
/**
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>yourserver.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
/**
Telling the system to dispatch after 2 seconds on the main queue
*/
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(2.00 * Double(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue()) { [weak self] in
guard let strongSelf = self, let aProperty = aProperty else {
return
}
// Do something with aProperty
//
// Extensions.swift
// PaylevenSDKSwiftExample
//
// Created by Bob Obi on 7/2/15.
// Copyright (c) 2015 Obi Bob Godwin. All rights reserved.
//
import Foundation
import UIKit