swift5

Using SecRandomCopyBytes in Swift

匆匆过客 提交于 2019-11-27 18:09:55
问题 I want to generate random bytes using SecRandomCopyBytes in Swift 3.0. Here is how I did it in Swift 2.2 private static func generateRandomBytes() -> String? { let data = NSMutableData(length: Int(32)) let result = SecRandomCopyBytes(kSecRandomDefault, 32, UnsafeMutablePointer<UInt8>(data!.mutableBytes)) if result == errSecSuccess { return data!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)) } else { print("Problem generating random bytes") return nil } } In Swift 3,

Swift: 'Hashable.hashValue' is deprecated as a protocol requirement;

老子叫甜甜 提交于 2019-11-26 21:09:04
问题 I've been facing following issue (it's just a warning) with my iOS project. 'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'ActiveType' to 'Hashable' by implementing 'hash(into:)' instead Xcode 10.2 Swift 5 Source Code: public enum ActiveType { case mention case hashtag case url case custom(pattern: String) var pattern: String { switch self { case .mention: return RegexParser.mentionPattern case .hashtag: return RegexParser.hashtagPattern case .url: return

How do I write dispatch_after GCD in Swift 3, 4, and 5?

家住魔仙堡 提交于 2019-11-26 11:30:06
In Swift 2, I was able to use dispatch_after to delay an action using grand central dispatch: var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime, dispatch_get_main_queue(), { // your function here }) But this no longer seems to compile since Swift 3. What is the preferred way to write this in modern Swift? The syntax is simply: // to run something in 0.1 seconds DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // your code here } Note, the above syntax of adding seconds as a Double seems to be a source of

How do I write dispatch_after GCD in Swift 3, 4, and 5?

狂风中的少年 提交于 2019-11-26 02:04:15
问题 In Swift 2, I was able to use dispatch_after to delay an action using grand central dispatch: var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime, dispatch_get_main_queue(), { // your function here }) But this no longer seems to compile since Swift 3. What is the preferred way to write this in modern Swift? 回答1: The syntax is simply: // to run something in 0.1 seconds DispatchQueue.main.asyncAfter(deadline: .now()