Skip to content

Commit

Permalink
[add] propertyWrapper in swift keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
RinniSwift committed May 17, 2021
1 parent 25a32ac commit 0685e29
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Expand Up @@ -124,7 +124,18 @@
>    Dependancy injection\
>    Notification center
>
> **[Swift Keywords](https://github.com/RinniSwift/Computer-Science-with-iOS/blob/main/swiftKeywords.md)**
> **[Swift Keywords](https://github.com/RinniSwift/Computer-Science-with-iOS/blob/main/swiftKeywords.md)**\
>    `@objc`\
>    `@escaping`\
>    `@available`\
>    `@final`\
>    `@discardableResult`\
>    `@propertyWrapper`\
>    Declarations\
>    Type aliases\
>    Stored Type Properties and Computed Properties\
>    Instance Properties and Type Properties\
>    Identifiers
>
> **[Collection Protocols](https://github.com/RinniSwift/Computer-Science-with-iOS/blob/main/collectionProtocols.md)**
>
Expand Down
29 changes: 28 additions & 1 deletion swiftKeywords.md
Expand Up @@ -7,7 +7,7 @@
- `available`
- `final`
- `@discardableResult`

- `@propertyWrapper`
- `instance properties` and `type properties`
- `class` and `static`
- `lazy` stored properties
Expand All @@ -23,6 +23,33 @@ Add this attribute when declaring a function with a return type but telling the

This will make it available to call directly without needing to store in a variable.

### `@propertyWrapper`
Introduced in Swift 5.1, this enables property values to be wrapped by using a custom type. Here's an example use case:

```swift
@propertyWrapper
struct Trimmed {
private var str: String = ""
var wrappedValue: String {
get {
str
}
set {
str = newValue.trimmingCharacters(in: .whiteSpacesAndNewLines)
}
}
init(wrappedValue: String) {
self.wrappedValue = wrappedValue
}
}

@Trimmed var firstName = ""
@Trimmed var lastName = ""
```
*code snippet from Mastering Swift 5.3 - Sixth Addition by Jon Hoffman*

Now when setting the `firstName` and `lastName` variables, it will automatically remove the white spaces.

---

## Declarations
Expand Down

0 comments on commit 0685e29

Please sign in to comment.