gpt4 book ai didi

arrays - 问题填充数组 swift 3

转载 作者:搜寻专家 更新时间:2023-11-01 07:12:54 25 4
gpt4 key购买 nike

尝试用数组填充 ViewControler,但获取的数组没有成员名称。然而,我多次设置同一个数组没有问题,但它们都在一个 tableView 中,这是一个 ViewController。

出现错误的 Controller :

class LocationViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, LocationDelegate
{

var diveSiteArray: Array<DiveSiteSimpleModel> = [DiveSiteSimpleModel]()

override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)

self.diveSiteArray = AppDelegate.getDLDatabase().getDiveSitesSimple()
loadSitesArray()

let location = CLLocationCoordinate2D(latitude: siteLatitude,longitude: siteLongitude)
let span = MKCoordinateSpanMake(0.15, 0.15)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)

let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = diveSiteLabel
mapView.addAnnotation(annotation)

}

}
func loadSitesArray () {

self.diveSiteLabel = diveSiteArray.name // here I get the error "Array< DiveSiteSimpleModel > does not have a member name
siteLongitude = diveSiteArray.longitude // here I get the error "Array< DiveSiteSimpleModel > does not have a member longitude
siteLatitude = diveSiteArray.latitude // here I get the error "Array< DiveSiteSimpleModel > does not have a member latitude
}

DiveSiteSimpleModel 如下:

struct DiveSiteSimpleModel
{
var id: Int = -1
var name: String = ""
var latitude: Double = 0.0
var longitude: Double = 0.0
var diveSiteImage: String = ""

}

DLDatabase如下:

func getDiveSitesSimple(_ query: String) -> Array<DiveSiteSimpleModel>
{
var sites: Array<DiveSiteSimpleModel> = [DiveSiteSimpleModel]()

if self.database == nil {
return sites
}

var statement: OpaquePointer? = nil

sqlite3_prepare_v2(self.database, query, -1, &statement, nil)
while sqlite3_step(statement) == SQLITE_ROW
{
var site: DiveSiteSimpleModel = DiveSiteSimpleModel()

site.id = Int(sqlite3_column_int64(statement, 0))
site.name = String(cString: (sqlite3_column_text(statement, 1)))
site.latitude = Double(sqlite3_column_double(statement, 2))
site.longitude = Double(sqlite3_column_double(statement, 3))
site.diveSiteImage = String(cString: (sqlite3_column_text(statement, 4)))

sites.append(site)

}

sqlite3_finalize(statement)

return sites
}

最后,SQL是

static let SelectDiveSiteSimple         = "SELECT * FROM DiveSite"

为此疯狂,任何帮助都是巨大的

最佳答案

diveSiteArrayDiveSiteSimpleModel 结构的数组。当您收到错误时,您将 diveSiteArray 视为 DiveSiteSimpleModel 的实例,而不是它实际的数组。您需要在 diveSiteArray 中引用其中一个 DiveSiteSimpleModel 结构,例如:

diveSiteLabel = diveSiteArray[0].name

获取数组中第一个结构体的名称元素。

关于arrays - 问题填充数组 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44015086/

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