0

finalRestaurantArray is an array of PFObjects which contains the array CloseHours for each object. CloseHours contains the closing time for each day of the week [0-6].

How can I create an array that contains the value of CloseHours[dayOfWeek] for each object. If it is Tuesday (let dayOfWeek = 1) the array should look like:

 [0015, 2350]

Current Attempt:

    //create array of CloseHours
    let initialCloseRestaurantHours = finalRestaurantArray.map { $0.objectForKey("CloseHours") as [String] }
    //get the close hour for given day
    let closeRestaurantHours = initialCloseRestaurantHours.map { $0.objectAtIndex(dayOfWeek) as String }
    //Error: [string] does not have a member named objectAtIndex 

Additional Info

This is the array of Objects finalRestaurantArray

[<Restaurant: 0x17411aca0, objectId: LA74J92QDA, localId: (null)> {
    Name = "First One";
    CloseHours =     (
        0005,
        0015,
        0025,
        0035,
        0045,
        0055,
        0065
    );
}, <Restaurant: 0x17411b480, objectId: 0aKFrpKN46, localId: (null)> {
    Name = "Second One";
    CloseHours =     (
        0015,
        2350,
        2350,
        2350,
        2350,
        2350,
        2350
    );
}]

1 Answer 1

1

Instead of $0.objectAtIndex(dayOfWeek) try $0[dayOfWeek]. $0 is now a Swift array of type [String] and Swift arrays do not have the objectAnIndex: method.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry that was a typo. and thanks for the heads up. now i'm assuming this isn't the ideal way to accomplish what i'm trying to do?
If your goal is to get an array of close times per day then it works well. The only thing that was slightly confusing was that initialCloseRestaurantHours was an array of arrays. Perhaps naming it closeRestaurantHoursArrays or maybe adding a comment that its an array of string arrays might be useful?

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.