0

My project uses an array of computed properties (hourly wages) from which I need to isolate/return only the first 5 objects (wages) in the array. (Note: I have simplified my code for the purpose of isolating this problem, but in so doing it may seem strange that this array is a computed property and not simply a variable. Please accept this oddity as it is necessary in my more complex, complete code.)

Research effort: The Apple Documentation "prefix(upTo:)" would seemingly be a perfect fit for this task. I have referenced the SO question that was asked 7 years ago, but when I apply all the recommendations of all 13 answers, I still get the following errors:

enter image description here

Here is my code with a variety of approaches attempting to accomplish the same result:

class EarningsViewModel: ObservableObject {
    
    var catWageArray: [Double] {
        [81,156,162,166,169,173,177,181,185,192,277,280,282,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285]
    }

    var sliceCareerTest =  catWageArray.prefix(5)
    var sliceCareerTest2 =  catWageArray.prefix(upTo: 5)
    var sliceCareer: [Double] {
        catWageArray.prefix(5)}
    var sliceCareer2: [Double] {
        catWageArray.prefix(upTo: 5)}
    
    var n = 5
    var firstFiveSlice = catWageArray[0..<n]
    let firstFiveArray = Array(firstFiveSlice)

It seems to me that the sliceCareer computed property with the error "No 'prefix' candidates produce the expected contextual result type '[Double]'" may be the closest to the actual solution, but I don't understand why there the compiler cannot find prefix candidates.

1 Answer 1

2

The proper way is prefix(5) but you have to create an array from the slice (aka SubSequence) and it must be a computed property.

On the other hand catWageArray can be a stored property

class EarningsViewModel: ObservableObject {
    
    var catWageArray: [Double] = [81,156,162,166,169,173,177,181,185,192,277,280,282,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285,285]
    
    
    var sliceCareer: [Double] {
        Array(catWageArray.prefix(5))
    }
...
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! You're my hero! Follow-up question: If I want to remove the first 5 instead of isolate, I've tried using "catWageArray.removeSubrange(0..<5)" but get the following 2 errors: "Cannot use mutating member on immutable value: 'catWageArray' is a get-only property" & "Type '()' cannot conform to 'Sequence'" Feel free to respond with an another "Answer" so I can mark it as "accepted" and "useful"
This is simply Array(catWageArray.dropFirst(5)). removeSubrange mutates the receiver in place and it works if you follow my suggestion to declare catWageArray as stored property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.