Promisekit: Error when inserting variable within .then block

Created on 31 Jan 2016  路  2Comments  路  Source: mxcl/PromiseKit

I have a few functions that return promises. if I add any code within the then block, like a variable declaration, my then I get a compile error. I'm pretty confused as to why and any help is appreciated.

screen shot 2016-01-30 at 6 04 50 pm

(no errors above)

screen shot 2016-01-30 at 6 05 17 pm

(above error is)
/Users/matthewchung/Documents/Versame/ios/starling-ios/Starling/Classes/Realm/RealmManager.swift:41:29: Cannot convert return expression of type 'Promise>' to return type 'AnyPromise'

and here is how I am creating my promises

    func createChildWordSamples(filename:String, childName:String) -> Promise<Array<String>> {
        return Promise { fulfill,reject in

        let startedTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
        dispatch_after(startedTime, dispatch_get_main_queue(), {

            if let url = NSBundle.mainBundle().URLForResource(filename, withExtension: "csv") {
                //reading
                do {
                    let text = try NSString(contentsOfURL: url, encoding: NSUTF8StringEncoding)
                    let lines = text.componentsSeparatedByString("\n")
                    let realm = try! Realm()
                    try! realm.write({ () -> Void in

                        for line in lines {
                            let components = line.componentsSeparatedByString(",")
                            var date = self.formatter.dateFromString(components[0])!
                            date = NSDate(timeIntervalSince1970: date.timeIntervalSince1970 + self.delta!)
                            let info:[String:AnyObject] = ["childId":childName, "count":Int(components[1])!, "interval":Int(components[2])!, "timestamp":date]
                            let sample = WordSample(info: info)
                            realm.add(sample, update: false)

                        }
                    })
                    fulfill(lines)
                }
                catch {/* error handling here */}

            }
        })
        }
    }

Most helpful comment

Sorry for the delayed response. If you haven't already, try specifying the return type in your closures that are returning promises. This has historically satisfied the Swift compiler in circumstances like these.

All 2 comments

Sorry for the delayed response. If you haven't already, try specifying the return type in your closures that are returning promises. This has historically satisfied the Swift compiler in circumstances like these.

got it. thanks!

Was this page helpful?
0 / 5 - 0 ratings