gpt4 book ai didi

ios - 在 Swift 中优化数组

转载 作者:可可西里 更新时间:2023-11-01 01:29:18 25 4
gpt4 key购买 nike

我需要在 Google map 上绘制多个多边形。我有针对不同 fips 的重复代码:

//对于 FIPS 06073//对于 FIPS 06013

我有以下代码:

let camera = GMSCameraPosition.cameraWithLatitude(37.0902, longitude: -95.7129, zoom: 4.0)

let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
view = mapView
mapView.setMinZoom(4.6, maxZoom: 8)

struct county {
let lat: CLLocationDegrees
let long: CLLocationDegrees
}

//for FIPS 06073
var Polygon:String = "-117.53781,33.45596 -117.51425,33.46779 -117.50857,33.47045 -117.50938,33.49185 -117.50972,33.50502 -117.50851,33.50502 -117.3665,33.49999 -117.29961,33.46761 -117.27432,33.45941 -117.24953,33.45143 -117.24147,33.43245 -117.22547,33.43203 -117.20616,33.43213 -117.17841,33.43224 -117.14449,33.43262 -117.14083,33.43257 -117.11877,33.43126 -117.08902,33.43029 -117.02485,33.42693 -116.94094,33.42749 -116.87069,33.42668 -116.83832,33.42673 -116.8093,33.4269 -116.71062,33.42712 -116.62585,33.42725 -116.53607,33.42697 -116.49289,33.42695 -116.41007,33.42731 -116.27514,33.42806 -116.16034,33.42736 -116.08623,33.42593 -116.08516,33.42593 -116.08516,33.42408 -116.08513,33.38386 -116.08506,33.35037 -116.08449,33.31736 -116.08365,33.28931 -116.08257,33.25621 -116.08122,33.19106 -116.08098,33.13784 -116.08106,33.09612 -116.1009,33.07469 -116.10324,33.03664 -116.10299,32.97434 -116.10306,32.94037 -116.10315,32.89856 -116.10257,32.85847 -116.10363,32.83857 -116.10371,32.80367 -116.10362,32.78758 -116.10349,32.75301 -116.10563,32.72158 -116.10569,32.68498 -116.10579,32.67149 -116.10576,32.65207 -116.10572,32.64677 -116.10586,32.64125 -116.10618,32.61859 -116.11543,32.61791 -116.15915,32.6141 -116.16942,32.61333 -116.18601,32.61215 -116.19782,32.61122 -116.20475,32.61064 -116.21089,32.61033 -116.26119,32.60616 -116.27792,32.60478 -116.30109,32.60295 -116.31535,32.60183 -116.35427,32.59863 -116.37913,32.59684 -116.39114,32.59575 -116.40925,32.59437 -116.41732,32.59374 -116.43543,32.59223 -116.45534,32.59053 -116.50085,32.58682 -116.51925,32.58529 -116.5355,32.58404 -116.54064,32.58375 -116.56588,32.5815 -116.59027,32.57942 -116.61646,32.57725 -116.6259,32.57637 -116.63188,32.57603 -116.66597,32.57321 -116.69018,32.5711 -116.71725,32.56886 -116.74629,32.5666 -116.75735,32.56566 -116.78281,32.5635 -116.78565,32.56326 -116.79591,32.56236 -116.81665,32.56066 -116.82259,32.56027 -116.82803,32.55985 -116.8483,32.55817 -116.87132,32.55603 -116.88943,32.55427 -116.90431,32.55286 -116.92805,32.55076 -116.93908,32.55018 -116.9624,32.54816 -116.98746,32.54616 -117.00862,32.54414 -117.02636,32.54246 -117.02846,32.54256 -117.02936,32.54245 -117.02943,32.54236 -117.02949,32.54226 -117.02972,32.54216 -117.03071,32.54216 -117.03619,32.54167 -117.04012,32.54134 -117.05832,32.54004 -117.0626,32.53973 -117.06345,32.53969 -117.08634,32.53776 -117.10511,32.53609 -117.12356,32.53431 -117.12496,32.53416 -117.18914,32.5299 -117.20705,32.53962 -117.21194,32.58312 -117.21929,32.61264 -117.2233,32.62124 -117.22841,32.6185 -117.24588,32.61499 -117.25509,32.61547 -117.26311,32.61765 -117.27125,32.6207 -117.27596,32.62313 -117.28584,32.63132 -117.30735,32.65404 -117.32352,32.71286 -117.32611,32.73932 -117.32689,32.75154 -117.32749,32.76087 -117.32939,32.79063 -117.33018,32.80303 -117.33382,32.81581 -117.33784,32.82798 -117.33981,32.85795 -117.32813,32.91893 -117.34575,32.97662 -117.34885,32.98675 -117.35631,33.0112 -117.36362,33.0351 -117.36601,33.04295 -117.37507,33.07258 -117.37575,33.07481 -117.39926,33.11512 -117.40914,33.13182 -117.43073,33.16916 -117.44954,33.19264 -117.50044,33.24176 -117.50774,33.25003 -117.521,33.26887 -117.58877,33.32175 -117.59911,33.37503 -117.59588,33.38663 -117.59548,33.38783 -117.59464,33.39158 -117.5941,33.394 -117.59394,33.3953 -117.59364,33.39661 -117.59303,33.39888 -117.59029,33.40855 -117.58987,33.41041 -117.58882,33.41504 -117.58432,33.43236 -117.58085,33.44524 -117.57865,33.45329 -117.57268,33.45328 -117.56805,33.4529 -117.56443,33.4524 -117.55775,33.45159 -117.55649,33.45148 -117.5563,33.45218 -117.54267,33.45499 -117.53843,33.45564"
var counties: [county] = []
var PolygonArr = Polygon.componentsSeparatedByString(" ")
for Index in 0...PolygonArr.count-1
{
let CoordinateString:String = PolygonArr[Index]
let CoordinateArray = CoordinateString.componentsSeparatedByString(",")
let MomentaryLatitude = (CoordinateArray[1] as NSString).doubleValue
let MomentaryLongitude = (CoordinateArray[0] as NSString).doubleValue
let countPolPlot = county(lat:MomentaryLatitude,long:MomentaryLongitude)
counties.append(countPolPlot)
}

