从Swift3中的json解析数组的奇怪结果

时间:2017-04-08 17:13:49

标签: arrays json swift3

大家好我不知道为什么数组Places返回奇怪的值,如0x6080004b3aa0,而不是显示我的JSON网址的标题,坐标和副标题。谢谢你的帮助!

import MapKit

@objc class Place: NSObject {


    var title: String?

    var coordinate: CLLocationCoordinate2D
    var subtitle: String?

    init(title:String,subtitle:String, coordinate:CLLocationCoordinate2D){

       self.title = title
        self.coordinate = coordinate
        self.subtitle = subtitle

    }

    static func getPlaces() -> [Place] {
      guard  let url = NSURL(string: "https://script.googleusercontent.com/macros/echo?user_content_key=Z-LfTMdhgAg_6SRd-iMucSyWu-LFBQO8MLxJZ6DPcL05Rtr3joCCypWD2l46qaegSpVpVINc1DLl5inoDOgGx3p3ANpY1AkGOJmA1Yb3SEsKFZqtv3DaNYcMrmhZHmUMWojr9NvTBuBLhyHCd5hHa1ZsYSbt7G4nMhEEDL32U4DxjO7V7yvmJPXJTBuCiTGh3rUPjpYM_V0PJJG7TIaKp4bydEiKBUZP6fpOyGJIhkmEGneM7ZIlWloTVbXmkjs15vHn8T7HCelqi-5f3gf3-sKiW3k6MDkf31SIMZH6H4k&lib=MbpKbbfePtAVndrs259dhPT7ROjQYJ8yx") else { return [] }

        let request = NSMutableURLRequest(url: url as URL!)
         var places = [Place]()
        let task = URLSession.shared.dataTask(with: request as URLRequest) {data,response,error in
            guard error == nil && data != nil else {
                print ("Error:",error)
                return
            }
            let httpStatus = response as? HTTPURLResponse
            if httpStatus?.statusCode == 200
            {  if data?.count != 0
            {
                let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary


                let contacts = responseString["Sheet1"] as? [AnyObject]

                for contact in contacts!{
                    var places = [Place]()
                    let title = contact["name"] as! String
                    let subtitle = contact["description"] as? String
                    let latitude = contact["latitude"] as? Double ?? 0, longitude = contact["longitude"] as? Double ?? 0


                    let place = Place(title:title,subtitle:subtitle!,coordinate: CLLocationCoordinate2DMake(latitude, longitude))
                     places.append(place)
                    print(latitude)
                    print(place)
                }
             }
            else {
                print("No data got from url")
                }

            } else {
                print("error httpsatus code is:", httpStatus!.statusCode)
            }
        }
        task.resume()


     return places as [Place]
    }


}

我认为问题是:

  

let place = Place(标题:标题,副标题:副标题!,坐标:CLLocationCoordinate2DMake(纬度,经度))

当我print(place)时,它会返回奇怪的结果

1 个答案:

答案 0 :(得分:0)

当你创建一个来自NSObject的子类的类时,你正在创建一个由Objective-c类支持的对象 - 在某些情况下这可能非常有用(最常见的用途是当你想要获取您的对象并将其存档为二进制数据blob。)

我猜测在你的情况下,你可能不想/需要继承NSObject

这是一个简单的示例,用于显示正在发生的事情:

这是由NSObject支持的课程:

@objc class ObjCPlace: NSObject {
    let name: String
    init(name: String) {
        self.name = name
    }
}

如果您创建此对象的实例并尝试打印内容 - 就像您找到的那样,您将获得内存中的对象位置:

let testObjcPlace = ObjCPlace(name: "New York")
print(testObjcPlace)
// prints: 
// <__lldb_expr_310.ObjCPlace: 0x600000055060>

替代使用print可以使用dump来更详细地查看您的对象:

let testObjcPlace = ObjCPlace(name: "New York")
dump(testObjcPlace)
// Prints:
// ▿ <__lldb_expr_310.ObjCPlace: 0x600000055060> #0
//   - super: NSObject
//   - name: "New York"

但是你可能只想创建一个Swift类(或者在这个例子中为一个结构,看看this question and answers来解释差异),而不是创建一个NSObject子类。

struct Place {
    let name: String
    init(name: String) {
        self.name = name
    } 
}

因为此对象不是Objective-c对象,所以您可以使用print并获取打印的内部属性:

let testPlace = Place(name: "New York")
print(testPlace)
// Prints: 
// Place(name: "New York")  

欢迎来到StackOverflow!