質問編集履歴

2

試したことを追加しました

2023/01/06 05:52

投稿

Tanaei0301
Tanaei0301

スコア4

test CHANGED
File without changes
test CHANGED
@@ -38,6 +38,11 @@
38
38
  で試したました。同じエラーが発生しました。
39
39
  別のプロジェクトで実行した時は問題なく動作しました。
40
40
  xcode、シュミレータの再起動をしました。
41
+
42
+ //追加
43
+ 別プロジェクトでもまるまるコピーして実行するとエラーが出ました。
44
+ 一度関数を全てコメントアウトして実行したあと関数を戻すとなぜか正常に動作しました。
45
+ そのコードをこのプロジェクトに戻して実行してみると同じエラーが出ました
41
46
 
42
47
  ### 補足情報(FW/ツールのバージョンなど)
43
48
  昨日まで正常に動作していたのですが今日試したらなぜかエラーが出るようになっていました。

1

一応全文追加しました。

2023/01/06 05:04

投稿

Tanaei0301
Tanaei0301

スコア4

test CHANGED
File without changes
test CHANGED
@@ -47,3 +47,212 @@
47
47
  macOS : 13
48
48
  xcode : 14.1
49
49
 
50
+ 一応全文です
51
+ ```swift
52
+ import Foundation
53
+ import AVFoundation
54
+ import UIKit
55
+
56
+ class FileController{
57
+ let fileManager = FileManager()
58
+ let documentPaths:URL
59
+ let getDateTime = GetDateTime()
60
+
61
+ init(){
62
+ documentPaths = try! fileManager.url(for: .documentDirectory,in: .userDomainMask,appropriateFor: nil, create: false)
63
+ }
64
+
65
+ //条件に準拠するファイル名だけを抽出して日付順に並び替えて返す
66
+ func searchFile()-> (Array<String>)?{
67
+ let urls = documentPaths
68
+ print(urls.path)
69
+ guard var fileNames = try? FileManager.default.contentsOfDirectory(atPath: urls.path) else {
70
+ return nil
71
+ }
72
+ fileNames.removeAll(where: {$0 == ".DS_Store"})
73
+ for (index, country) in fileNames.enumerated().reversed(){
74
+ if country.contains(".m4a") || country.contains(".txt"){
75
+ fileNames.remove(at: index)
76
+ }
77
+ }
78
+
79
+ //取得したファイルを作成日時が近い順に並び替える バブルソート
80
+ var aDate:Date
81
+ var bDate:Date
82
+ let fileCount = fileNames.count
83
+ print(fileCount)
84
+ guard fileCount != 0 && fileCount != 1 else{
85
+ return fileNames
86
+ }
87
+ for i in 0 ..< (fileCount-1){
88
+ for j in (0 ..< (fileCount-1-i)){
89
+ aDate = fileUpDate(fileName: fileNames[j], directName: nil, UDT:true) as! Date
90
+ bDate = fileUpDate(fileName: fileNames[j+1], directName: nil, UDT:true) as! Date
91
+ print(fileNames)
92
+
93
+ if aDate < bDate{
94
+ fileNames.swapAt(j,j+1)
95
+ }
96
+ }
97
+ }
98
+ print(fileNames)
99
+ return fileNames
100
+ }
101
+
102
+
103
+ func open_file(fileName: String ,directName: String?) -> String{
104
+ let docsDirect = documentPaths
105
+ var url_s:URL
106
+ if directName != nil{
107
+ let fileDirect = docsDirect.appendingPathComponent(directName!)
108
+ url_s = fileDirect.appendingPathComponent(fileName)
109
+ }
110
+ else{
111
+ url_s = docsDirect.appendingPathComponent(fileName)
112
+ }
113
+ var fileContents = try? String(contentsOf: url_s, encoding: .utf8)
114
+
115
+ if fileContents == nil{
116
+ fileContents = "データはありませんでした"
117
+ }
118
+ return fileContents!
119
+ }
120
+
121
+ func create_Direct(directName: String) -> Bool{
122
+ let fileManager = FileManager.default
123
+ do{
124
+ //ディレクトリ作成
125
+ let docs = try fileManager.url(for: .documentDirectory,
126
+ in: .userDomainMask,
127
+ appropriateFor: nil, create: false)
128
+ let directPath = docs.appendingPathComponent(directName)
129
+ print(directPath.path)
130
+ try FileManager.default.createDirectory(atPath: directPath.path, withIntermediateDirectories : true, attributes: nil)
131
+
132
+ return true
133
+ }catch{
134
+ print("createDirectNotFound")
135
+ return false
136
+ }
137
+ }
138
+
139
+ //データの保存
140
+ func savetext_File(directName : String? , FileName: String ,data: String) -> Bool{
141
+ let saveDirectPath : URL
142
+ if directName == nil{
143
+ saveDirectPath = documentPaths
144
+ }
145
+ else{
146
+ saveDirectPath = documentPaths.appendingPathComponent(directName!)
147
+ }
148
+
149
+ let savePaths = saveDirectPath.appendingPathComponent(FileName+".txt")
150
+ let Data = data.data(using: .utf8)!
151
+
152
+
153
+ guard fileManager.createFile(atPath: savePaths.path, contents: Data, attributes: nil) else{
154
+ return false
155
+ }
156
+ return true
157
+ }
158
+
159
+
160
+ func newFileNameChecker(checkFileName: String?) -> Bool{
161
+ let existingFileName = searchFile()
162
+ if checkFileName == nil || checkFileName == ""{
163
+ return false
164
+ }
165
+ else{
166
+ for element in existingFileName!{
167
+ if checkFileName == element{
168
+ return false
169
+ }
170
+ }
171
+ }
172
+ return true
173
+ }
174
+
175
+ func fileCounter() -> Int{
176
+ let fileList = self.searchFile()
177
+ guard fileList != nil else{
178
+ return 0
179
+ }
180
+ return fileList!.count
181
+ }
182
+
183
+ func fileUpDate(fileName : String ,directName : String? ,UDT:Bool) -> Any{
184
+ let directUrls:URL
185
+ if directName != nil{
186
+ directUrls = documentPaths.appendingPathComponent(directName!)
187
+ }
188
+ else{
189
+ directUrls = documentPaths
190
+ }
191
+ let urls = directUrls.appendingPathComponent(fileName)
192
+ let attributes = try? fileManager.attributesOfItem(atPath: urls.path)
193
+ if UDT == true{
194
+ return attributes![.modificationDate]!
195
+ }
196
+ else{
197
+ let jp_formatedTime = getDateTime.formateJPtime(UDT: attributes![.modificationDate]!)
198
+ return jp_formatedTime
199
+ }
200
+ }
201
+
202
+ func removeDirect(removeDirectName: String) -> Bool{
203
+ let removeURL = documentPaths.appendingPathComponent(removeDirectName)
204
+ do{
205
+ try fileManager.removeItem(at: removeURL)
206
+ }
207
+ catch{
208
+ return false
209
+ }
210
+ return true
211
+ }
212
+
213
+ func audioFileSave(directName:String)->Bool{
214
+ let saveUrl = forAudioUrlPath()
215
+ let toPath = documentPaths.appendingPathComponent(directName)
216
+ let fullPath = toPath.appendingPathComponent("audio.m4a")
217
+ do{
218
+ try fileManager.moveItem(atPath: saveUrl.path, toPath: fullPath.path)
219
+ }
220
+ catch{
221
+ print(error)
222
+ return false
223
+ }
224
+ return true
225
+ }
226
+
227
+ func forAudioUrlPath() -> URL{
228
+ let docsDirect = documentPaths
229
+ let url = docsDirect.appendingPathComponent("audio.m4a")
230
+ return url
231
+ }
232
+
233
+
234
+ func saveImageFile(directName:String,image:UIImage)-> Bool{
235
+ guard let jpegImage = image.jpegData(compressionQuality: 1.0) else{
236
+ return false
237
+ }
238
+
239
+ let saveDirectPath = documentPaths.appendingPathComponent(directName)
240
+ do{
241
+ try jpegImage.write(to: saveDirectPath.appendingPathComponent("Image.jpg"))
242
+ }
243
+ catch{
244
+ return false
245
+ }
246
+ return true
247
+ }
248
+
249
+ func loadImageFile(directName: String) -> UIImage?{
250
+ let imageFilePath = documentPaths.appendingPathComponent(directName)
251
+ if let imageData = try? Data(contentsOf: imageFilePath.appendingPathComponent("Image.jpg")),let image = UIImage(data: imageData){
252
+ return image
253
+ }
254
+ return nil
255
+ }
256
+ }
257
+ ```
258
+