!
去强制解包
if let
判断
推荐
if let optionalValue = optionalValue {
/* ... */
}
不推荐
if optionalValue != nil {
let value = optionalValue!
/* ... */
}
if let
合并
推荐
var subview: UIView?
var volume: Double?
if let subview = subview, let volume = volume {
/* ... */
}
不推荐
var optionalSubview: UIView?
var volume: Double?
if let unwrappedSubview = optionalSubview {
if let realVolume = volume {
/* ... */
}
}
推荐
// 使用if let as?判断
if let text = text as? String {
/* ... */
}
// 使用if let try 或者 try?
if let test = try aTryFuncton() {
/* ... */
}
推荐
var names: [String] = []
var lookup: [String: Int] = [:]
不推荐
var names = [String]()
var names: Array<String> = [String]() / 不够简洁
var lookup = [String: Int]()
var lookup: Dictionary<String, Int> = [String: Int]() // 不够简洁
推荐
let value = 1
let text = "xxxx"
不推荐
let value: int = 1
let text: String = "xxxx"
==
和!=
判断内容
上是否一致
推荐
// String类型没有-isEqualToString方法,用==判断是否相等
let str1 = "netease"
let str2 = "netease"
if str1 == str2 {
// is true
/* ... */
}
// 对于自定义类型,判断内容是否一致,需要实现Equatable接口
class BookItem {
let bookId: String
let title: String
init (bookdId: String, title: String) {
self.bookId = bookId
self.title = title
}
}
extension BookItem: Equatable {
}
func ==(lhs: bookItem, rhs: book: BookItem) -> bool {
return lhs.bookId == rhs.bookId
}
===
和!==
判断class类型对象是否同一个引用,而不是用 ==
和!=
推荐
if tenEighty === alsoTenEighty {
/* ... */
}
if tenEighty !== notTenEighty {
/* ... */
}
不推荐
/// 错误写法!
if tenEighty == alsoTenEighty {
/* ... */
}
/// 错误写法!
if tenEighty != notTenEighty {
/* ... */
}
推荐
class TestTabelViewCell: UITableViewCell {
static let kCellHeight = 80.0
/* ... */
}
// uses
let cellHeight = TestTabelViewCell.kCellHeight
不推荐
let kCellHeight = 80.0
class TestTabelViewCell: UITableViewCell {
/* ... */
}
// uses
let cellHeight = kCellHeight
推荐
class MyViewController: UIViewController {
// class stuff here
}
// MARK: - UITableViewDataSource
extension MyViewController: UITableViewDataSource {
// table view data source methods
}
// MARK: - UIScrollViewDelegate
extension MyViewController: UIScrollViewDelegate {
// scroll view delegate methods
}
不推荐
class MyViewController: UIViewController, UITableViewDataSource, UIScrollViewDelegate {
// all methods
}
推荐
UIView.animate(withDuration: 1.0) {
self.myView.alpha = 0
}
不推荐
UIView.animate(withDuration: 1.0, animations: {
self.myView.alpha = 0
})
break
关键词
推荐
let someCharacter: Character = "z"
switch someCharacter {
case "a":
print("The first letter of the alphabet")
case "z":
print("The last letter of the alphabet")
default:
print("Some other character")
}
// Prints "The last letter of the alphabet"
private
;如果在文件内可修改,则标记为fileprivate
推荐
class User {
fileprivate var name = "private"
}
extension User {
var accessPrivate: String {
// 同一文件内,可访问fileprivate类型属性,但不可访问private类型属性
return name
}
}
public
,否则标明为open
推荐
// User不可被外部继承
public class User {
private var name = "private"
}
// User可被外部继承
open class User {
private var name = "private"
}
@IBOutlet
、IBAction
、@discardableResult
、static
等关键字在最前面
@discardableResult
标明
推荐
@discardableResult
func printMessage(message: String) -> String {
let outputMessage = "Output : \(message)"
print(outputMessage)
return outputMessage
}
推荐
func login(with username: String?, password: String?) throws -> LoginError {
guard let username = contextusername else {
throw .noUsername
}
guard let password = password else {
throw .noPassword
}
/* login code */
}
不推荐
func login(with username: String?, password: String?) throws -> LoginError {
if let username = username {
if let password = inputDatapassword {
/* login code */
} else {
throw .noPassword
}
} else {
throw .noUsername
}
}
weak
修饰
推荐
public weak var dataSource: UITableViewDataSource?
public weak var delegate: UITableViewDelegate?
推荐
resource.request().onComplete { [weak self] response in
guard let strongSelf = self else {
return
}
let model = strongSelf.updateModel(response)
strongSelf.updateUI(model)
}
不推荐
// 不推荐使用unowned
// might crash if self is released before response returns
resource.request().onComplete { [unowned self] response in
let model = self.updateModel(response)
self.updateUI(model)
}
不推荐
// deallocate could happen between updating the model and updating UI
resource.request().onComplete { [weak self] response in
let model = self?.updateModel(response)
self?.updateUI(model)
}
推荐
class TestManager {
static let shared = TestManager()
/* ... */
}
推荐
@objc SwiftClass {
/* ... */
}
@objc protocol SwiftProtocol {
/* ... */
}
Swift语言本身对Runtime并不支持,需要在属性或者方法前添加dynamic修饰符才能获取动态型,继承自NSObject的类其继承的父类的方法也具有动态型,子类的属性和方法也需要加dynamic才能获取动态性
相关阅读:【Swift编码规范】(上篇)
网易云新用户大礼包:https://www.163yun.com/gift
本文来自网易实践者社区,经作者胡波授权发布。