-2

i've been struggling with this function here :P i hope someone could help me with it.

so my function retrieve some kind of data (double) from parse. the problem is i can't return the value of mark.

this is a picture of my error after updating the code : https://i.stack.imgur.com/UVeQG.png thanks in advance

func monday() -> Double{

    let student:PFObject = currentObject!
    let name:String = (student["student_name"] as? String)!

    let query = PFQuery(className: "progress")
    query.whereKey("student", equalTo: name)
    query.skip = 4
    query.getFirstObjectInBackgroundWithBlock{
        (objects , error) -> Void in
        if error == nil {

            let mark = objects?.objectForKey("average") as! Double
            print(mark)
              // i want to return the value of -> mark              
        }

    }
    return query  // error here : cannot convert return expression of type PFQuery to return type Double
}
hamdi islam
  • 1,327
  • 14
  • 26

3 Answers3

1

Here's how I would do it

func monday(completionHandler: (Double) -> Void){

let student:PFObject = currentObject!
let name:String = (student["student_name"] as? String)!

let query = PFQuery(className: "progress")
query.whereKey("student", equalTo: name)
query.skip = 4
query.getFirstObjectInBackgroundWithBlock{
    (objects , error) -> Void in
    if error == nil {

        let mark = objects?.objectForKey("average") as! Double
        completionHandler(mark)
          // i want to return the value of -> mark              
    }

}

}

And then when you're trying to use the function:

var markvalue: Double!
    monday(completionHandler: {mark in 

          print(mark)

//edit: if you want to set mark as a variable
markvalue = mark
    })
John D
  • 1,435
  • 2
  • 16
  • 24
0

The function should be returning a Double, but it's returning query, a PFQuery. I think you meant to return mark.

Update: Oh, I see now: mark is inside the block, which presumably is executing after this function has returned. You're going to have to rethink the way you do this. Hard to know the best way to handle it without knowing more about how your code works.

zpasternack
  • 17,838
  • 2
  • 63
  • 81
0

First of all PFQuery is not the Double. But your main problem that your function end and return some value earlier than executing the closure's body. You don't need to return value from this function. You need to call some other function with mark value that need to use it inside the closure like this:

query.getFirstObjectInBackgroundWithBlock{ (objects , error) -> Void in
    if error == nil {
        let mark = objects?.objectForKey("average") as! Double 
        // Here call other function 
        self.someFunc(mark)             
    }
}

EDIT:

Your code may looks like this after apdate:

func monday() {

    let student:PFObject = currentObject!
    let name:String = (student["student_name"] as? String)!

    let query = PFQuery(className: "progress")
    query.whereKey("student", equalTo: name)
    query.skip = 4
    query.getFirstObjectInBackgroundWithBlock{
        (objects , error) -> Void in
        if error == nil {

            let mark = objects?.objectForKey("average") as! Double
            self.test(mark)              
        }
    }
}

func test(value: Double) {
    // Here you work with mark value
}

You don't need to return value from all this functions. You need to transfer you code after calling monday and return value from it (in your current code) to test

Alexey Pichukov
  • 3,377
  • 2
  • 19
  • 22
  • hi, thanx for your reply, i created a func to pass in the value `func test(number:Double) -> Double { return number } ` – hamdi islam Nov 20 '15 at 20:22
  • what should i return at the last return line ? `let mark = objects?.objectForKey("average") as! Double print(mark) self.test(mark) } } return // value ? } ` – hamdi islam Nov 20 '15 at 20:24
  • @inzo I think you do not understood what I meant. This function `test` in your case is the logical place where you need to work with `mark` data. For example you have some code where you call `monday` function and then after it return value you wand to do some actions with this value. In this case you need to replace this some actions to `test` function. This `test` function may not return any value. It is the place where you will work with `mark` data. About return value from `monday`: you don't need to return value from it. `func monday() {...}` – Alexey Pichukov Nov 20 '15 at 20:29
  • oh ok i understand now :) i will give it a try . thank you for your time @alex_p yourit was very helpful – hamdi islam Nov 20 '15 at 20:34