git cherry-pick
git-cherry-pick - Apply the changes introduced by some existing commits.
实际开发中会有这种情况:同时存在 v1、v2 两个分支,且不可合并。然后发现两个分支都存在某 bug,在 v1 中修复,需要合并到 v2,要么手动修改,或者用 git cherry-pick:
git cherry-pick 0ba264a1e666bacc
准爸爸
2013-10-20.
Crash Early, Crash Often
最近开发中的一点感悟。
在开发阶段要尽量多的尽量早的暴露问题,应用 crash 恰恰是暴露问题最直接的方式,方便定位没有考虑到的细节问题。
举个例子,应用中对数据边界通常会进行保护判断,比如数组取值前判断数组长度是否满足。其实在开发阶段完全没必要,要求 array[3] 有值你之前的操作就必须要满足,如果不能达到就要想想哪里出了问题,如果数据源根本不能提供那么就要检查业务设计是否有问题。过分的保护检查不一定是好事。
@zadr's iOS 7 dev tricks
@zadr 在 iOS 7 正式发布几个小时后在 Twitter 上连续发了一些 iOS 7 SDK 小技巧,大部分都是我还不知道的东西,所以摘录一下,版权归 @zadr 完全所有。
iOS 7: -[AVPlayer volume] and -[AVPlayer muted]; ! You can finally programmatically control the volume of playback without private APIs! via
AVPlayer 音量状态。
iOS 7: [CTTelephonyNetworkInfo currentRadioAccessTechnology]; what kind of cellular connection you’re on now. LTE/GPRS/CDMA{Rev 0, …}/etc! via
获取运营商网络类别。
iOS 7: -[NSArray firstObject]; Like -[NSArray lastObject];, only, the other end of the array. via
NSArray 直接取第一个对象。推荐 Underscore.m.
iOS 7: NSData has base64 encoding/decoding support now. And this fancy method, enumerateByteRangesUsingBlock:. via
NSData 有了原生的 base64 方法 base64EncodedStringWithOptions:
。
iOS 7: NSURLComponents: Build a NSURL and have it automatically handle encoding of strings for fragement/path/query/etc. via
NSURLComponents 自动编码处理。
iOS 7: iAd support for prerolls in MPMoviePlayerController. This is more of a heads up than an FYI. via
iAd 支持从前卷广告类型。
iOS 7: JavaScriptCore! Bridge between executing native code and executing JavaScript without needing a UIWebView. via
ObjC 直接操作 JS 的 API,JavaScriptCore 是个里程碑,值得好好学习一番。
iOS 7: MKDistanceFormatter. Localized, unit-specific formatting of distances for both imperial and metric systems. via
MapKit 相关,支持本地化的地点距离换算。
iOS 7: -[MPVolumeView wirelessRouteActive] and -[MPVolumeView wirelessRoutesAvailable] — Customize volume control when AirPlay is available. via
AirPlay 音量控制。
iOS 7: -[UIScrollView keyboardDismissMode]; — Easily recreate Messages.app’s scroll-to-dismiss-keyboard behavior! via
类似系统 Message 的键盘滑动消失效果,UIScrollViewKeyboardDismissModeInteractive
.
iOS 7: UITextView supports inserting and tapping on links. Making your own Twitter client just got that much easier! via
UITextView 支持链接点击,应该是 TextKit 带来的新功能。TextKit 也是一个很值得学习的新东西。
iOS 7: AVCaptureDeviceFormat has video zooming with stabilization and simple control over frame rate/duration! via
视频录制支持缩放,以前不支持?
iOS 7: AVSpeechSynthesis - Speak text to users without requiring VoiceOver to be turned on. via
VoiceOver 相关。
iOS 7: Foundation changed a whole bunch of return types to be
instancetype
instead ofid
. Yay, type safety! via
默认对象返回类型改为 instancetype,自己封装的类库要跟进。
iOS 7: MediaAccessibility. New framework that makes it really easy to work with closed captions and video. via
图片视频功能继续加强。
iOS 7: MFMessageComposeViewController supports adding attachments! Take a photo/video and iMessage it to someone in the same flow. via
邮件分享支持直接添加附件。
iOS 7: AVCaptureMetadataOutput can detect and decode most kinds of barcodes, including QR codes! via
原生支持二维码扫描!
iOS 7: Also fixed some security vulnerabilities and updated root certificates. Check ‘em out, http://support.apple.com/kb/HT5934 via
修复了一些证书相关的安全问题。
iOS 7: SSReadingList. Add Stuff to Safari’s reading list that syncs between your Mac and iOS devices. via
Safari Reading List API。
iOS 7: I don’t even know how to describe it accurately, but, there’s some amazing motion APIs in there to explore. Can’t call out just one. via
CoreMotion API,M7 也是 iPhone 5s 最吸引我的东西。
iOS 7: UIInputView: If you have a custom input field, use this as the root object. All subviews then get tinting and blur effects. via
iOS 7 输入框的模糊效果。
iOS 7: UISimpleTextPrintFormatter: Print attributed strings without having to fall back to CoreGraphics to render content before printing. via
更为简单的打印内容格式化。
iOS 7: -[NSObject decreaseSize:] / -[NSObject increaseSize:]; // called when cmd+ & cmd- are hit, so you can increase/decrease content size. via
外接键盘可控制显示大小,类似 OS X 上的 Cmd+/Cmd-.
iOS 7: dispatch_source_memorypressure_flags_t, for whenever the system’s memory pressure conditions change. Critical <-> Warn <-> Normal. via
GCD 相关,话说 GCD 的使用一直停留在基本层面,要多多深入。
PS: 分享一个 Twitter 搜索技巧: iOS 7 from:zadr 即可搜索 @zadr 所有包含 iOS 7 关键字的 Tweets。最后鄙视一下一些所谓的开发者,完全无视 NDA 的存在,在 iOS 7 SDK 正式发布前通过翻译等方式来刷自己的技术存在感。
NSURLConnection 在页面滑动时继续执行
这篇笔记只是为了清掉 BlogTodos,实际开发中用了 AFN 等是不会遇到这个问题。当然也可以作为原理理解。
首先是 NSRunLoop,我的理解 runloop 就是 iOS 的消息循环处理机制,响应处理各种消息事件。runloop 有不同的执行模式,不同模式下只会响应处理该模式类型的事件。App 运行时会有一个主线程 mainRunLoop,在程序中可以用 [NSRunloop currentRunLoop]
简单获取当前的 runloop。
NSURLConnection 在网络请求的时候(无论主线程还是子线程),如果有点击或滑动界面,网络请求会被暂停执行,直到滑动等操作结束。这是因为 NSURLConnection 默认是 NSDefaultRunLoopMode
,也就是说只会在该模式下执行,当有滑动、点击界面等操作的时候,runloop 会切换到 NSEventTrackingRunLoopMode
来处理界面操作,这时候网络请求就会被暂停执行,直到界面操作结束,runloop 模式切换回去。
解决方法就是设置 NSURLConnection 的执行模式为 NSRunLoopCommonModes
,这样就会在所有模式下持续执行:
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
NSDefaultRunLoopMode - The mode to deal with input sources other than NSConnection objects.
NSRunLoopCommonModes - Objects added to a run loop using this value as the mode are monitored by all run loop modes that have been declared as a member of the set of “common” modes.
参考 NSURLConnection: How to avoid blocked file loading during scrolling of UIScrollView or UITableView
NSInvocation Notes
iOS 中一般用 performSelector
系列方法调用某个对象的方法消息,但是参数过多就不太方便,这时候就可以用 NSInvocation。一个简单的例子:
NSMethodSignature *sig = [self methodSignatureForSelector:@selector(addAlbum:atIndex:)];
NSInvocation *action = [NSInvocation invocationWithMethodSignature:sig];
[action setTarget:self]; //0
[action setSelector:@selector(addAlbum:atIndex:)]; //1
[action setArgument:&deletedAlbum atIndex:2]; //2
[action setArgument:¤tAlbumIndex atIndex:3]; //3
[action retainArguments];
[action invoke];
NSMethodSignature
直译就是方法签名,保存了方法的参数类型和返回值信息 (type information for the arguments and return value of a method)。 通过方法签名信息就可以完整构建一个 invocation,对各个参数进行赋值后激活执行,也就完成了对象方法调用。
再看 NSMethodSignature 一段文档:
Indices begin with 0. The hidden arguments self (of type id) and _cmd (of type SEL) are at indices 0 and 1; method-specific arguments begin at index 2.
NSInvocation 第一步设定 Target(0),第二步设定 Selector(1),然后从 index 2 开始依次对参数赋值,因为 0/1 已被 Target/Selector 占用。要注意赋值的时候传的都是 指针 ,如果赋值参数可能会被释放,要记得 retainArguments。如果需要 NSInvocation 执行后的返回值:
NSString *returnString = nil; //假定返回值类型为 NSString
[action getReturnValue:&returnString];
最后附上 Three20 里用 NSInvocation 实现多参数 performSelector:
https://github.com/facebook/three20/blob/1.0.12/src/Three20Core/Sources/NSObjectAdditions.m#L89
- (id)performSelector:(SEL)selector
withObject:(id)p1
withObject:(id)p2
withObject:(id)p3
withObject:(id)p4
withObject:(id)p5
{
NSMethodSignature *sig = [self methodSignatureForSelector:selector];
if (sig) {
NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];
[invo setTarget:self];
[invo setSelector:selector];
[invo setArgument:&p1 atIndex:2];
[invo setArgument:&p2 atIndex:3];
[invo setArgument:&p3 atIndex:4];
[invo setArgument:&p4 atIndex:5];
[invo setArgument:&p5 atIndex:6];
[invo invoke];
if (sig.methodReturnLength) {
id anObject;
[invo getReturnValue:&anObject];
return anObject;
} else {
return nil;
}
} else {
return nil;
}
}
MBP 不能充电解决
MBP 电源适配器不能充电,各种插拔都无效,真以为要悲剧了,搜到一个办法:
苹果的电源适配器在电流过大或电压不稳的情况下会自动启用保护机制,切断电流以保护电脑,所以才会充不进电。而解决方法非常简单,只需将 MagSafe 电源适配器拔出电源,静置 60秒以上,就可以重置电源适配器。
简单重置后果然有效,留记。
Dev 放权
最近的一点反思。
着重技术往 Dev Leader 转型的过程中,老是做不到技术放权。新需求和问题过来第一反应往往是自己怎么去做,原因有二:
- 不自信。自己技术不懂不熟,所以想借实战强化;刚学到一个新技术点想实用一次,毕竟技术性调研和实际项目应用差别还是很大。这种情况多出现在新项目。
- 不信任。对事不对人的说,项目紧,其他人对业务不是特别熟悉,所以很不放心把东西交出去做。
结果就是自己排了很多 tickets,很容易拖累进度,而其他人对业务继续不熟悉。
以后要加多 Code Reivew,前期系统设计多参与,后期实现时多跟进把控,具体实现大家一起完成,强化整体架构能力。
StatusBar in iOS 7
iOS 7 下状态栏默认是白底黑字,如果应用是黑色背景整个状态栏就啥也看不见。解决办法:
- plist 设置
UIViewControllerBasedStatusBarAppearance
NO. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
多 UIScrolllView 下点击状态栏回到顶部
iOS 下 UITableView/UIScrollView 有个特性:点击状态栏回到顶部。如果当前 view 下有多个 scrollView,或者多个 tableView 嵌套,点击回到顶部就无效,因为系统不知道该响应哪个,索性就全部禁用。文档:
On iPhone, we execute this gesture only if there’s one on-screen scroll view with
scrollsToTop
== YES. If more than one is found, none will be scrolled.
找到了原因解决也就很简单:只保留需要点击回到顶部 scrollView.scrollsToTop = YES,其他全部禁用。
就这么简单的 tip 我也是最近才知道,而解决办法就在官方文档里,so RTFM first.