ios swift - 函数导致索引永远占用而不构建

时间:2017-09-21 17:59:08

标签: ios json swift xcode

希望有人有这个答案。

我花了几个小时搞清楚为什么xcode会永远编译,以及为什么它会被困在构建/索引中。我发现这是因为这两个功能:

public func setLocationsToSession(view: UIViewController, Completion callback: @escaping (_ status: String) -> Void) {
    getStudentLocations(view: view){(error, data) in
        print("setting session")
        print(data)

        if let results = data!["results"] {
            for student in results as! [AnyObject] {
                // check the object for the required keys/subscripts
                if let objectId = student["objectId"], let uniqueKey = student["uniqueKey"], let fname = student["firstName"], let lname = student["lastName"], let mapString = student["mapString"], let mediaURL = student["mediaURL"], let lat = student["latitude"], let lng = student["longitude"], let createdAt = student["createdAt"], let updatedAt = student["updatedAt"] {
                    // some other students are forgetting to POST/PUT the required key-value pairs in their request(s), 
                    // and it breaks my code/app. So this is why i have all of these optional defaults
                    if(fname as String == "" || lname as String == "" || lat as String == "" || lng as String == "" || lat == 0 || lng == 0) {
                        // ignore the bad ones
                        continue
                    }
                    let newStudent = Student(dict: [
                        "objectId": objectId as? String ?? "",
                        "uniqueKey": uniqueKey as? String ?? "",
                        "firstName": fname as! String,
                        "lastName": lname as! String,
                        "mapString": mapString as? String ?? "",
                        "mediaURL": mediaURL as? String ?? "",
                        "latitude": lat as! Double ?? 0.0,
                        "longitude": lng as? Double ?? 0.0,
                        "createdAt": createdAt as? String ?? "",
                        "updatedAt": updatedAt as? String ?? ""
                        ])
                    session.studentLocations.append(newStudent)
                }
                else {
                    continue
                }

            }
        }
        else {
            let value = keys.error
            callback(value)
        }

        let value = error == true ? keys.error : keys.success
        callback(value)
    }
}

public func getStudentLocation(view: UIViewController, key: String, Completion callback: @escaping (_ Error: Bool) -> Void) {
    var reqDICT: [String: Any] = [String:Any]()

    reqDICT["url"] = "https://parse.udacity.com/parse/classes/StudentLocation?where={\"uniqueKey\":\"\(key)\"}"
    reqDICT["method"] = "GET"

    let request = NSMutableURLRequest(url: URL(string: escapeString(value: reqDICT["url"] as! String))!)
    request.httpMethod = reqDICT["method"] as! String
    request.addValue("application/json", forHTTPHeaderField: "Accept")
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("QrX47CA9cyuGewLdsL7o5Eb8iug6Em8ye0dnAbIr", forHTTPHeaderField: "X-Parse-Application-Id")
    request.addValue("QuWThTdiRmTux3YaDseUSEpUKo7aBYM737yKd4gY", forHTTPHeaderField: "X-Parse-REST-API-Key")
    reqDICT["request"] = request

    httpRequest(view: view, requestDICT: reqDICT) {(error, data) in
        print("error: \(error) ; data: \(data)")

        if let results = data!["results"], results.count > 0 {
            print("results exist: \(results.count)")
            let Res = results as! [[String:AnyObject]]
                let student = Res[0] as! AnyObject
                print("results: \(results)")
                print("student: \(student)")
                let newStudent = Student(dict: [
                    "objectId": student["objectId"] as? String ?? "",
                    "uniqueKey": student["uniqueKey"] as? String ?? "",
                    "firstName": student["firstName"] as? String ?? "",
                    "lastName": student["lastName"] as? String ?? "",
                    "mapString": student["mapString"] as? String ?? "",
                    "mediaURL": student["mediaURL"] as? String ?? "",
                    "latitude": student["latitude"] as? Double ?? 0.0,
                    "longitude": student["longitude"] as? Double ?? 0.0,
                    "createdAt": student["createdAt"] as? String ?? "",
                    "updatedAt": student["updatedAt"] as? String ?? ""
                    ])
                session.you = newStudent
                session.objID = student["objectId"] as! String
                session.dataExists = true

        }
        else {
            session.dataExists = false
        }

        callback(error)
    }

}

我创建了这些函数来处理来自HTTP请求的JSON响应。问题是并非结果数组中的所有json对象都具有相同的键;有些人没有别人做过的钥匙。这就是为什么在我编写的这两个函数中,我试图在if let语句中解开JSON对象中的键,并在我实例化新的struct时阻止nil。这就是导致xcode永远编译的原因,因为当我评论这两个时,它在几秒钟内编译/构建/运行。我还能怎么做呢?谢谢!

1 个答案:

答案 0 :(得分:1)

这是字典文字。如果您想加快速度,请以正常方式创建字典,然后添加所有项目,例如:

$ which php
$ /usr/local/bin/php