Skip to content

Instantly share code, notes, and snippets.

View tanduynguyen's full-sized avatar

Duy Nguyen tanduynguyen

View GitHub Profile
@huynguyencong
huynguyencong / CodableTransformerPropertyWrapper.swift
Last active May 28, 2021 10:15
Use property wrapper to reduce meaningless code in a decodable, when you need to customize how to parse just 1 key, but have to implement all keys. It makes decoding a custom key using Codable is short and simple like TransformerOf of ObjectMapper open source.
// IMPORTANCE: I don't use this anymore, because it has problem with a json that missing key. In normal, we can ignore a missing key by set a property optional. But in this case, setting property optional still causes no key error.
/* Huy Nguyen
Use property wrapper to reduce meaningless code in a decodable, when you need to customize how to parse just 1 key, but have to implement all keys.
This snippet assumes you need to decode some models that currency values are Strings with "$" prefix ("$20.3" to 20.3), and you want to parse them to Double values.
For example, the JSON of Product model:
{
"price": "$20.3",
class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = -1.0
attributes?.forEach { layoutAttribute in
if layoutAttribute.frame.origin.y >= maxY {
leftMargin = sectionInset.left
@auycro
auycro / spritesheetGen.m
Last active September 22, 2016 06:56
generate sprite from sprite sheet
//http://stackoverflow.com/questions/20271812/use-a-one-image-sprite-sheet-in-sprite-kit-ios
Entity *cookie = [[Entity alloc] initWithSpriteSheetNamed:@"cookie_sheet" withinNode:map sourceRect:CGRectMake(0, 0, 32, 32) andNumberOfSprites:6];
- (id) initWithSpriteSheetNamed: (NSString *) spriteSheet withinNode: (SKSpriteNode *) scene sourceRect: (CGRect) source andNumberOfSprites: (int) numberOfSprites {
// @param numberOfSprites - the number of sprite images to the left
// @param scene - I add my sprite to a map node. Change it to a SKScene
// if [self addChild:] is used.
@nonamelive
nonamelive / DMNavigationController.m
Last active April 21, 2023 06:56
Prevent UINavigationController from pushing two view controllers at the same time in case of stack inconsistency and crashes
@interface UINavigationController (DMNavigationController)
- (void)didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
@end
@interface DMNavigationController ()
@property (nonatomic, assign) BOOL shouldIgnorePushingViewControllers;
@hujunfeng
hujunfeng / ios7statusbar.md
Last active March 20, 2019 14:20
About iOS 7 Status Bar Style

UIStatusBarStyle in iOS 7

  • The status bar in iOS 7 is transparent, the view behind it shows through.

  • The style of the status bar refers to the appearances of its content. In iOS 7, the status bar content is either dark (UIStatusBarStyleDefault) or light (UIStatusBarStyleLightContent). Both UIStatusBarStyleBlackTranslucent and UIStatusBarStyleBlackOpaque are deprecated in iOS 7.0. Use UIStatusBarStyleLightContent instead.

How to change UIStatusBarStyle

  • If below the status bar is a navigation bar, the status bar style will be adjusted to match the navigation bar style (UINavigationBar.barStyle):