-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.swift
More file actions
47 lines (40 loc) · 1.26 KB
/
Copy pathArray.swift
File metadata and controls
47 lines (40 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// Array.swift
// DevLog
//
// Created by opfic on 6/19/25.
//
import Foundation
// AppStorage에서 배열을 저장할 수 있도록 해주는 Extension
// @retroactive를 사용하여 후에 애플이 이 Extension의 기능을 제공했을 때 중복되었을 때 에러 방지 및 컴파일러 경고 제거
extension Array: @retroactive RawRepresentable where Element: Codable {
public init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode([Element].self, from: data)
else {
return nil
}
self = result
}
public var rawValue: String {
guard let data = try? JSONEncoder().encode(self),
let result = String(data: data, encoding: .utf8)
else {
return "[]"
}
return result
}
}
extension Array {
func chunked(maxCount: Int) -> [[Element]] {
guard 0 < maxCount else { return [] }
var chunks = [[Element]]()
var startIndex = 0
while startIndex < count {
let endIndex = Swift.min(startIndex + maxCount, count)
chunks.append(Array(self[startIndex..<endIndex]))
startIndex = endIndex
}
return chunks
}
}