简体   繁体   English

无法将类型“ [String]”的值分配为类型“ String”? 迅捷2

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

I am receiving this error message "Cannot assign value of type '[String]' to type 'String?'" associated with this line "point.title = r" and this line "point.subtitle = z". 我收到与此行“ point.title = r”和此行“ point.subtitle = z”相关联的错误消息“无法将类型'[String]'的值分配给类型'String?'”。 I've tried lots of things like place.description which makes the entire array a 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
    }

How do I fix the error above? 如何解决以上错误?

EDIT: With Santosh's help this is what I changed... 编辑:在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)
            }}}

This is running correctly now or at least reporting the correct values but the loop just keeps a loopin... 现在可以正常运行,或者至少报告正确的值,但是循环只是保持循环...

Try this: 尝试这个:

//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.

相关问题 Swift不能指定类型'()'的值来键入'String?' - Swift cannot assign value of type '()' to type 'String?' 无法将类型“ [String]”的值分配给类型“ String?” (迅速) - Cannot assign value of type '[String]' to type 'String?' (Swift) 错误:无法将类型“[String]”的值分配给 swift 中的“String”类型 - Error: Cannot assign value of type '[String]' to type 'String' in swift 无法将类型“ [String]”的值分配给类型“ [MovieVO]”的值-Swift - Cannot assign a value of type '[String]' to a value of type '[MovieVO]' - Swift 无法将String类型的值分配为AnyObject类型? 斯威夫特3.0 - Cannot assign value of type String to type AnyObject? Swift 3.0 无法分配类型“[ResultItem]?”的值键入“字符串?” Swift 5 - Cannot assign value of type '[ResultItem]?' to type 'String?' Swift 5 不能快速分配“ String”类型的值来输入“ UILabel” - Cannot assign a value of type “String” to type “UILabel” in swift 无法将类型“ [String]”的值分配给类型“ String?” - Cannot assign value of type '[String]' to type 'String?' 无法将类型[[(string,string)]'的值分配给类型[[string:string]' - cannot assign value of type '[(string, string)]' to type '[string : string]' 无法将String类型的值分配为UITextField类型 - Cannot assign a value of type String to type UITextField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM