Skip to content

Instantly share code, notes, and snippets.

View nvkiet's full-sized avatar

Kiet Nguyen - CoFounder & CEO at GrabLingo.com nvkiet

View GitHub Profile
@nvkiet
nvkiet / SingleTon
Created July 20, 2014 13:32
Declare a Singleton in Swift
class var sharedInstance : PusherManager {
struct Static {
static let instance : PusherManager = PusherManager()
}
return Static.instance;
}
@nvkiet
nvkiet / [Swift] GetCountryNames
Last active April 23, 2016 20:36
Declare a static variable in a function
class func countryNames() -> [String] {
struct Static {
static var instance: [String]?
}
if let names = Static.instance {
return names
}
var names = [String]()
for code in NSLocale.ISOCountryCodes() as [String] {
@nvkiet
nvkiet / LinkerFlag
Created July 24, 2014 02:00
Compile correctly for non-ARC projects
-fno-objc-arc
Add -fobjc-arc to compiler flags under build phases to ARC files to make them compile correctly for non-ARC projects.
@nvkiet
nvkiet / HideStatusbar
Created July 24, 2014 08:04
Force hide status bar in iOS 6 and iOS 7
//viewDidload
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
} else {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
// Add this Method
@nvkiet
nvkiet / SaveClassObject
Created July 28, 2014 08:34
Save class object use NSUserDefaults + Mental
+ (id)objectForKey:(NSString *)key
{
if ([key length] == 0) {
return nil;
}
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
if (!data) {
return nil;
}
@nvkiet
nvkiet / [Swift] UpdatedCodeBeta5
Last active August 29, 2015 14:04
Updated code to these changes for Xcode 6 beta 5
// Indicate != nil instead of bool
if navigationController {
}
if navigationController != nil {
}
// Use require keywork for require methods
@nvkiet
nvkiet / keyboardWindowLevel
Last active August 29, 2015 14:05
Increase keyboard window level
NSArray* array = [UIApplication sharedApplication].windows;
for(id windowObject in array){
NSString* class = NSStringFromClass([windowObject class]);
if([class isEqualToString:@"UITextEffectsWindow"]){
UIWindow* keyboardWindow = (UIWindow*)windowObject;
keyboardWindow.windowLevel = self.view.window.windowLevel+1;
}
}
@nvkiet
nvkiet / AnyObjectToJSONString
Created August 16, 2014 07:34
[Swift] Convert AnyObject to JSON String
class LINJSONHelper {
class func jsonStringWithObject(obj: AnyObject) -> String? {
var error: NSError?
let jsonData = NSJSONSerialization.dataWithJSONObject(obj, options: NSJSONWritingOptions(0), error: &error)
if error != nil {
println("Error creating JSON data: \(error!.description)");
return nil
}
return NSString(data: jsonData, encoding: NSUTF8StringEncoding)
@nvkiet
nvkiet / [Swift]KeepUItableviewStatic
Created August 17, 2014 06:12
Keep uitableview static when inserting rows at the top
private func addListBubbleCellsWithCount(count: Int) {
var contentOffset = self.tableView.contentOffset
UIView.setAnimationsEnabled(false)
var indexPaths = [NSIndexPath]()
var heightForNewRows: CGFloat = 0
for var i = 0; i < count; i++ {
let indexPath = NSIndexPath(forRow: i, inSection: 0)
@nvkiet
nvkiet / [Swift] Hide-show keyboard
Created August 20, 2014 14:16
Hide/Show keyboard with Swift language
func handleKeyboardWillShowNotification(notification: NSNotification) {
if self.inputContainerViewBottomLayoutGuideConstraint.constant == 0 {
keyboardWillChangeFrameWithNotification(notification, showKeyboard: true)
scrollBubbleTableViewToBottomAnimated(true)
}
}
func handleKeyboardWillHideNotification(notification: NSNotification) {
if self.inputContainerViewBottomLayoutGuideConstraint.constant > 0 {
keyboardWillChangeFrameWithNotification(notification, showKeyboard: false)