{"id":7248,"date":"2025-06-03T11:51:13","date_gmt":"2025-06-05T04:47:13","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7248"},"modified":"2025-06-05T11:51:13","modified_gmt":"2025-06-05T04:47:13","slug":"https-badgameshow-com-steven-296","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-296\/","title":{"rendered":"Swift \u6587\u4ef6\u7ba1\u7406 \ud83d\uddc2\ufe0f 2025 \u6700\u65b0\u7684\u5c08\u696d\u6587\u4ef6\u7ba1\u7406\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u6587\u4ef6\u7ba1\u7406, \u6587\u4ef6\u7ba1\u7406\u6559\u5b78, FileManager \u4f7f\u7528\u6559\u5b78\"><\/p>\n<h1>Swift \u6587\u4ef6\u7ba1\u7406 \ud83d\uddc2\ufe0f 2025 \u6700\u65b0\u7684\u5c08\u696d\u6587\u4ef6\u7ba1\u7406\u6559\u5b78<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u958b\u767c\u51fa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u5728\u958b\u767c\u61c9\u7528\u7a0b\u5f0f\u6642\uff0c\u6587\u4ef6\u7ba1\u7406\u662f\u4e00\u500b\u91cd\u8981\u7684\u90e8\u5206\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u61c9\u7528\u7a0b\u5f0f\u7684\u6587\u4ef6\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u7ba1\u7406\u6587\u4ef6\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u5be6\u73fe\u5c08\u696d\u7684\u6587\u4ef6\u7ba1\u7406\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>Swift \u6587\u4ef6\u7ba1\u7406\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Swift \u6587\u4ef6\u7ba1\u7406\u7684\u57fa\u672c\u6982\u5ff5\u662f\u5c07\u6587\u4ef6\u5206\u70ba\u4e0d\u540c\u7684\u985e\u5225\uff0c\u4e26\u5c07\u5b83\u5011\u5b58\u5132\u5728\u4e0d\u540c\u7684\u6587\u4ef6\u593e\u4e2d\u3002\u9019\u6a23\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u6587\u4ef6\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u67e5\u627e\u6587\u4ef6\u3002<\/p>\n<p>\u5728 Swift \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>FileManager<\/code> \u985e\u5225\u4f86\u7ba1\u7406\u6587\u4ef6\u3002 <code>FileManager<\/code> \u985e\u5225\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f86\u5275\u5efa\u3001\u522a\u9664\u3001\u79fb\u52d5\u548c\u67e5\u627e\u6587\u4ef6\u3002<\/p>\n<h2>\u4f7f\u7528 Swift \u5be6\u73fe\u5c08\u696d\u7684\u6587\u4ef6\u7ba1\u7406<\/h2>\n<p>\u4f7f\u7528 Swift \u5be6\u73fe\u5c08\u696d\u7684\u6587\u4ef6\u7ba1\u7406\uff0c\u9996\u5148\u9700\u8981\u5b9a\u7fa9\u6587\u4ef6\u7684\u985e\u5225\uff0c\u4e26\u5c07\u5b83\u5011\u5b58\u5132\u5728\u4e0d\u540c\u7684\u6587\u4ef6\u593e\u4e2d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c07\u5716\u7247\u6587\u4ef6\u5b58\u5132\u5728 <code>images<\/code> \u6587\u4ef6\u593e\u4e2d\uff0c\u6587\u672c\u6587\u4ef6\u5b58\u5132\u5728 <code>text<\/code> \u6587\u4ef6\u593e\u4e2d\uff0c\u97f3\u983b\u6587\u4ef6\u5b58\u5132\u5728 <code>audio<\/code> \u6587\u4ef6\u593e\u4e2d\uff0c\u8996\u983b\u6587\u4ef6\u5b58\u5132\u5728 <code>video<\/code> \u6587\u4ef6\u593e\u4e2d\uff0c\u4ee5\u6b64\u985e\u63a8\u3002<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>FileManager<\/code> \u985e\u5225\u4f86\u5275\u5efa\u6587\u4ef6\u593e\uff0c\u4e26\u5c07\u6587\u4ef6\u5b58\u5132\u5728\u6587\u4ef6\u593e\u4e2d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u5275\u5efa <code>images<\/code> \u6587\u4ef6\u593e\uff1a<\/p>\n<pre><code>let fileManager = FileManager.default\nlet documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!\nlet imagesDirectory = documentsDirectory.appendingPathComponent(\"images\")\n\ndo {\n    try fileManager.createDirectory(at: imagesDirectory, withIntermediateDirectories: true, attributes: nil)\n} catch {\n    print(\"\u5275\u5efa\u6587\u4ef6\u593e\u5931\u6557: \\(error)\")\n}\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>FileManager<\/code> \u985e\u5225\u7684 <code>copyItem<\/code> \u65b9\u6cd5\u5c07\u6587\u4ef6\u5f9e\u4e00\u500b\u6587\u4ef6\u593e\u8907\u88fd\u5230\u53e6\u4e00\u500b\u6587\u4ef6\u593e\u4e2d\uff1a<\/p>\n<pre><code>let sourceURL = URL(fileURLWithPath: \"source\/image.jpg\")\nlet destinationURL = imagesDirectory.appendingPathComponent(\"image.jpg\")\n\ndo {\n    try fileManager.copyItem(at: sourceURL, to: destinationURL)\n} catch {\n    print(\"\u8907\u88fd\u6587\u4ef6\u5931\u6557: \\(error)\")\n}\n<\/code><\/pre>\n<p>\u6700\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>FileManager<\/code> \u985e\u5225\u7684 <code>contentsOfDirectory<\/code> \u65b9\u6cd5\u4f86\u67e5\u627e\u6587\u4ef6\u593e\u4e2d\u7684\u6587\u4ef6\uff1a<\/p>\n<pre><code>do {\n    let files = try fileManager.contentsOfDirectory(at: imagesDirectory, includingPropertiesForKeys: nil)\n    for file in files {\n        print(\"\u627e\u5230\u6587\u4ef6: \\(file)\")\n    }\n} catch {\n    print(\"\u7372\u53d6\u6587\u4ef6\u593e\u5167\u5bb9\u5931\u6557: \\(error)\")\n}\n<\/code><\/pre>\n<p>\u4f7f\u7528 Swift \u4f86\u7ba1\u7406\u6587\u4ef6\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u6587\u4ef6\uff0c\u4e26\u4e14\u53ef\u4ee5\u5be6\u73fe\u5c08\u696d\u7684\u6587\u4ef6\u7ba1\u7406\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<h3>1. \u5728 Swift \u4e2d\uff0c\u5982\u4f55\u67e5\u627e\u7279\u5b9a\u985e\u578b\u7684\u6587\u4ef6\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>contentsOfDirectory<\/code> \u65b9\u6cd5\u7d50\u5408 <code>filter<\/code> \u4f86\u67e5\u627e\u7279\u5b9a\u985e\u578b\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u904e\u6ffe\u51fa\u6240\u6709\u7684\u5716\u7247\u6587\u4ef6\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406\u6587\u4ef6\u64cd\u4f5c\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>\u5728\u9032\u884c\u6587\u4ef6\u64cd\u4f5c\u6642\uff0c\u5efa\u8b70\u4f7f\u7528 <code>do-catch<\/code> \u8a9e\u53e5\u4f86\u6355\u7372\u548c\u8655\u7406\u53ef\u80fd\u767c\u751f\u7684\u932f\u8aa4\uff0c\u5f9e\u800c\u63d0\u9ad8\u7a0b\u5f0f\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u8b80\u6027\u3002<\/p>\n<h3>3. \u53ef\u4ee5\u4f7f\u7528 Swift \u7ba1\u7406\u96f2\u7aef\u6587\u4ef6\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Swift \u7d50\u5408\u5404\u7a2e\u96f2\u7aef\u670d\u52d9\u7684 SDK \u4f86\u5be6\u73fe\u5c0d\u96f2\u7aef\u6587\u4ef6\u7684\u7ba1\u7406\uff0c\u50cf\u662f Firebase Storage \u6216 AWS S3\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u6587\u4ef6\u7ba1\u7406-\ud83d\uddc2\ufe0f-\u5c08\u696d\u7684\u6587\u4ef6\u7ba1\u7406_1675409566.016602.webp\" alt=\"Swift \u6587\u4ef6\u7ba1\u7406 \ud83d\uddc2\ufe0f \u5c08\u696d\u7684\u6587\u4ef6\u7ba1\u7406\" title=\"Swift \u6587\u4ef6\u7ba1\u7406, \u6587\u4ef6\u7ba1\u7406, \u6587\u4ef6\u7ba1\u7406\u65b9\u6cd5, \u6587\u4ef6\u7ba1\u7406\u6280\u5de7, \u6587\u4ef6\u7ba1\u7406\u5de5\u5177\" width=\"1200\" height=\"628\"\/><br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u5b78\u7fd2Swift\u6587\u4ef6\u7ba1\u7406\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u6709\u6548\u7387\u5730\u7ba1\u7406\u6587\u4ef6\uff0c\u66f4\u5feb\u901f\u5730\u5b8c\u6210\u5de5\u4f5c\u3002\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2d\u7684\u6587\u4ef6\u7ba1\u7406\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":7245,"comment_status":"open","ping_status":"closed","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-7248","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u6587\u4ef6\u7ba1\u7406-\ud83d\uddc2\ufe0f-\u5c08\u696d\u7684\u6587\u4ef6\u7ba1\u7406_1675409566.016602.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1SU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7248","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=7248"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7248\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7245"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}