Search code examples
iosarraysswiftposition

find the position of an element in an array with PFFile. Swift


My below code works if orgArray is [String] and return a [String]. My question is how can I change it from String to PFFile like below. Below code has an error that highlighted orgArray[count] and return tempArray. Is there any way I can find the position of an element in an array with PFFile.

Thanks

func seperateArrayByTrueAndFalsePFFile (orgArray: [PFFile], arrayTrueFalse: [Bool], byBool: Bool) -> [PFFile] {
    var count: Int = 0
    var tempArray = [String]()
    while count != orgArray.count {
        if arrayTrueFalse[count] == byBool {
            tempArray.append(orgArray[count])
        }
        count += 1
    }
    return tempArray
}

Solution

  • tempArray is initialized to be an array of String (var tempArray = [String]()) but you are returning it from your function which says it returns an array of type [PFFile]. That's one error.

    orgArray is an array of PFFiles and you're trying to pull one of its elements to put it into tempArray which expects to receive a String. Type String and type PFFile are not interchangeable. Likewise type [String] and type [PFFile] are not interchangeable.