{"id":6676,"date":"2025-06-04T11:47:17","date_gmt":"2025-06-05T04:45:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6676"},"modified":"2025-06-05T11:47:17","modified_gmt":"2025-06-05T04:45:16","slug":"https-badgameshow-com-steven-16","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-16\/","title":{"rendered":"Swift \u97f3\u6a02\u64a5\u653e\u5668\u6559\u5b78 \ud83c\udfb5 \u5b8c\u6574\u81ea\u8a02\u97f3\u6a02\u64a5\u653e\u529f\u80fd\u6307\u5357\u30102025 \u6700\u65b0\u8a9e\u6cd5\u3011"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u97f3\u6a02\u64a5\u653e\u5668, \u81ea\u8a02\u97f3\u6a02\u64a5\u653e\u529f\u80fd, iOS, Swift \u6559\u5b78\"><\/p>\n<h2>Swift \u97f3\u6a02\u64a5\u653e\u5668\u6559\u5b78 \ud83c\udfb5 \u5b8c\u6574\u81ea\u8a02\u97f3\u6a02\u64a5\u653e\u529f\u80fd\u6307\u5357<\/h2>\n<p>Swift \u662f\u4e00\u7a2e\u5feb\u901f\u3001\u5b89\u5168\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u7528\u65bc\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u7b49\u61c9\u7528\u7a0b\u5f0f\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6df1\u5165\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u958b\u767c\u4e00\u500b\u81ea\u8a02\u7684\u97f3\u6a02\u64a5\u653e\u5668\uff0c\u8b93\u4f60\u80fd\u5920\u8f15\u9b06\u64ad\u653e\u81ea\u5df1\u559c\u611b\u7684\u97f3\u6a02\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u6559\u5b78\u6d41\u7a0b\u548c\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h3>\u5b89\u88dd\u8207\u8a2d\u7f6e Xcode<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b89\u88dd <strong>Xcode<\/strong>\uff0c\u5b83\u662f Apple \u5b98\u65b9\u63d0\u4f9b\u7684\u958b\u767c\u74b0\u5883\uff0c\u5c08\u70ba\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u61c9\u7528\u7a0b\u5f0f\u800c\u8a2d\u3002\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u4fbf\u53ef\u4ee5\u958b\u59cb\u4f7f\u7528 Swift \u958b\u767c\u97f3\u6a02\u64a5\u653e\u5668\u3002<\/p>\n<h3>\u5b9a\u7fa9\u97f3\u6a02\u64ad\u653e\u5668\u7d50\u69cb<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u4e00\u500b <strong>struct<\/strong>\uff0c\u7528\u65bc\u5b58\u5132\u97f3\u6a02\u64ad\u653e\u5668\u7684\u8cc7\u8a0a\uff0c\u4f8b\u5982\u6b4c\u66f2\u540d\u7a31\u3001\u64ad\u653e\u6642\u9593\u7b49\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">struct Music {\n    var songName: String\n    var playTime: Double \/\/ \u4ee5\u79d2\u70ba\u55ae\u4f4d\n}<\/code><\/pre>\n<h3>\u63a7\u5236\u97f3\u6a02\u64ad\u653e\u5668\u7684\u884c\u70ba<\/h3>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u4e00\u500b <strong>class<\/strong>\uff0c\u7528\u65bc\u63a7\u5236\u97f3\u6a02\u64ad\u653e\u5668\u7684\u884c\u70ba\uff0c\u4f8b\u5982\u64ad\u653e\u3001\u66ab\u505c\u3001\u505c\u6b62\u7b49\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">class MusicController {\n    private var currentMusic: Music?\n\n    func play(music: Music) {\n        self.currentMusic = music\n        \/\/ \u64ad\u653e\u97f3\u6a02\u7684\u908f\u8f2f\n        print(\"\u6b63\u5728\u64ad\u653e: \\(music.songName)\")\n    }\n\n    func pause() {\n        \/\/ \u66ab\u505c\u97f3\u6a02\u7684\u908f\u8f2f\n        print(\"\u97f3\u6a02\u5df2\u66ab\u505c\")\n    }\n\n    func stop() {\n        \/\/ \u505c\u6b62\u97f3\u6a02\u7684\u908f\u8f2f\n        print(\"\u97f3\u6a02\u5df2\u505c\u6b62\")\n    }\n}<\/code><\/pre>\n<h3>\u5b9a\u7fa9\u97f3\u6a02\u64ad\u653e\u5668\u5354\u8b70<\/h3>\n<p>\u6211\u5011\u9084\u53ef\u4ee5\u5b9a\u7fa9\u4e00\u500b <strong>protocol<\/strong>\uff0c\u7528\u65bc\u898f\u7bc4\u97f3\u6a02\u64ad\u653e\u5668\u7684\u884c\u70ba\u3002\u4f8b\u5982\u64ad\u653e\u3001\u66ab\u505c\u3001\u505c\u6b62\u7b49\u529f\u80fd\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">protocol MusicPlayerDelegate {\n    func didPlay()\n    func didPause()\n    func didStop()\n}<\/code><\/pre>\n<h3>\u5be6\u4f5c\u97f3\u6a02\u64a5\u653e\u5668<\/h3>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u5b9a\u7fa9\u7684 <strong>struct<\/strong>\u3001<strong>class<\/strong> \u548c <strong>protocol<\/strong> \u4f86\u958b\u767c\u6211\u5011\u7684\u97f3\u6a02\u64a5\u653e\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u7bc4\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let song = Music(songName: \"My Favorite Song\", playTime: 240)\nlet musicController = MusicController()\nmusicController.play(music: song)<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u4f60\u5c31\u80fd\u5920\u8f15\u9b06\u64ad\u653e\u4f60\u559c\u6b61\u7684\u97f3\u6a02\uff0c\u4e26\u81ea\u8a02\u97f3\u6a02\u64a5\u653e\u529f\u80fd\uff0c\u8b93\u97f3\u6a02\u64ad\u653e\u5668\u7684\u884c\u70ba\u66f4\u52a0\u9748\u6d3b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u97f3\u6a02\u64a5\u653e\u5668-\ud83c\udfb5-\u81ea\u8a02\u97f3\u6a02\u64a5\u653e\u529f\u80fd_1675393596.8905091.webp\" alt=\"Swift \u97f3\u6a02\u64a5\u653e\u5668 \ud83c\udfb5 \u81ea\u8a02\u97f3\u6a02\u64a5\u653e\u529f\u80fd\" title=\"Swift \u97f3\u6a02\u64a5\u653e\u5668, \u81ea\u8a02\u97f3\u6a02\u64a5\u653e, Swift \u51fd\u6578, Swift \u65b9\u6cd5, Swift \u7269\u4ef6\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 Swift \u4e2d\u6dfb\u52a0\u80cc\u666f\u97f3\u6a02\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 AVFoundation \u6846\u67b6\u4f86\u6dfb\u52a0\u80cc\u666f\u97f3\u6a02\uff0c\u900f\u904e AVAudioPlayer \u4f86\u64ad\u653e\u97f3\u6a02\u6a94\u6848\u3002<\/p>\n<h3>2. \u5982\u4f55\u63a7\u5236\u97f3\u6a02\u7684\u97f3\u91cf\uff1f<\/h3>\n<p>\u53ef\u4ee5\u5728\u97f3\u6a02\u64ad\u653e\u5668\u4e2d\u6dfb\u52a0\u97f3\u91cf\u5c6c\u6027\uff0c\u4e26\u4f7f\u7528\u6ed1\u687f\u4f86\u8b93\u7528\u6236\u8abf\u6574\u97f3\u91cf\u3002<\/p>\n<h3>3. \u5982\u4f55\u8655\u7406\u97f3\u6a02\u64ad\u653e\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>\u53ef\u4ee5\u5728\u64ad\u653e\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u932f\u8aa4\u8655\u7406\u908f\u8f2f\uff0c\u5229\u7528 Swift \u7684\u932f\u8aa4\u6355\u7372\u6a5f\u5236\u4f86\u8655\u7406\u4e0d\u540c\u7684\u932f\u8aa4\u60c5\u6cc1\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u5efa\u7acb\u81ea\u8a02\u97f3\u6a02\u64a5\u653e\u5668\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u51fd\u6578\u3001\u65b9\u6cd5\u53ca\u7269\u4ef6\uff0c\u8b93\u4f60\u7684\u97f3\u6a02\u64a5\u653e\u5668\u66f4\u52a0\u5b8c\u5584\u3002<\/p>\n","protected":false},"author":1,"featured_media":6675,"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-6676","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-\u97f3\u6a02\u64a5\u653e\u5668-\ud83c\udfb5-\u81ea\u8a02\u97f3\u6a02\u64a5\u653e\u529f\u80fd_1675393596.8905091.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1JG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6676","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=6676"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6676\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6675"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}