回答編集履歴

5

訂正

2020/05/30 22:39

投稿

退会済みユーザー
test CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
 
10
10
 
11
+ ```swift
12
+
11
13
  import UIKit
12
14
 
13
15
 
@@ -26,7 +28,7 @@
26
28
 
27
29
 
28
30
 
29
- print(fileNames.newFileName)
31
+ print(fileNames.newFileName) // => "fileName_004"
30
32
 
31
33
  }
32
34
 
@@ -36,7 +38,7 @@
36
38
 
37
39
  extension String {
38
40
 
39
-
41
+ // 同一接頭句でないと連番の意味が薄れるが念の為。
40
42
 
41
43
  var numberStr: String {
42
44
 
@@ -114,6 +116,8 @@
114
116
 
115
117
 
116
118
 
119
+ ```
120
+
117
121
  ---
118
122
 
119
123
 

4

訂正

2020/05/30 22:39

投稿

退会済みユーザー
test CHANGED
@@ -2,7 +2,119 @@
2
2
 
3
3
 
4
4
 
5
-
5
+ ---
6
+
7
+ 備忘目的追記
8
+
9
+
10
+
11
+ import UIKit
12
+
13
+
14
+
15
+ class ViewController: UIViewController {
16
+
17
+
18
+
19
+ let fileNames = ["03", "fileName_094", "fileName_056", "BBB", "45", "fileName_001", "fileName_002", "fileName_005"]
20
+
21
+
22
+
23
+ override func viewDidLoad() {
24
+
25
+ super.viewDidLoad()
26
+
27
+
28
+
29
+ print(fileNames.newFileName)
30
+
31
+ }
32
+
33
+ }
34
+
35
+
36
+
37
+ extension String {
38
+
39
+
40
+
41
+ var numberStr: String {
42
+
43
+
44
+
45
+ guard let regex = try? NSRegularExpression(pattern: "^*[\d]+$", options: []) else {
46
+
47
+ return ""
48
+
49
+ }
50
+
51
+
52
+
53
+ let results = regex.matches(in: self, options: [], range: NSMakeRange(0, self.count))
54
+
55
+
56
+
57
+ var str: String = ""
58
+
59
+
60
+
61
+ for i in 0 ..< results.count{
62
+
63
+ for j in 0 ..< results[i].numberOfRanges{
64
+
65
+ let range = results[i].range(at: j)
66
+
67
+ str.append((self as NSString).substring(with: range))
68
+
69
+ }
70
+
71
+ }
72
+
73
+ return str
74
+
75
+ }
76
+
77
+ }
78
+
79
+
80
+
81
+ extension Array where Element == String {
82
+
83
+
84
+
85
+ var newFileName: String {
86
+
87
+
88
+
89
+ let prefixStr: String = "fileName_"
90
+
91
+
92
+
93
+ let maxDigit = self.map { $0.numberStr.count }.max() ?? 1
94
+
95
+
96
+
97
+ let suffixNumbers: [Int] = self.map { Int($0.numberStr) ?? 0 }.sorted(by: <)
98
+
99
+
100
+
101
+ let newSuffixNumber: String
102
+
103
+ = String(format: "%0(maxDigit)d", ((1...).lazy.map { $0 }.first { !suffixNumbers.contains($0) }) ?? 0 )
104
+
105
+
106
+
107
+ return prefixStr + newSuffixNumber
108
+
109
+
110
+
111
+ }
112
+
113
+ }
114
+
115
+
116
+
117
+ ---
6
118
 
7
119
 
8
120
 

3

訂正

2020/05/30 22:36

投稿

退会済みユーザー
test CHANGED
@@ -104,6 +104,16 @@
104
104
 
105
105
 
106
106
 
107
+ // 配列が空だったら? -> 1を返す?
108
+
109
+      // ファイル名に数値がなかったら -> 1を返す?
110
+
111
+ // 桁数調整(入ってくる連番の桁数とのかねあいも考慮)
112
+
113
+ // 最終的にはStringを返す?
114
+
115
+
116
+
107
117
  let numArray: [Int] = self.map { ($0.number ?? 0) }.sorted(by: <)
108
118
 
109
119
 

2

訂正

2020/05/30 10:23

投稿

退会済みユーザー
test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
 
18
18
 
19
- let fileNames = ["AA003", "094", "556", "BBB", "DE45", "BB001", "CCDFR002", "005", "kaegei990785304743", "08307320683"]
19
+ let fileNames = ["AA003y", "094", "556", "BBB", "DE45", "BB001", "CCDFR002", "005", "kaegei990785304743", "08307320683"]
20
20
 
21
21
 
22
22
 
@@ -28,7 +28,7 @@
28
28
 
29
29
 
30
30
 
31
- print("num: (fileNames.fileNumber)") => num: Optional("4")
31
+ print("num: (fileNames.fileNumber)") => num: Optional("3")
32
32
 
33
33
 
34
34
 
@@ -48,7 +48,7 @@
48
48
 
49
49
  var number: Int? {
50
50
 
51
-
51
+ // 正規表現わかんない
52
52
 
53
53
  guard let regex = try? NSRegularExpression(pattern: "^*[\d]+$", options: []) else {
54
54
 

1

追記

2020/05/30 10:16

投稿

退会済みユーザー
test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
 
18
18
 
19
- let fileNames = ["AA003", "094", "556", "BBB", "DE45", "BB001", "CCDFR002", "005"]
19
+ let fileNames = ["AA003", "094", "556", "BBB", "DE45", "BB001", "CCDFR002", "005", "kaegei990785304743", "08307320683"]
20
20
 
21
21
 
22
22