Skip to content

Instantly share code, notes, and snippets.

View ShannonChenCHN's full-sized avatar

ShannonChen ShannonChenCHN

View GitHub Profile
@joyrexus
joyrexus / README.md
Last active February 24, 2024 15:16
collapsible markdown

collapsible markdown?

CLICK ME

yes, even hidden code blocks!

print("hello world!")
@nicklockwood
nicklockwood / Deprecated.md
Last active March 28, 2022 08:16
Writing Objective-C framework code that works on multiple OS versions AND can be compiled using multiple SDK versions without warnings can be a PITA. Here's my approach:

Suppose we want to add support for a new iOS 8 API in our framework that replaces an older iOS 7 API. There are a few problems we might face:

  1. The new API will crash if we call it on iOS 7
  2. The new API won't compile if we build it using the iOS 7 SDK
  3. The old API will raise a deprecation warning if built with a deployment target of iOS 8 and up

These three problems require three different technical solutions:

  1. We can avoid calling the new API on an old OS version by using runtime detection (e.g. respondsToSelector:)
  2. We can avoid compiling new APIs on old SDKs using the __IPHONE_OS_VERSION_MAX_ALLOWED macro
@steipete
steipete / CaseInsensitiveDictionary.m
Created December 1, 2013 19:12
Implemented as a category on NSDictionary.
static Boolean PSPDFCaseInsensitiveEqualCallback(const void *a, const void *b) {
id objA = (__bridge id)a, objB = (__bridge id)b;
Boolean ret = FALSE;
if ([objA isKindOfClass:NSString.class] && [objB isKindOfClass:NSString.class]) {
ret = ([objA compare:objB options:NSCaseInsensitiveSearch|NSLiteralSearch] == NSOrderedSame);
}else {
ret = [objA isEqual:objB];
}
return ret;
}
@floriankugler
floriankugler / gist:6870499
Last active September 29, 2023 15:56
Mapping of NSURLConnection to NSURLSession delegate methods. Created by Mattt Thompson.
NSURLConnection | NSURLSession
------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------
NSURLConnectionDelegate connectionShouldUseCredentialStorage: |
------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------
NSURLConnectionDelegate connection:willSendRequestForAuthenticationChallenge: | NSURLSessionDelegate URLSession:didReceiveChallenge:completionHandler:
| N
@steipete
steipete / PSPDFUIKitMainThreadGuard.m
Last active March 10, 2024 19:23
This is a guard that tracks down UIKit access on threads other than main. This snippet is taken from the commercial iOS PDF framework http://pspdfkit.com, but relicensed under MIT. Works because a lot of calls internally call setNeedsDisplay or setNeedsLayout. Won't catch everything, but it's very lightweight and usually does the job.You might n…
// Taken from the commercial iOS PDF framework http://pspdfkit.com.
// Copyright (c) 2014 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
// Licensed under MIT (http://opensource.org/licenses/MIT)
//
// You should only use this in debug builds. It doesn't use private API, but I wouldn't ship it.
// PLEASE DUPE rdar://27192338 (https://openradar.appspot.com/27192338) if you would like to see this in UIKit.
#import <objc/runtime.h>
#import <objc/message.h>
@steipete
steipete / PSPDFViewController.h
Last active June 6, 2017 03:56
This method will help to prevent a lot of emails about "weird bugs".
// Defines a yet undocumented method to add a warning if super isn't called.
#ifndef NS_REQUIRES_SUPER
#if __has_attribute(objc_requires_super)
#define NS_REQUIRES_SUPER __attribute((objc_requires_super))
#else
#define NS_REQUIRES_SUPER
#endif
#endif
@interface UIViewController (SubclassingWarnings)
@tomohisa
tomohisa / gist:2897676
Created June 8, 2012 19:20
Add and Remove ChildViewController
// add child view
UIViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"test"];
[self addChildViewController:controller];
controller.view.frame = CGRectMake(0, 44, 320, 320);
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
// remove child view
UIViewController *vc = [self.childViewControllers lastObject];
[vc.view removeFromSuperview];
@jxson
jxson / README.md
Created February 10, 2012 00:18
README.md template

Synopsis

At the top of the file there should be a short introduction and/ or overview that explains what the project is. This description should match descriptions added for package managers (Gemspec, package.json, etc.)

Code Example

Show what the library does as concisely as possible, developers should be able to figure out how your project solves their problem by looking at the code example. Make sure the API you are showing off is obvious, and that your code is short and concise.

Motivation