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
private let kEllipsesCharacter = "\u{2026}"
/// This componet is designed to resolve those problems:
/// 1. Calculate frame.
/// 2. TouchableLinks.
/// 3. Line/Height limits and followed by "..."/"...全文"/... .
class BKLabel: UIView {
/// 文本内容
var attributedText: NSAttributedString? {
@acalism
acalism / min_max.swift
Created March 23, 2018 09:57
同时找到最大和最小值
// 原问题:https://blog.csdn.net/harry_lyc/article/details/7715639
func findMinMax<T: Comparable>(_ array: [T]) throws -> (T, T) {
guard array.count > 0 else {
throw NSError()
}
var min: T = array[0], max: T = array[0]
// 比较次数 O(2n)
// for value in array {
// if value < min {
@acalism
acalism / BasicStatements.swift
Created March 19, 2018 07:08
Basic Statements
// 1. for 循环条件筛选
let urlStrings: [String?] = ["https://baidu.com", nil, "https://im.qq.com"]
for case let urlString? in urlStrings where !urlString.isEmpty {
if let u = URL(string: url) {
print(u)
}
}
// 2.
@acalism
acalism / NotificationViewController.swift
Created March 6, 2018 07:39
App Extension: Notifiation Content
import UIKit
import UserNotifications
import UserNotificationsUI
// 可考虑在 info.plist 里限定初始高与屏幕高的比例,比如 0.3,但不建议使用1。体验一下加载图片的效果就知道为什么了
class NotificationViewController: UIViewController, UNNotificationContentExtension {
//@IBOutlet var label: UILabel?
@IBOutlet var imageView: UIImageView! // 建议在storyboard限定图片最大高度和最大宽度,避免图片被截断
//@IBOutlet var containerView: UIView!
@acalism
acalism / NotificationServiceDemo.swift
Created March 6, 2018 07:32
App Extension: Notification Service
import UserNotifications
import MobileCoreServices
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
@acalism
acalism / String+NSRange.swift
Last active March 6, 2018 07:42
Sometimes, String's Range<String.Index> and NSString's NSRange is not compatible. Here is an example.
import Foundation
// Because NSString using UTF-16, so the following answer is suitable.
// Update for Swift 4 (Xcode 9):
extension String {
/// 将 Range 转为 NSRange
///
/// - Parameter range: 要转换的range
/// - Returns: 转换后的结果(如果range合法,那么nsRange就不会是nil)
// CFBundleIcons
// https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-SW13
if #available(iOS 10.3, *) {
UIApplication.shared.setAlternateIconName("AppIcon-alternate", completionHandler: { (error) in
//
})
} else {
// no Fallback on earlier versions
}
@acalism
acalism / UTType.swift
Created February 27, 2018 07:32
What is a live photo file?
import Foundation
import MobileCoreServices
if let t = UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension, "mp4" as CFString, nil)?.takeRetainedValue() as? [CFString] {
print(t)
print(UTTypeConformsTo(t[0], kUTTypeMovie), "\n")
}
if let t = UTTypeCreateAllIdentifiersForTag(kUTTagClassMIMEType, "application/json" as CFString, nil)?.takeRetainedValue() as? [CFString] {
@acalism
acalism / InstantMap.swift
Last active February 27, 2018 06:39
Convert to chain-invocation
protocol InstantMap {
func instantMap<T>(_ transform: (Self)->T) -> T
func instantFlatMap<T>(_ transform: (Self)->T?) -> T?
}
extension InstantMap {
func instantMap<T>(_ transform: (Self)->T) -> T {
return transform(self)
}
func instantFlatMap<T>(_ transform: (Self)->T?) -> T? {
@acalism
acalism / Digest.swift
Last active March 1, 2018 11:09
md5 计算。被注释掉的部分是较冗长的写法
// 需要在 xxx-Bridging-Header.h 中引入 CommonCrypto
// #import <CommonCrypto/CommonCrypto.h> // add to your xxx-Bridging-Header.h
// 所以在 playground 中无法测试下面的代码 ( NOT work with Playground )
// 阅读代码时注意以下几点:
// 1. Data 本质是 UInt8 数组,所以 String.utf8.count 与 Data.count 是相等的(String 转换成 Data 前后)
// 2. 如果一个C方法接受的参数类型是 void*,即 Swift 里的 UnsafeRawPointer,那么它也接受 UnsafePointer<T> 类型
// 3. 如果某个方法接受 UnsafePointer<UInt8> 类型参数,那么直接填入 String 也是可以的