C语言由Dennis M. Ritchie在1973年设计和实现。从那以后使用者逐渐增加。到1978年Ritchie和Bell实验室的另一位程序专家Kernighan合写了著名的《The C Programming Language》,将C语言推向全世界,许多国家都出了译本,国内有一些C语言书就是这本书的翻译或者编译。由这本书定义的C语言后来被人们称作K&R C。
// Immutable, and NSError must be Sendable because it conforms to Error in Swift openclassNSError : NSObject, NSCopying, NSSecureCoding, @uncheckedSendable {
/* Domain cannot be nil; dict may be nil if no userInfo desired. */ publicinit(domain: String, code: Int, userInfodict: [String : Any]?=nil) .... }
/// Retrieve the localized description for this error. publicvar localizedDescription: String { get } }
extensionNSError : Error { }
实际上这个协议没有任何内容,因此任意类、结构体、枚举都可以轻松实现Error协议。
定义Swift Error:
使用枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
enumNetworkError: Error { case domainError case decodingError case noDataError var localizedDescription: String { switchself { case .domainError: return"url错误" case .decodingError: return"解析出错" case .noDataError: return"无数据" } } }
由于错误一般是后台接口给出,所以个人觉得使用结构体表示错误,使用是最方便的:
1 2 3 4 5 6 7 8 9 10 11 12 13
structBusinessError: Error { var code ="" var message ="" init(code: String, message: String) { self.code = code self.message = message } var localizedDescription: String { return message } }
The @autoclosure attribute can be applied to a closure parameter for a function, and automatically creates a closure from an expression you pass in. When you call a function that uses this attribute, the code you write isn’t a closure, but it becomes a closure, which can be a bit confusing – even the official Swift reference guide warns that overusing autoclosures makes your code harder to understand.
#ifndef __cplusplus #error Since WCDB is an Objective-C++ framework, for those files in your project that includes WCDB, you should rename their extension .m to .mm. #endif
1.观察者需要实现observeValueForKeyPath:方法,并在该方法中做出相应行为.如果观察者没有实现observeValueForKeyPath:方法,且其父类也没有实现,那么最终会走到NSObject的实现,而NSObject的默认实现就是崩溃:An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.如果观察者本身没有实现observeValueForKeyPath:…方法,但是其父类实现了,那么执行的就是父类的实现,这也是消息的传递过程.