Skip to content

Instantly share code, notes, and snippets.

View zwang's full-sized avatar
😀
Plato App

Zhao Wang zwang

😀
Plato App
View GitHub Profile
@zwang
zwang / KeychainItemWrapper in ARC
Created August 27, 2012 04:23
Use Apple's KeychainItemWrapper in ARC project from @ErikEvenson
Thanks to ErikEvenson https://github.com/ErikEvenson
I did three things to use Apple's current sample code in my ARC projects:
1) Set -fno-objc-arc as a compiler flag on KeychainItemWrapper.m under Build Phases, Compile Sources.
2) Use __bridge_transfer in the call to the wrapper: NSString *password = [wrapper objectForKey:(__bridge_transfer id)kSecValueData];
3) Add an autorelease to line 196 of Apple's code to plug Apple's memory leak: self.keychainItemData = [[[NSMutableDictionary alloc] init] autorelease];
Another solution which rewrites the code in ARC can be found here
https://gist.github.com/1170641
@zwang
zwang / gist:5667392
Created May 29, 2013 01:36
Load a class dynamically in Java
private Class<?> clazz = null;
public MonitorResinThread(Timer timer) {
super(timer, 1);
//Make sure the class is only load once
try {
String homePath = "/Home/folder/to/jar";
File file = new File(homePath + "/lib/resin.jar");
URL url = new URL("jar", "", "file:" + file.getAbsolutePath() + "!/");
URL[] urls = new URL[] { url };
@zwang
zwang / center div.css
Last active December 19, 2015 22:09
Make the inner Div center horizontally and vertically in the outer div
<style>
#outer
{
background:red;
display:table;
width: 100%;
height:600px;
}
#inner
{
@zwang
zwang / removeTrailingSpace
Last active December 23, 2015 11:09
Remove trailing white space in Eclipse
You can easily remove all the trailing whitespace within a single file by using
a regular expression with the Find/Replace dialog. Enter [ \t]+$ in the "Find:" box
and leave the "Replace with:" box empty. Check the "Regular expressions" option
then click "Replace All"
@zwang
zwang / iosVersionRelatedBugFix.swift
Last active March 12, 2016 01:55
iOS 9.0.x bug automaticallyAdjustsScrollViewInsets doesn't change insets on iOS 9
#if os(iOS)
// temp fix for iOS 9.0.x http://www.openradar.me/22106545
let os = NSProcessInfo().operatingSystemVersion
switch (os.majorVersion, os.minorVersion, os.patchVersion) {
case (9, 0, _):
// "iOS >= 9.0.0, < 9.1.0"
// Only in iOS 9.0.x, the contentInset of the collection view does not automatically add the statusbar height and the navigation bar height
let statusBarheight = UIApplication.sharedApplication().statusBarFrame.height
let navigationBarHeight = self.navigationController?.navigationBar.frame.height ?? 0
self.constants.defaultContentInsets.top += statusBarheight + navigationBarHeight
@zwang
zwang / sqlite3.sh
Created March 15, 2016 20:11 — forked from fluidsonic/sqlite3.sh
Make sqlite3 module available in Swift
#!/bin/sh
modulesDirectory=$DERIVED_FILES_DIR/modules
modulesMap=$modulesDirectory/module.modulemap
modulesMapTemp=$modulesDirectory/module.modulemap.tmp
mkdir -p "$modulesDirectory"
cat > "$modulesMapTemp" << MAP
module sqlite3 [system] {
@zwang
zwang / DoubleToStringWithMoreDecimals.swift
Last active March 18, 2016 20:04
Convert double to string in swift with more than default 15 decimals (a bug in swift should be fixed later)
// https://github.com/apple/swift/pull/348
private func getDoubleStrWith16DecimalDigits(value: Double) -> String {
return String(format: "%.16f", value)
}
@zwang
zwang / StringSize.swift
Created November 6, 2016 16:50 — forked from plumhead/StringSize.swift
String extension to find the layout size of a String with specified attributes.
extension String {
func size(withAttributes attrs: [String:AnyObject], constrainedTo box: NSSize) -> NSRect {
let storage = NSTextStorage(string: self)
let container = NSTextContainer(containerSize: NSSize(width: box.width, height: box.height))
let layout = NSLayoutManager()
layout.addTextContainer(container)
storage.addLayoutManager(layout)
storage.addAttributes(attrs, range: NSMakeRange(0, storage.length))
container.lineFragmentPadding = 0.0
let _ = layout.glyphRangeForTextContainer(container)
When you get an error code in with the domain kCFStreamErrorDomainSSL, you can generally find the error codes in SecureTransport.h which is in /System/Library/Frameworks/Security.framework. Here are the error codes:
enum {
errSSLProtocol = -9800, /* SSL protocol error */
errSSLNegotiation = -9801, /* Cipher Suite negotiation failure */
errSSLFatalAlert = -9802, /* Fatal alert */
errSSLWouldBlock = -9803, /* I/O would block (not fatal) */
errSSLSessionNotFound = -9804, /* attempt to restore an unknown session */
errSSLClosedGraceful = -9805, /* connection closed gracefully */
errSSLClosedAbort = -9806, /* connection closed via error */
@zwang
zwang / introrx.md
Created April 4, 2017 17:21 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing