ios swift - 函数导致索引需要永远而不是构建[重复]

Posted

技术标签:

【中文标题】ios swift - 函数导致索引需要永远而不是构建[重复]【英文标题】:ios swift - functions causes indexing to take forever and not build [duplicate] 【发布时间】:2017-09-21 17:59:07 【问题描述】:

希望有人对此有答案。

我花了几个小时弄清楚为什么 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】:

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

var newStudent = [String: String]()
newStudent["objectId"] = student["objectId"] as? String ?? ""
// etc...

【讨论】:

好的,所以先创建一个字典,然后将它传递给 Struct 对吗?我会试试的。谢谢! 查看medium.com/@RobertGummesson/… 并使用那里提到的工具也是有效的。

以上是关于ios swift - 函数导致索引需要永远而不是构建[重复]的主要内容,如果未能解决你的问题,请参考以下文章

索引-mysql

Swift - 检测应用程序何时发送到后台而不是设备锁定时

Swift 中的 Error Handling

Swift/iOS - Firebase 远程配置获取值永远不会完成

超越Objective-C,超越Swift

Swift:Xcode 6.1.1 更改由代码块执行而不是按钮触发的视图