{"id":3933,"date":"2025-05-31T13:30:01","date_gmt":"2025-06-04T06:04:00","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3933"},"modified":"2025-06-04T13:30:01","modified_gmt":"2025-06-04T06:04:00","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e6%96%87%e4%bb%b6%e7%b3%bb%e7%b5%b1%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8swift%e4%b8%ad%e7%9a%84%e6%96%87%e4%bb%b6%e7%b3%bb%e7%b5%b1%e6%93%8d%e4%bd%9c\/","title":{"rendered":"\u6df1\u5165\u5b78\u7fd2Swift\u4e2d\u7684\u6587\u4ef6\u7cfb\u7d71\u64cd\u4f5c\uff1aFileManager\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, FileManager, \u6587\u4ef6\u7cfb\u7edf\u64cd\u4f5c, iOS\u5f00\u53d1, \u521b\u5efa\u6587\u4ef6, \u8bfb\u53d6\u6587\u4ef6, \u5220\u9664\u6587\u4ef6, \u76ee\u5f55\u64cd\u4f5c\"><\/p>\n<h1>\u6df1\u5165\u5b78\u7fd2Swift\u4e2d\u7684\u6587\u4ef6\u7cfb\u7d71\u64cd\u4f5c\uff1aFileManager\u7684\u5b8c\u6574\u6307\u5357<\/h1>\n<p>Swift\u662f\u4e00\u7a2e\u5ee3\u53d7\u6b61\u8fce\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u4e3b\u8981\u7528\u65bc\u958b\u767ciOS\u3001macOS\u548cwatchOS\u61c9\u7528\u7a0b\u5e8f\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u9032\u884c\u6587\u4ef6\u7cfb\u7d71\u64cd\u4f5c\u5c0d\u65bc\u958b\u767c\u8005\u81f3\u95dc\u91cd\u8981\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u7d22\u5982\u4f55\u5229\u7528FileManager\u985e\u4f86\u9032\u884c\u6587\u4ef6\u548c\u76ee\u9304\u7684\u5275\u5efa\u3001\u8b80\u53d6\u548c\u522a\u9664\u7b49\u57fa\u672c\u64cd\u4f5c\u3002<\/p>\n<h2>FileManager\u985e\u6982\u8ff0<\/h2>\n<p>FileManager\u662f\u4e00\u500b\u5f37\u5927\u7684\u5167\u7f6e\u985e\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4ee5\u64cd\u4f5c\u6587\u4ef6\u548c\u76ee\u9304\u3002\u5b83\u80fd\u5920\u8b93\u60a8\u8f15\u9b06\u57f7\u884c\u5982\u5275\u5efa\u3001\u8b80\u53d6\u3001\u522a\u9664\u6587\u4ef6\u548c\u76ee\u9304\u7684\u4efb\u52d9\uff0c\u4e26\u6aa2\u67e5\u5b83\u5011\u7684\u5b58\u5728\u6027\u3002<\/p>\n<h2>\u5275\u5efa\u6587\u4ef6\u548c\u76ee\u9304<\/h2>\n<p>\u8981\u5275\u5efa\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528FileManager\u7684`createFile(atPath:contents:attributes:)`\u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u63a5\u53d7\u6587\u4ef6\u8def\u5f91\u548c\u6587\u4ef6\u5167\u5bb9\u4f5c\u70ba\u53c3\u6578\u3002\u4ee5\u4e0b\u4ee3\u78bc\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5275\u5efa\u4e00\u500b\u540d\u70ba\u201ctest.txt\u201d\u7684\u6587\u4ef6\uff0c\u5167\u5bb9\u70ba\u201cHello World\u201d\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet filePath = &#8220;test.txt&#8221;<br \/>\nlet content = &#8220;Hello World&#8221;.data(using: .utf8)<\/p>\n<p>do {<br \/>\n    try FileManager.default.createFile(atPath: filePath, contents: content)<br \/>\n    print(&#8220;File created successfully&#8221;)<br \/>\n} catch {<br \/>\n    print(&#8220;Error creating file: \\(error)&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u8981\u5275\u5efa\u76ee\u9304\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528`createDirectory(atPath:withIntermediateDirectories:attributes:)`\u65b9\u6cd5\u3002\u4ee5\u4e0b\u4ee3\u78bc\u5275\u5efa\u4e00\u500b\u540d\u70ba\u201ctest\u201d\u7684\u76ee\u9304\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet directoryPath = &#8220;test&#8221;<\/p>\n<p>do {<br \/>\n    try FileManager.default.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)<br \/>\n    print(&#8220;Directory created successfully&#8221;)<br \/>\n} catch {<br \/>\n    print(&#8220;Error creating directory: \\(error)&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u8b80\u53d6\u6587\u4ef6\u548c\u76ee\u9304<\/h2>\n<p>\u8981\u8b80\u53d6\u6587\u4ef6\u5167\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528FileManager\u7684`contents(atPath:)`\u65b9\u6cd5\u3002\u4ee5\u4e0b\u4ee3\u78bc\u793a\u4f8b\u8b80\u53d6\u201ctest.txt\u201d\u6587\u4ef6\u7684\u5167\u5bb9\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet filePath = &#8220;test.txt&#8221;<\/p>\n<p>do {<br \/>\n    let content = try String(contentsOfFile: filePath, encoding: .utf8)<br \/>\n    print(&#8220;File content: \\(content)&#8221;)<br \/>\n} catch {<br \/>\n    print(&#8220;Error reading file: \\(error)&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u8981\u8b80\u53d6\u76ee\u9304\u4e2d\u7684\u5167\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528`contentsOfDirectory(atPath:)`\u65b9\u6cd5\u3002\u4ee5\u4e0b\u4ee3\u78bc\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u7372\u53d6\u201ctest\u201d\u76ee\u9304\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u548c\u5b50\u76ee\u9304\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet directoryPath = &#8220;test&#8221;<\/p>\n<p>do {<br \/>\n    let contents = try FileManager.default.contentsOfDirectory(atPath: directoryPath)<br \/>\n    print(&#8220;Directory contents: \\(contents)&#8221;)<br \/>\n} catch {<br \/>\n    print(&#8220;Error reading directory: \\(error)&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u522a\u9664\u6587\u4ef6\u548c\u76ee\u9304<\/h2>\n<p>\u82e5\u8981\u522a\u9664\u6587\u4ef6\u6216\u76ee\u9304\uff0c\u53ef\u4ee5\u4f7f\u7528`removeItem(atPath:)`\u65b9\u6cd5\u3002\u4ee5\u4e0b\u4ee3\u78bc\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u522a\u9664\u201ctest.txt\u201d\u6587\u4ef6\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet filePath = &#8220;test.txt&#8221;<\/p>\n<p>do {<br \/>\n    try FileManager.default.removeItem(atPath: filePath)<br \/>\n    print(&#8220;File deleted successfully&#8221;)<br \/>\n} catch {<br \/>\n    print(&#8220;Error deleting file: \\(error)&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u6aa2\u67e5\u6587\u4ef6\u548c\u76ee\u9304\u662f\u5426\u5b58\u5728<\/h2>\n<p>\u8981\u78ba\u8a8d\u6587\u4ef6\u6216\u76ee\u9304\u662f\u5426\u5b58\u5728\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528`fileExists(atPath:)`\u65b9\u6cd5\u3002\u4ee5\u4e0b\u4ee3\u78bc\u793a\u4f8b\u6aa2\u67e5\u201ctest.txt\u201d\u6587\u4ef6\u7684\u5b58\u5728\u6027\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet filePath = &#8220;test.txt&#8221;<\/p>\n<p>if FileManager.default.fileExists(atPath: filePath) {<br \/>\n    print(&#8220;File exists&#8221;)<br \/>\n} else {<br \/>\n    print(&#8220;File does not exist&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0c\u672c\u6587\u6df1\u5165\u63a2\u8a0e\u4e86\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684FileManager\u985e\u9032\u884c\u6587\u4ef6\u548c\u76ee\u9304\u7684\u64cd\u4f5c\uff0c\u6db5\u84cb\u4e86\u5275\u5efa\u3001\u8b80\u53d6\u3001\u522a\u9664\u7b49\u57fa\u672c\u529f\u80fd\u3002\u5e0c\u671b\u9019\u4e9b\u793a\u4f8b\u80fd\u5e6b\u52a9\u60a8\u5728Swift\u958b\u767c\u4e2d\u66f4\u597d\u5730\u7ba1\u7406\u6587\u4ef6\u7cfb\u7d71\u64cd\u4f5c\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u6587\u4ef6\u7cfb\u7d71\u64cd\u4f5c\uff0c\u5305\u62ec\u4f7f\u7528FileManager\u7c7b\u4f86\u5275\u5efa\u3001\u8b80\u53d6\u3001\u522a\u9664\u6587\u4ef6\u548c\u76ee\u9304\u7b49\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u63d0\u4f9b\u6709\u95dc\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u6587\u4ef6\u7cfb\u7d71\u64cd\u4f5c\u7684\u8a73\u7d30\u4fe1\u606f\uff0c\u4ee5\u4fbf\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u4f7f\u7528\u5b83\u5011\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-3933","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-11r","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3933"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3933\/revisions"}],"predecessor-version":[{"id":3934,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3933\/revisions\/3934"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}