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 / ConvertNSDataToNSString
Last active August 29, 2015 14:19
ConvertNSDataToNSString
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"ck"
ofType:@"pem"];
NSData *p12data = [NSData dataWithContentsOfFile:certificatePath];
NSString *apnsToken = [p12data base64EncodedString];
NSDictionary *parameters = @{@"apnsToken": apnsToken};
@nvkiet
nvkiet / SimpleEnum
Created March 31, 2015 10:05
How to make a enum conform to a protocol in Swift.
enum SimpleEnum : ExampleProtocol {
case Base, Adjusted
var simpleDescription: String {
get {
return self.getDescription()
}
}
func getDescription() -> String {
@nvkiet
nvkiet / Books.markdown
Last active August 29, 2015 14:17
Books

Books

@nvkiet
nvkiet / gist:a28bbc22488a324df61d
Last active August 29, 2015 14:10
multi-context-coredata
Solution 1:
NSMangedObjectContext *temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
temporaryContext.parentContext = mainMOC;
[temporaryContext performBlock:^{
// do something that takes some time asynchronously using the temp context
// push to parent
NSError *error;
@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)
@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 / 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 / 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 / [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 / 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;
}