Skip to content

Instantly share code, notes, and snippets.

View acalism's full-sized avatar
🏠
Working from home

Dawn Song acalism

🏠
Working from home
  • Tencent, Alibaba
  • Shenzhen City, Guangdong Province, China
View GitHub Profile
@acalism
acalism / xml and json viewer in terminal
Last active February 1, 2016 07:50
XML 和 JSON 在命令行下的可读性
Pretty-printing JSON and XML on Mac OSX
Like my colleagues at Avisi I’m an avid OSX user. One of the reasons I like OSX is it gives you the power of Unix right under your fingertips while still providing a nice user interface.
Now suppose you’re working on RESTful services and you need to verify or debug some JSON output. Digging through an unformatted JSON string can be a real pain. To make a JSON string human readable open up the Terminal and type:
cat unformatted.json | python -m json.tool > formatted.json
This will pretty-print the contents of “unformatted.json” to a new file called “formatted.json”. For this to work you need to have at least Python 2.6 installed. Which is the case when your running Snow Leopard or higher.
The above command assumes you have a file containing the JSON content. When you’re debugging it’s more likely you copied a JSON string to your clipboard from a (remote) log file. To pretty-print the JSON content on your clipboard type:
pbpaste | python -m json.tool > formatted.json
L
@acalism
acalism / gestureRecognizer
Created February 22, 2016 09:32
gesture如何排除子view
// http://stackoverflow.com/a/18886352/1107242
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (touch.view != _mySuperView) { // accept only touchs on superview, not accept touchs on subviews
return NO;
}
return YES;
}
@acalism
acalism / gist:b3167f96b0f477515bac
Created March 25, 2016 01:12
跟Xcode有关的几个目录
// 1. Xcode->preferences->Components下载后存在哪?(runtime 和 document)
// /Library/Developer/CoreSimulator/Profiles/Runtimes
// ~/Library/Developer/CoreSimulator/Devices/
// ~/Library/Developer/Shared/Documentation/DocSets/
// 2. 虚拟设备的配置文件和安装的app,建议用Xcode的Devices工具管理,也可以用 xcrun simctl 命令管理
// ~/Library/Developer/CoreSimulator/Devices/
// 3. 移动设备接入电脑后,会自动生成这些。删除后下次会重建————没必要删除,除非你确认这个设备再不会使用了
// ~/Library/Developer/Xcode/iOS\ DeviceSupport/
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
let g = ESGlobal.sharedInstance()
segmentedControl.selectedSegmentIndex = g.networkType()
textView0.text = g.debugInfo().description
@acalism
acalism / ViewSnapshot.m
Created June 29, 2016 10:49
Snapshot any view for iOS
// Might be you will need to snapshot a particular view into an image. Previously we do like this:
// #import <QuartzCore/QuartzCore.h>
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *copied = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
@acalism
acalism / Xcode-wrap-around-search
Created August 26, 2016 03:17
Xcode's text search how to wrap around?
@acalism
acalism / observeAllNotification.swift
Created April 14, 2017 05:27
observe all notification
// 监听窗口事件
// NSNotification.Name.UIWindowDidBecomeHidden = "UIWindowDidBecomeHiddenNotification"
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIWindowDidResignKey, object: nil, queue: .main) { (notice) in
guard let w = notice.object as? UIWindow else { return }
if w == self.window {
lockDeviceInterfaceToLandscape = true
app.statusBarOrientation = .landscapeRight
} else {
lockDeviceInterfaceToLandscape = false
app.statusBarOrientation = .portrait
@acalism
acalism / ProgressSlider.swift
Last active June 16, 2017 11:19
如何调整UISlider的thumbImage的size?
class ProgressSlider: UISlider {
override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
let max = maximumValue == 0 ? 1.0 : maximumValue
let side: CGFloat = 8
let centerX: CGFloat = rect.width * CGFloat(value / max)
let r = CGRect(x: ceil(centerX) - side / 2, y: rect.midY - side / 2, width: side, height: side)
return r
}
override func layoutSubviews() {
protocol MakeSelf {
init()
}
class MS: MakeSelf {
static let shared = MS()
required init() {
//
}
}
@acalism
acalism / EnumCases.swift
Created July 5, 2017 07:57
如何枚举enum类型的所有case
protocol EnumCollection : Hashable {}
extension EnumCollection {
static func cases() -> AnySequence<Self> {
typealias S = Self
return AnySequence { () -> AnyIterator<S> in
var raw = 0
return AnyIterator {
let current : Self = withUnsafePointer(to: &raw) { $0.withMemoryRebound(to: S.self, capacity: 1) { $0.pointee } }
guard current.hashValue == raw else { return nil }
raw += 1