Skip to content

Instantly share code, notes, and snippets.

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

Juwencheng juwencheng

🏠
Working from home
View GitHub Profile
@juwencheng
juwencheng / Dictionary+KeyPath.swift
Created February 5, 2018 09:41
Dictionary+KeyPath
extension Dictionary {
mutating public func setValue(value: Any, forKeyPath keyPath: String) {
var keys = keyPath.components(separatedBy: ".")
guard let first = keys.first as? Key else { print("Unable to use string as key on type: \(Key.self)"); return }
keys.remove(at: 0)
if keys.isEmpty, let settable = value as? Value {
self[first] = settable
} else {
let rejoined = keys.joined(separator: ".")
var subdict: [NSObject : AnyObject] = [:]
# thanks to https://gist.github.com/dergachev/4627207
ffmpeg -i Wave.mov -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > out.gif
@juwencheng
juwencheng / stringLength.m
Created July 24, 2017 09:34
得到NSString可视字符串的长度,比如包含1个emoji表情的字符串,长度应该是1,而不是2。Unicode 的code point 到 UTF-16转换产生的问题。
NSString *s = @"😀";
__block NSInteger count = 0;
[s enumerateSubstringsInRange:NSMakeRange(0, s.length) options:NSStringEnumerationBySentences usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
count++;
}];
NSLog(@"%d", count);
@juwencheng
juwencheng / docker-command-manually.md
Last active August 8, 2017 07:44
常用的docker命令

docker 常用命令查询手册

docker 相关

# 查询 docker 命令
docker --help

# 停止 docker 服务
sudo service docker stop
@juwencheng
juwencheng / GBK2UTF8String.m
Created July 17, 2017 09:12
GBK to UTF8 String
- (NSString *)responseString
{
NSData *data = [self responseData];
if (!data) {
return nil;
}
//明确表示用 gbkEncoding 进行解码
NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
return [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:gbkEncoding];
}
@juwencheng
juwencheng / install-manifest.plist
Created July 17, 2017 08:56 — forked from alexcristea/install-manifest.plist
Over-the-Air Ad Hoc Distribution manifest for iOS8. More about this subject you can find on http://www.informit.com/articles/article.aspx?p=1829415&seqNum=16
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- array of downloads. -->
<key>items</key>
<array>
<dict>
<!-- an array of assets to download -->
@juwencheng
juwencheng / collectionView_paging.m
Created July 13, 2017 07:16
UICollectionView 分页滚动
// original : https://stackoverflow.com/questions/24303883/uicollectionview-with-paging-enable
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
{
CGFloat cellWidth = self.cellWidth;
CGFloat cellPadding = 9;
NSInteger page = (scrollView.contentOffset.x - cellWidth / 2) / (cellWidth + cellPadding) + 1;
@juwencheng
juwencheng / static_array.m
Created July 13, 2017 07:12
如何定义静态的字符串数组,不能用NSArray,得用static array
const static NSString * const strings[] = {
[0] = @"string_1",
[1] = @"string_2",
[2] = @"string_3",
[3] = @"string_4",
// ...
};
@juwencheng
juwencheng / propTypeUsage.jsx
Created June 3, 2017 02:38
react native prop type validation usage
// import the library
import PropTypes from 'prop-types';
// define a component
class Greeting extends React.Component {
render() {
return (
<h1>Hello, {this.props.name}</h1>
);
}
@juwencheng
juwencheng / rnfetch.js
Created May 23, 2017 00:26
React Native Network Fetch Usage
import React, { Component } from 'react'
import { AppRegistry, View, Text, ActivityIndicator, ScrollView, StyleSheet } from 'react-native'
class App extends Component {
state = {
loading: true,
error: false,
posts: [],
}