簡體   English   中英

無法將類型“ [String]”的值分配為類型“ String”? 迅捷2

[英]Cannot assign value of type '[String]' to type 'String? Swift 2

我收到與此行“ point.title = r”和此行“ point.subtitle = z”相關聯的錯誤消息“無法將類型'[String]'的值分配給類型'String?'”。 我已經嘗試了很多東西,例如place.description,它使整個數組成為一個字符串...

//I have multiple arrays, lat, long and pass besides place.
var place = [String]()

//I have four of the chunks of code for each array
let json = try NSJSONSerialization.JSONObjectWithData(jSONData, options: .AllowFragments)
            if let dataFile3 = json["data"] as? [[String: AnyObject]] {
                for c in dataFile3 {
                    if let placeName = c["PlaceName"] {
                        place.append((placeName as? String)!)

for var (i,x) in zip(lat, long) {
            for _ in place {
                for _ in pass {
            print(i)
            print(x)
            //print(i + ", " + x)

        // String to double conversion, can I do this when I set the array as a string?
        let lati = NSString(string: i).doubleValue
        let longi = NSString(string: x).doubleValue


        //I have a list of values array so I need to have a for loop that inputs the value over and over again.
        var point = MGLPointAnnotation()
            point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)

            point.title = place
            point.subtitle = pass

        mapView.addAnnotation(point)
            }}}

        //var i and x for loop
        }

    //viewDidLoad
    }

    //creates the markers popup for each point with data
    func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
        // Always try to show a callout when an annotation is tapped.
        return true
    }

如何解決以上錯誤?

編輯:在Santosh的幫助下,這就是我所改變的...

let point = MGLPointAnnotation()

        var i = lat
        var x = long

        //lat and long is served and then all the r,z points, then another lat, long...need to fix this and it may work.
        for (i,x) in zip(lat, long) {
            for aPlace in place {
                for aPass in pass {
            print(i)
            print(x)

        // String to double conversion, can I do this when I set the array as a string?
        let lati = NSString(string: i).doubleValue
        let longi = NSString(string: x).doubleValue

        point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)


            point.title = aPlace
            print(aPlace)


            point.subtitle = aPass
            print(aPass)

        self.mapView.addAnnotation(point)
            }}}

現在可以正常運行,或者至少報告正確的值,但是循環只是保持循環...

嘗試這個:

//I have four of the chunks of code for each array
    let json = try NSJSONSerialization.JSONObjectWithData(jSONData, options: .AllowFragments)
    if let dataFile3 = json["data"] as? [[String: AnyObject]] {
        for c in dataFile3 {
            if let placeName = c["PlaceName"] {
                place.append((placeName as? String)!)

                for var (i,x) in zip(lat, long) {
                    for aPlace in place {
                        for aPass in pass {
                            print(i)
                            print(x)
                            //print(i + ", " + x)

                            // String to double conversion, can I do this when I set the array as a string?
                            let lati = NSString(string: i).doubleValue
                            let longi = NSString(string: x).doubleValue


                            //I have a list of values array so I need to have a for loop that inputs the value over and over again.
                            var point = MGLPointAnnotation()
                            point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)

                            point.title = aPlace
                            point.subtitle = aPass

                            mapView.addAnnotation(point)
                        }}}

                //var i and x for loop
            }

            //viewDidLoad
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM