gpt4 book ai didi

arrays - 如何用函数中的数据填充数组?

转载 作者:行者123 更新时间:2023-11-28 08:29:54 25 4
gpt4 key购买 nike

您好,我有以下功能:

 func parceDosi()
{

struct City{

let name : String
// let location : CLLocation

}

var cities = [City]()


var person = "Person"
let path: String = NSBundle.mainBundle().pathForResource("jsonFile", ofType: "json") as String!
let jsonData = NSData(contentsOfFile: path) as NSData!
let ReadableJSON = JSON ( data:jsonData, options: NSJSONReadingOptions.MutableContainers, error: nil )

NumberRows = ReadableJSON ["People"].count

for (var i = 0 ; i <= NumberRows; i++ )
{
Person += "\(i)"

let NameImeNaObekt = ReadableJSON ["People"] [Person] ["B1"].string as String!

let NameShirina = ReadableJSON ["People"] [Person] ["C1"].string as String!

let NameDuljina = ReadableJSON ["People"] [Person] ["D1"].string as String!

let latitude = (NameShirina as NSString).doubleValue
let longitude = (NameDuljina as NSString).doubleValue
// let loc = CLLocation( latitude: latitude, longitude: longitude)
// let ci = City(name: NameImeNaObekt, location: loc)

let ci = City( name: NameImeNaObekt)

cities.append(ci)

从 json 文件中读取信息。读到的信息我想把它返回到一个

变量数据 = [??]

如果采用以下方式,则此变量数据可以正常工作:

var data = [纽约,波士顿,加利福尼亚],

但我不想写 New York, Boston, California ,而是想返回函数 func parceDosi(), 因为所有的城市都是从一个 json 文件中解析出来的,我不必写每一个城市。你知道怎么做吗,我是新手,我的语法有问题。这是 JSON 文件:

  {
"People": {
"Person0": {
"A1": "Безплатен Сок",
"B1": "Царево 11",
"C1": "43.0757",
"D1": "23.6172",
"E1": "http://weknowyourdreams.com/image.php?pic=/images/beer/beer-07.jpg"
},

"Person1": {
"A1": "Безплатна Бира",
"B1": "Царево 22",
"C1": "44.0757",
"D1": "24.6172",
"E1": "http://weknowyourdreams.com/image.php?pic=/images/beer/beer-07.jpg"
},
"Person2": {
"A1": "Безплатен Десерт",
"B1": "Царево 33",
"C1": "45.0757",
"D1": "25.6172",
"E1": "http://weknowyourdreams.com/image.php?pic=/images/beer/beer-07.jpg"
},
"Person3": {
"A1": "Безплатно Кафе",
"B1": "Царево 55",
"C1": "41.0757",
"D1": "21.6172",
"E1": "http://weknowyourdreams.com/image.php?pic=/images/beer/beer-07.jpg"
},
"Person4": {
"A1": "Безплатна Кола",
"B1": "Царево 66",
"C1": "46.0757",
"D1": "26.6172",
"E1": "http://weknowyourdreams.com/image.php?pic=/images/beer/beer-07.jpg"


}
}
}

最佳答案

JSON 中的每个 Person 包含 5 个键值对,所以我扩展了结构 City,属性 location 将从纬度创建和经度:

struct City {
let name : String
let city : String
let location : CLLocation
let imageURL : NSURL
}

由于您使用的是 SwiftJSON,因此解析安排得非常清楚

var cities = [City]()

func parceDosi() {
guard let url = NSBundle.mainBundle().URLForResource("jsonFile", withExtension: "json"), jsonData = NSData(contentsOfURL: url) else {
print("Error finding JSON File")
return
}

let readableJSON = JSON(data: jsonData) // the mutable option is not needed.

let people = readableJSON["People"].dictionaryValue
for (_, person) in people {
let personName = person["A1"].stringValue
let cityName = person["B1"].stringValue
let latitude = person["C1"].doubleValue
let longitude = person["D1"].doubleValue
let imageURL = person["E1"].stringValue

let city = City(name: personName, city: cityName, location: CLLocation(latitude: latitude, longitude: longitude), imageURL: NSURL(string:imageURL)!)
cities.append(city)
}

// This is an example to get the nearest city from a given location

let myLocation = CLLocation(latitude: 50.0, longitude: 8.0)
let nearestlocations = cities.sort { (city1, city2) -> Bool in
return city1.location.distanceFromLocation(myLocation) < city2.location.distanceFromLocation(myLocation)
}
print(nearestlocations.first!)
}

PS:由于您负责 bundle 中的 JSON 文件,因此我建议为人员使用数组(而不是字典),并为“棋盘”键提供更具描述性的名称。

关于arrays - 如何用函数中的数据填充数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39052225/

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