Giter Club home page Giter Club logo

Comments (3)

xxtesaxx avatar xxtesaxx commented on July 17, 2024

Hello,
I'm completely new to Bolts and also a beginner in swift so maybe this might be completely wrong but wouldn't it be possible to create a struct or class called e. g. "MyTaskResult" to wrap your actual result?

I just gave it a quick try and it seems to work:

func printHelloWorld() {
    doSomething().continueWith() { task in
        print(task.result?.result)
    }
}

func doSomething() -> Task<MyResult> {
    let tcs = TaskCompletionSource<MyResult>()
    ...
    tcs.set(result: MyResult(result: "Hello World" as AnyObject))
    ...
    return tcs.task
}

struct MyResult {
    var result: AnyObject
}


Surely there is a better way of doing this but this, I think, could at least work somehow.

from bolts-swift.

appacea avatar appacea commented on July 17, 2024

Hi. Any updates on this? I'm running into the same problem.

I'm porting my code from Objc to Swift and have a function like this:

(void) handleException:(BFTask*)task{}

converted to Swift:

func handleException(_ task: Task)()

In ObjC the input can be any BFTask but in Swift I get the "Task cannot be caster to Task" error mentioned above by @adrianhr91 .

@xxtesaxx solution is a smart approach but requires the use of AnyObject in the input rather than a specific type.

from bolts-swift.

appacea avatar appacea commented on July 17, 2024

I ended up going a different route, by using an extension I can create a function in the Task Class that can be called from any generic Task. So instead of a function that takes a generic Task as input, I have a function that is a prototype of a generic Task.

extension Task{
func handleException()()
}

This way handleException() can be called on any Task

Hope that helps someone else.

from bolts-swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.