{"id":7008,"date":"2025-05-30T14:59:02","date_gmt":"2025-06-05T07:05:01","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7008"},"modified":"2025-06-05T14:59:02","modified_gmt":"2025-06-05T07:05:01","slug":"https-badgameshow-com-steven-180","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-180\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Swift \u9910\u5ef3\u641c\u5c0b\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u6559\u5b78 \ud83c\udf54 \u5730\u9ede\u9078\u64c7\u3001\u98df\u8a55\u3001\u98df\u7269\u5206\u985e\u8207\u8ddd\u96e2\u6392\u5e8f\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u9910\u5ef3\u641c\u5c0b, \u5730\u9ede\u9078\u64c7, \u98df\u8a55, \u98df\u7269\u5206\u985e, \u8ddd\u96e2\u6392\u5e8f\"><\/p>\n<h2>2025 \u6700\u65b0\u7248 Swift \u9910\u5ef3\u641c\u5c0b\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u6559\u5b78<\/h2>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u4e00\u8d77\u958b\u767c\u4e00\u500b\u4f7f\u7528 Swift \u8a9e\u8a00\u7684\u9910\u5ef3\u641c\u5c0b\u5de5\u5177\uff0c\u9019\u500b\u5de5\u5177\u80fd\u5920\u5e6b\u52a9\u4f7f\u7528\u8005\u8f15\u9b06\u5730\u627e\u5230\u6700\u9069\u5408\u7684\u9910\u5ef3\u3002\u6211\u5011\u6703\u6db5\u84cb\u5730\u9ede\u9078\u64c7\u3001\u98df\u8a55\u3001\u98df\u7269\u5206\u985e\u53ca\u8ddd\u96e2\u6392\u5e8f\u7b49\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h3>\u74b0\u5883\u8a2d\u7f6e<\/h3>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u78ba\u4fdd\u4f60\u7684\u958b\u767c\u74b0\u5883\u5df2\u5b89\u88dd Xcode 14 \u6216\u66f4\u9ad8\u7248\u672c\uff0c\u4e26\u4e14\u5177\u5099 Swift 5.7 \u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<h3>\u5efa\u7acb\u5c08\u6848<\/h3>\n<p>1. \u6253\u958b Xcode\uff0c\u9078\u64c7\u300cCreate a new Xcode project\u300d\u3002<br \/>\n2. \u9078\u64c7\u300cApp\u300d\uff0c\u7136\u5f8c\u9ede\u64ca\u300cNext\u300d\u3002<br \/>\n3. \u586b\u5beb\u5c08\u6848\u540d\u7a31\uff08\u4f8b\u5982\uff1aRestaurantFinder\uff09\uff0c\u9078\u64c7 Swift \u4f5c\u70ba\u8a9e\u8a00\uff0c\u7136\u5f8c\u9ede\u64ca\u300cNext\u300d\u4e26\u9078\u64c7\u4fdd\u5b58\u4f4d\u7f6e\u3002<\/p>\n<h3>\u5730\u9ede\u9078\u64c7\u529f\u80fd\u5be6\u4f5c<\/h3>\n<p>\u70ba\u4e86\u5be6\u73fe\u5730\u9ede\u9078\u64c7\u529f\u80fd\uff0c\u6211\u5011\u5c07\u4f7f\u7528 Core Location \u6846\u67b6\u3002\u8acb\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<p>1. \u5728\u5c08\u6848\u7684 Info.plist \u6a94\u6848\u4e2d\uff0c\u6dfb\u52a0\u300cPrivacy &#8211; Location When In Use Usage Description\u300d\u9375\uff0c\u4e26\u63d0\u4f9b\u4f7f\u7528\u8005\u770b\u5230\u7684\u63cf\u8ff0\u3002<br \/>\n2. \u5728 ViewController.swift \u4e2d\uff0c\u5c0e\u5165 Core Location\uff1a<br \/>\n   &#8220;`swift<br \/>\n   import CoreLocation<br \/>\n   &#8220;`<br \/>\n3. \u5efa\u7acb CLLocationManager \u7684\u5be6\u4f8b\u4e26\u8a2d\u7f6e\u4ee3\u7406\uff1a<br \/>\n   &#8220;`swift<br \/>\n   class ViewController: UIViewController, CLLocationManagerDelegate {<br \/>\n       let locationManager = CLLocationManager()<\/p>\n<p>       override func viewDidLoad() {<br \/>\n           super.viewDidLoad()<br \/>\n           locationManager.delegate = self<br \/>\n           locationManager.requestWhenInUseAuthorization()<br \/>\n           locationManager.startUpdatingLocation()<br \/>\n       }<br \/>\n   }<br \/>\n   &#8220;`<\/p>\n<h3>\u98df\u8a55\u8207\u98df\u7269\u5206\u985e\u529f\u80fd<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u70ba\u4f7f\u7528\u8005\u63d0\u4f9b\u9910\u5ef3\u7684\u98df\u8a55\u8207\u5206\u985e\u529f\u80fd\u3002\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u4e00\u500b\u9663\u5217\u4f86\u5132\u5b58\u9910\u5ef3\u8cc7\u6599\uff0c\u5982\u4e0b\u6240\u793a\uff1a<br \/>\n&#8220;`swift<br \/>\nstruct Restaurant {<br \/>\n    let name: String<br \/>\n    let rating: Double<br \/>\n    let cuisineType: String<br \/>\n}<\/p>\n<p>let restaurants = [<br \/>\n    Restaurant(name: &#8220;Restaurant A&#8221;, rating: 4.5, cuisineType: &#8220;Italian&#8221;),<br \/>\n    Restaurant(name: &#8220;Restaurant B&#8221;, rating: 4.0, cuisineType: &#8220;Chinese&#8221;),<br \/>\n    \/\/ \u66f4\u591a\u9910\u5ef3&#8230;<br \/>\n]\n&#8220;`<\/p>\n<h3>\u8ddd\u96e2\u6392\u5e8f\u529f\u80fd<\/h3>\n<p>\u70ba\u4e86\u5be6\u73fe\u8ddd\u96e2\u6392\u5e8f\u529f\u80fd\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 CLLocationCoordinate2D \u4f86\u8a08\u7b97\u7528\u6236\u7576\u524d\u4f4d\u7f6e\u8207\u9910\u5ef3\u4e4b\u9593\u7684\u8ddd\u96e2\u3002\u4ee5\u4e0b\u662f\u8a08\u7b97\u8ddd\u96e2\u7684\u7bc4\u4f8b\uff1a<br \/>\n&#8220;`swift<br \/>\nfunc sortRestaurantsByDistance(userLocation: CLLocation) -> [Restaurant] {<br \/>\n    \/\/ \u5047\u8a2d\u6bcf\u500b Restaurant \u6709\u4e00\u500b\u5730\u7406\u5ea7\u6a19<br \/>\n    return restaurants.sorted { (first, second) -> Bool in<br \/>\n        let firstDistance = userLocation.distance(from: first.location)<br \/>\n        let secondDistance = userLocation.distance(from: second.location)<br \/>\n        return firstDistance < secondDistance\n    }\n}\n```\n\n\n\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\u4f4d\u7f6e\u670d\u52d9\u672a\u555f\u7528\u6216\u4f7f\u7528\u8005\u672a\u6388\u6b0a\u4f4d\u7f6e\u5b58\u53d6\u3002\u78ba\u4fdd\u5728\u9069\u7576\u7684\u5730\u65b9\u6dfb\u52a0\u932f\u8aa4\u8655\u7406\u908f\u8f2f\uff1a<br \/>\n&#8220;`swift<br \/>\nfunc locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {<br \/>\n    print(&#8220;Failed to find user&#8217;s location: \\(error.localizedDescription)&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u9019\u500b\u61c9\u7528\u7a0b\u5f0f\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\uff0c\u4f8b\u5982\u52a0\u5165\u7528\u6236\u8a55\u50f9\u529f\u80fd\u3001\u793e\u4ea4\u5a92\u9ad4\u5206\u4eab\u3001\u83dc\u55ae\u67e5\u770b\u7b49\u3002\u9019\u4e9b\u529f\u80fd\u80fd\u8b93\u4f7f\u7528\u8005\u6709\u66f4\u5b8c\u6574\u7684\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u9910\u5ef3\u641c\u5c0b-\ud83c\udf54-\u5730\u9ede\u9078\u64c7\u3001\u98df\u8a55\u3001\u98df\u7269\u5206\u985e\u3001\u8ddd\u96e2\u6392\u5e8f_1675400461.21897.webp\" alt=\"Swift \u9910\u5ef3\u641c\u5c0b \ud83c\udf54 \u5730\u9ede\u9078\u64c7\u3001\u98df\u8a55\u3001\u98df\u7269\u5206\u985e\u3001\u8ddd\u96e2\u6392\u5e8f\" title=\"Swift \u9910\u5ef3\u641c\u5c0b\u3001\u5730\u9ede\u9078\u64c7\u3001\u98df\u8a55\u3001\u98df\u7269\u5206\u985e\u3001\u8ddd\u96e2\u6392\u5e8f\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528\u5730\u7406\u4f4d\u7f6e\u670d\u52d9\uff1f<\/h3>\n<p>A1: \u4f60\u9700\u8981\u5c0e\u5165 Core Location \u6846\u67b6\u4e26\u8a2d\u7f6e CLLocationManager\uff0c\u8acb\u53c3\u8003\u4e0a\u8ff0\u4ee3\u78bc\u7bc4\u4f8b\u4ee5\u4e86\u89e3\u8a73\u7d30\u6b65\u9a5f\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u8655\u7406\u7528\u6236\u672a\u6388\u6b0a\u4f4d\u7f6e\u5b58\u53d6\u7684\u60c5\u6cc1\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u5728\u4f4d\u7f6e\u7ba1\u7406\u5668\u7684\u4ee3\u7406\u65b9\u6cd5\u4e2d\u6aa2\u67e5\u932f\u8aa4\uff0c\u4e26\u63d0\u4f9b\u9069\u7576\u7684\u63d0\u793a\u7d66\u7528\u6236\u3002<\/p>\n<h3>Q3: \u6709\u4ec0\u9ebc\u65b9\u6cd5\u53ef\u4ee5\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\uff1f<\/h3>\n<p>A3: \u5118\u91cf\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u66f4\u65b0\uff0c\u4f7f\u7528\u7de9\u5b58\u6280\u8853\u4f86\u5132\u5b58\u9910\u5ef3\u6578\u64da\uff0c\u4e26\u53ea\u5728\u9700\u8981\u6642\u9032\u884c\u7db2\u7d61\u8acb\u6c42\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aSwift\u662f\u4e00\u7a2e\u5feb\u901f\u3001\u5b89\u5168\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5feb\u901f\u641c\u5c0b\u9910\u5ef3\uff0c\u63d0\u4f9b\u5730\u9ede\u9078\u64c7\u3001\u98df\u8a55\u3001\u98df\u7269\u5206\u985e\u3001\u8ddd\u96e2\u6392\u5e8f\u7b49\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u66f4\u5feb\u901f\u3001\u66f4\u5b89\u5168\u5730\u641c\u5c0b\u9910\u5ef3\u3002<\/p>\n","protected":false},"author":1,"featured_media":7007,"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-7008","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-\u9910\u5ef3\u641c\u5c0b-\ud83c\udf54-\u5730\u9ede\u9078\u64c7\u3001\u98df\u8a55\u3001\u98df\u7269\u5206\u985e\u3001\u8ddd\u96e2\u6392\u5e8f_1675400461.21897.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1P2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7008","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=7008"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7008\/revisions"}],"predecessor-version":[{"id":13313,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7008\/revisions\/13313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7007"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}