Skip to content

Instantly share code, notes, and snippets.

View alfian0's full-sized avatar
🎯
Focusing

Muhammad Alfiansyah alfian0

🎯
Focusing
View GitHub Profile
@alfian0
alfian0 / SwiftNativeMergingAPICall.swift
Created December 10, 2016 17:11
Just Simple Merging API Call in Swift
func mergeAPICall() {
let group = dispatch_group_create()
dispatch_group_enter(group)
/***
Call your method for each endpoint after dispatch_group_enter(group)
*/
func fetchEventList(success: { (eventList) in
/***
Add dispatch_group_leave(group) if API Call return success or error
@alfian0
alfian0 / UITableViewCellUtils.swift
Created December 10, 2016 17:21
Swift UITableViewCell Utils
extension UITableViewCell {
class func nib() -> UINib {
let name = NSStringFromClass(self).componentsSeparatedByString(".").last!
return UINib(nibName: name, bundle: nil)
}
var parentTsableView: UITableView? {
get {
var table: UIView? = superview
@alfian0
alfian0 / UILabelUtils.swift
Last active October 8, 2017 12:19
Swift UILable Utils - Get UILabel Heigth based string length
extension UILabel {
func getEstimatedHeight(width: CGFloat) -> CGFloat {
guard let text = self.text else { return CGFloat.min }
var maxHeight = CGFloat.max
if (self.numberOfLines > 0) {
maxHeight = (ceil(self.font.lineHeight) * CGFloat(self.numberOfLines))
}
let maxSize = CGSizeMake(width, maxHeight)
@alfian0
alfian0 / UIViewUtils.swift
Created December 10, 2016 18:42
Swift UIView Utils - For AutoLayouting Programmatically
extension UIView {
func addConstarintsWithFormat(format: String, views: UIView...) {
var viewsDictionary = [ String : UIView ]()
for (index, view) in views.enumerate() {
let key = "v\(index)"
viewsDictionary[key] = view
view.translatesAutoresizingMaskIntoConstraints = false
}
@alfian0
alfian0 / UIColorUtils.swift
Created December 11, 2016 01:53
Swift UIColor Utils
extension UIColor {
public convenience init(hex: String) {
let characterSet = NSCharacterSet.whitespaceAndNewlineCharacterSet().mutableCopy() as! NSMutableCharacterSet
characterSet.formUnionWithCharacterSet(NSCharacterSet(charactersInString: "#"))
let cString = hex.stringByTrimmingCharactersInSet(characterSet).uppercaseString
if (cString.characters.count != 6) {
self.init(white: 1.0, alpha: 1.0)
} else {
var rgbValue: UInt32 = 0
@alfian0
alfian0 / GetJSONLocalFile.Swift
Created December 11, 2016 01:57
For get JSON localy
class GetJSONLocalFile {
class func fromFile(fileName: String) -> String? {
guard let path: String = NSBundle.mainBundle().pathForResource(fileName, ofType: "json") else {
print("Error!, Unable to load \(fileName).json")
return nil
}
do {
guard let data: String = try String(contentsOfFile: path) else {
return nil
@alfian0
alfian0 / JSON.swift
Created December 11, 2016 09:13 — forked from eblaauw/JSON.swift
Swift
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let urlPath = "http://telize.com/geoip"
let url = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
@alfian0
alfian0 / DateHelper
Created December 11, 2016 09:15 — forked from hpstuff/DateHelper
Swift
extension String {
func toDate(format: String) -> NSDate? {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.dateFromString(self)
}
}
extension NSDate {
func toString(format: String) -> String {
@alfian0
alfian0 / AppDelegate.swift
Created December 11, 2016 09:17
AppDelegate for Storyboard less
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.backgroundColor = UIColor.whiteColor()
window.makeKeyAndVisible()
self.window = window
let vc = UIViewController()
let nc = UINavigationController(rootViewController:vc)
window.rootViewController = nc
extension Array {
func forEach(f: (element: T) -> Void) {
for e in self {
f(element: e)
}
}
}