This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 原问题: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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 需要在 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 也是可以的 |