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 / RandomRedPacket.swift
Last active March 16, 2018 09:20
带最小金额的随机红包分配方法
/// 随机红包(仅带最小金额)
///
/// - Parameters:
/// - amount: 总额(分)
/// - slice: 分成几份,至少一份
/// - minimun: 每个红包的最小金额(分),默认1分(这和微信目前业务场景是一致的)
func redPacket(ammount: Int, slice: Int = 1, minimum: Int = 1) throws -> [Int] {
guard minimum > 0, slice > 0, ammount >= minimum * slice else {
throw NSError(domain: "com.tencent.wechat.redpacket", code: -1, userInfo: [NSLocalizedDescriptionKey: "无法按要求分配红包"])
return [ammount]
// 坑一:设置样式
// 有效的代码
navigationBar.tintColor = Color.text_light
navigationBar.shadowImage = UIImage.create(UIColor(rgb: 0xE5E5E5))
navigationBar.setBackgroundImage(UIImage.create(UIColor.white), for: .default)
navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: Color.app, NSFontAttributeName : Font.bf20]
navigationBar.setBackgroundImage(UIImage(), for: .default)
// 坑爹的代码
navigationBar.barStyle = .white // 单设有效,若有页面需改变背景,则改完复原就会灰背景————说好的白呢?
// Cause of 'unrecognized selector' crash
// 1. 内存问题,该对象已释放,垃圾内存区域被某个新的对象取代,而该新的对象很可能不认识这个selector
// 2. 头文件加到工程里了,但对应的.m文件未加入到工程设置的“compile sources”里————这个很诡异,Xcode并不会报错,只会运行时崩溃。
@acalism
acalism / String+Unwrap.swift
Last active November 17, 2017 11:43
Alternative for String(describing:) or String(reflecting:)
// MARK: - Optional Unwrap 并转化为字符串
extension String {
enum UnwrapType {
case describing
case reflecting
}
/// 将任意的Optional类型转化为String,并不会用Optional(abc)这样的包裹形式
@acalism
acalism / json-performance-models.swift
Created October 27, 2017 13:17
JSONEncoder, ObjectMapper, HandyJson
let TransformInt64 = TransformOf<Int64, String>(fromJSON: { (value: String?) -> Int64? in
guard let v = value else {
return nil
}
return Int64(value!)
}, toJSON: { (value: Int64?) -> String? in
return String(value ?? 0)
})
let TransformUInt64 = TransformOf<UInt64, String>(fromJSON: { (value: String?) -> UInt64? in
@acalism
acalism / 好担心
Created November 2, 2017 12:54
我mac机上的这些文件被我删了,希望mac不要宕机
ls -l /private/var/folders/92/v28csfjn6rzdvvg60nhyblb40000gn/C/
total 3424
drwx------@ 2 donald staff 68B Feb 7 2017 32HNEEH56A.com.outercorner.osx.Secrets.Helper
drwx------@ 3 donald staff 102B Feb 27 2017 AnZhg.ChineseCalendar
drwx------@ 2 donald staff 68B Feb 8 2017 AnZhg.ChineseCalendarHelper
drwx------ 3 donald staff 102B Sep 25 2016 AppIconMaker
drwx------ 2 donald staff 68B Nov 2 13:49 AudioComponentRegistrar
drwx------ 2 donald staff 68B Dec 22 2014 ComputeCache
drwx------@ 5 donald staff 170B Sep 20 2016 FN2V63AD2J.com.tencent.ScreenCapture2
drwx------@ 3 donald staff 102B Sep 25 2016 FN2V63AD2J.com.tencent.localserver2
@acalism
acalism / LineLimitedTextView.swift
Last active August 11, 2018 13:15
Pitfall in Calculating a line-limited textView's size
textView.attributedText = originalContent
let lineLimit = 5
textView.isEditable = true
textView.isScrollEnabled = false
// eliminate inner paddings, when right is minus number, it seems useless.
// textView.contentInset = UIEdgeInsets(top: 0, left: -5, bottom: 0, right: 0)
textView.textContainer.lineFragmentPadding = 0; // better way
textView.textContainerInset = .zero // default is (8, 0, 8, 0)
textView.textContainer.maximumNumberOfLines = lineLimit // Important condition
@acalism
acalism / Pitfall_in_register.swift
Created November 9, 2017 09:56
Pitfalls in UserNotification
// 在 app.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) 之后收到用户“接受”或“拒绝”及“默拒”后,此委托方法被调用
func application(_ app: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
// 1. 征询“推送许可”时,用户把app切到后台————默拒了推送
// 2. 在系统设置里打开推送,但关掉所有形式的提醒,等价于拒绝推送(得不到token,也收不到推送)
// 3. 关掉 badge, alert 和 sound 时,notificationSettings.types.rawValue == 0 && app.isRegisteredForRemoteNotifications 成立,但能得到token,也能收到推送(锁屏和通知中心也能看到推送)————说明types涵盖并不全面
print(app.isRegisteredForRemoteNotifications, notificationSettings.types) // 对于模拟器来说,由于不能接收推送,所以 isRegisteredForRemoteNotifications 始终为 false
if !app.isRegisteredForRemoteNotifications && notificationSettings.types.rawValue == 0 {
BKPushSDKManager.onRefuseRemoteNotification()
}
}
// 一些设备通用消息 //
struct DeviceInfo {
//获取设备名称 例如:elan的手机
static let deviceName = UIDevice.current.name
//获取系统版本 例如:10.3
static let sysVersion = UIDevice.current.systemVersion
@acalism
acalism / LineLimitedLabel.swift
Last active July 12, 2018 14:48
Pitfalls in calculating label size
// 1. 多行文本
let label = UILabel()
label.font = Font.f14
label.numberOfLines = 2 // 若此处的行数限定为0(即可无数行),则以下面的 limitedToNumberOfLines 为准;否则两个行数中较小的那个会生效
label.text = model.svideoInfo.svideoTitle // 多行文本
let titleRect = label.textRect(forBounds: CGRect(origin: .zero, size: CGSize(width: cellWidth - margins.xInset, height: CGFloat.greatestFiniteMagnitude)), limitedToNumberOfLines: 0)
// 事实上,用sizeThatFits(_:)就好了。