* Add guide on comments

* Add Casting convention

* Delete the runtime cast section

Co-authored-by: Chen Yang <yangche@microsoft.com>
This commit is contained in:
Chen Yang 2021-07-30 09:41:22 -07:00 коммит произвёл GitHub
Родитель 4c80350ee5
Коммит eab3ad8e6a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 40 добавлений и 0 удалений

Просмотреть файл

@ -15,6 +15,7 @@ The Swift coding conventions guide documents many best practices for writing Swi
* [Immutability](Immutability.md)
* [Naming](Naming.md)
* [Property Observers](PropertyObservers.md)
* [Type Casting](TypeCasting.md)
* [Type Inference](TypeInference.md)
# Legal Notices

39
TypeCasting.md Normal file
Просмотреть файл

@ -0,0 +1,39 @@
# Type Casting
In general, follow the [official guidelines](https://docs.swift.org/swift-book/LanguageGuide/TypeCasting.html).
## Convention - Static casting
Use keyword `as` for static casting. Only use static casting when automatic type casting is not available or when the compiler's inferred type does not match the intended type.
## Rationale
Compiler can infer most types correctly so static casting is not necessary in most cases.
## Examples
``` swift
// Bad: unnecessary type casting
let padding = 42 as CGFloat
// Good: use type annotations instead
let padding: CGFloat = 42
```
### Necessary type casting
``` swift
// setTelemetryID is an Objective-C function that takes an NSNumber as the argument
// id is of type Int, the casting to NSNumber is not automatic
button.setTelemetryID(id as NSNumber)
let x = 10
switch x {
// Without the type cast, 1...Int.max would be interpreted as Range type
case 1...Int.max as ClosedInterval:
...
case Int.min..<0 as HalfOpenInterval:
...
case 0:
...
}
```