let rectFIPS06073 = GMSMutablePath()
for county in counties {
rectFIPS06073.addCoordinate(CLLocationCoordinate2D(latitude: county.lat, longitude: county.long))
}
var polygon = GMSPolygon(path: rectFIPS06073)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = UIColor.blackColor()
polygon.strokeWidth = 2
polygon.map = mapView

//for FIPS 06013
Polygon = "-121.91161,38.04503 -121.89071,38.04653 -121.87505,38.05055 -121.86519,38.05991 -121.86246,38.06603 -121.86244,38.06607 -121.84085,38.04702 -121.8342,38.03473 -121.82767,38.02792 -121.82227,38.02502 -121.81331,38.02053 -121.80245,38.02137 -121.79192,38.01889 -121.78314,38.01854 -121.77062,38.02111 -121.75825,38.02413 -121.74193,38.02564 -121.73196,38.02942 -121.72576,38.03372 -121.70756,38.04085 -121.69876,38.04632 -121.68536,38.0579 -121.68203,38.06066 -121.67978,38.06578 -121.68025,38.07665 -121.68092,38.08466 -121.64936,38.09032 -121.63809,38.08613 -121.62899,38.09726 -121.59336,38.09771 -121.58064,38.09416 -121.58002,38.09441 -121.57925,38.09342 -121.5822,38.06842 -121.57811,38.03307 -121.56895,38.03023 -121.55797,38.01595 -121.56697,38.01353 -121.57485,38.00308 -121.57649,37.99598 -121.57861,37.97763 -121.56514,37.95985 -121.56455,37.95277 -121.55817,37.94538 -121.55767,37.93276 -121.56559,37.91819 -121.56816,37.90102 -121.57485,37.88502 -121.57444,37.86376 -121.56877,37.85978 -121.56149,37.85474 -121.55252,37.86112 -121.54103,37.85574 -121.5362,37.85081 -121.53659,37.84472 -121.54061,37.83607 -121.55165,37.82619 -121.55507,37.81744 -121.55694,37.81722 -121.557,37.81732 -121.58156,37.81216 -121.66954,37.78963 -121.79189,37.7591 -121.87996,37.73772 -121.92436,37.72731 -121.9403,37.72341 -121.95961,37.71889 -122.00433,37.77014 -122.11766,37.80299 -122.16687,37.81342 -122.18501,37.83271 -122.19419,37.84128 -122.20723,37.85151 -122.22138,37.87532 -122.24897,37.88703 -122.25309,37.8941 -122.25586,37.8965 -122.25876,37.89909 -122.26254,37.90194 -122.26822,37.90397 -122.27494,37.90453 -122.28906,37.89787 -122.29788,37.89879 -122.30706,37.89816 -122.37312,37.88388 -122.37378,37.88372 -122.37398,37.88388 -122.37458,37.88436 -122.37478,37.88452 -122.38174,37.88995 -122.39938,37.90372 -122.40259,37.90627 -122.40951,37.91176 -122.41214,37.91385 -122.42005,37.92013 -122.42268,37.92222 -122.4246,37.92374 -122.42644,37.9252 -122.4302,37.92818 -122.43036,37.9283 -122.43222,37.92977 -122.43228,37.92982 -122.43261,37.93074 -122.43378,37.93402 -122.43396,37.93439 -122.43417,37.93487 -122.43498,37.93682 -122.43978,37.9533 -122.44108,37.95806 -122.44148,37.96062 -122.44148,37.96532 -122.44148,37.9668 -122.44148,37.96802 -122.44156,37.97026 -122.44158,37.97239 -122.43945,37.97627 -122.43493,37.98124 -122.42248,37.99492 -122.41794,37.99992 -122.41368,38.00452 -122.40126,38.0179 -122.39448,38.0252 -122.39078,38.02958 -122.38275,38.03982 -122.37733,38.04674 -122.37447,38.04977 -122.36845,38.05501 -122.35165,38.06961 -122.34745,38.07326 -122.27598,38.06114 -122.23295,38.0621 -122.22612,38.06104 -122.22242,38.06067 -122.19868,38.05743 -122.19033,38.0555 -122.16954,38.03927 -122.16459,38.03473 -122.15742,38.03303 -122.14929,38.03181 -122.13439,38.03372 -122.12359,38.03589 -122.12081,38.03804 -122.09978,38.04701 -122.08875,38.05009 -122.0673,38.06008 -122.04594,38.0602 -121.98316,38.0673 -121.94686,38.0529 -121.93558,38.04886 -121.92846,38.04705 -121.91194,38.04505"
counties = []
PolygonArr = []
PolygonArr = Polygon.componentsSeparatedByString(" ")
for Index in 0...PolygonArr.count-1
{
let CoordinateString:String = PolygonArr[Index]
let CoordinateArray = CoordinateString.componentsSeparatedByString(",")
let MomentaryLatitude = (CoordinateArray[1] as NSString).doubleValue
let MomentaryLongitude = (CoordinateArray[0] as NSString).doubleValue
let countPolPlot = county(lat:MomentaryLatitude,long:MomentaryLongitude)
counties.append(countPolPlot)
}
let rectFIPS06013 = GMSMutablePath()
for county in counties {
rectFIPS06013.addCoordinate(CLLocationCoordinate2D(latitude: county.lat, longitude: county.long))
}
// Create the polygon, and assign it to the map.
polygon = GMSPolygon(path: rectFIPS06013)
polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
polygon.strokeColor = UIColor.blackColor()
polygon.strokeWidth = 2
polygon.map = mapView

是否可以优化,让代码不重复?

最佳答案

只做一个函数:

function addCounties(PolygonArr: GMSPolygon)->[county] {

var counties: [county] = []
for Index in 0...PolygonArr.count-1
{
let CoordinateString:String = PolygonArr[Index]
let CoordinateArray = CoordinateString.componentsSeparatedByString(",")
let MomentaryLatitude = (CoordinateArray[1] as NSString).doubleValue
let MomentaryLongitude = (CoordinateArray[0] as NSString).doubleValue
let countPolPlot = county(lat:MomentaryLatitude,long:MomentaryLongitude)
counties.append(countPolPlot)
}
return counties
}

调用它:

let newCounties = addCountries(PolygonArr)
counties.append(contentsOf: newCounties) // Swift 3
counties.appendContentsOf(newCounties) // Swift 2.x

关于ios - 在 Swift 中优化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39805486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com