Clarify Swift standards guidance for agent use
This commit is contained in:
@@ -157,9 +157,18 @@ extension Drawable {
|
|||||||
|
|
||||||
extension Collection {
|
extension Collection {
|
||||||
func chunked(into size: Int) -> [[Element]] {
|
func chunked(into size: Int) -> [[Element]] {
|
||||||
stride(from: 0, to: count, by: size).map {
|
guard size > 0 else { return [] }
|
||||||
Array(self[$0..<Swift.min($0 + size, count)])
|
|
||||||
|
var result: [[Element]] = []
|
||||||
|
var i = startIndex
|
||||||
|
|
||||||
|
while i != endIndex {
|
||||||
|
let j = index(i, offsetBy: size, limitedBy: endIndex) ?? endIndex
|
||||||
|
result.append(Array(self[i..<j]))
|
||||||
|
i = j
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -216,7 +225,8 @@ struct Point {
|
|||||||
|
|
||||||
### 4.2 Use Classes When Needed
|
### 4.2 Use Classes When Needed
|
||||||
|
|
||||||
Use classes for shared mutable state, identity matters:
|
Use classes when identity, shared ownership, or reference semantics matter.
|
||||||
|
Prefer actors for mutable state shared across concurrent tasks:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
class NetworkManager {
|
class NetworkManager {
|
||||||
@@ -271,6 +281,9 @@ class Person {
|
|||||||
|
|
||||||
### 5.2 Closure Capture Lists
|
### 5.2 Closure Capture Lists
|
||||||
|
|
||||||
|
Use capture lists intentionally to avoid retain cycles.
|
||||||
|
Choose `weak` or `unowned` based on lifetime guarantees:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
// Weak capture for optional self
|
// Weak capture for optional self
|
||||||
onComplete = { [weak self] in
|
onComplete = { [weak self] in
|
||||||
@@ -385,6 +398,8 @@ func perform<T>(_ operation: () throws -> T) rethrows -> T {
|
|||||||
|
|
||||||
**Impact:** CRITICAL
|
**Impact:** CRITICAL
|
||||||
|
|
||||||
|
Use actor isolation and `Sendable` to prevent data races across concurrency domains.
|
||||||
|
|
||||||
### 7.1 Async Functions
|
### 7.1 Async Functions
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
@@ -715,13 +730,14 @@ UIView.animate(withDuration: 0.3) {
|
|||||||
- [ ] No implicitly unwrapped optionals (`!`) in data models
|
- [ ] No implicitly unwrapped optionals (`!`) in data models
|
||||||
|
|
||||||
### Memory
|
### Memory
|
||||||
- [ ] Closures use `[weak self]` when capturing self in escaping closures
|
- [ ] Escaping closures capture `self` intentionally; use `[weak self]` or `[unowned self]` to avoid retain cycles when needed
|
||||||
- [ ] Delegate properties are `weak`
|
- [ ] Delegate properties are `weak`
|
||||||
- [ ] No retain cycles between objects
|
- [ ] No retain cycles between objects
|
||||||
|
|
||||||
### Concurrency
|
### Concurrency
|
||||||
- [ ] Async functions used instead of completion handlers
|
- [ ] Async functions used instead of completion handlers
|
||||||
- [ ] Actors protect shared mutable state
|
- [ ] Mutable state shared across concurrency domains is isolated, often with actors
|
||||||
|
- [ ] Types crossing concurrency domains use `Sendable` when appropriate
|
||||||
- [ ] UI updates on `@MainActor`
|
- [ ] UI updates on `@MainActor`
|
||||||
- [ ] Task cancellation checked in long operations
|
- [ ] Task cancellation checked in long operations
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user