Leverage Xcode for APCSP - Using Swift for the Performance Task

The performance task in the APCSP course is a large portion of their exam, but finding examples of these projects coded in Swift so they can be analyzed by our learners is tough. Thats why I have created a sample that meet all the criteria that you can utilize in your classroom! With this sample you will be able to analyze an app written in Swift alongside your learners similar to those presented on the CollegeBoard site except it will be in the same language your learners have been focusing on the whole time. Below you will find a brief description of the key parts of program that you can download from this post along with some ways that you can modify it to make it more meaningful to you and your learners.

How the Data is Held

The minimum requirement for data in a students program is to has a list that is being used in some way throughout the use of the app and other way that we can make this hold more data is making that list one the holds a custom structure. In the sample project provided, this page holds all of the data that I being analyzed along with the created structure that holds all of the information for the classes that will be in the catalog.

//Define enum
enum classType: String {
    case math = "Math"
    case english = "English"
    case history = "History"
}

//Define struct
struct Course {
    let name: String
    let subject: classType
    let prereq: Bool
    let grade: Int
}

//List of things
let catalog = [Course(name: "Alg1", subject: .math, prereq: false, grade: 9),
Course(name: "Alg2", subject: .math, prereq: true, grade: 11),
Course(name: "Eng1", subject: .english, prereq: false, grade: 9),
Course(name: "HEng1", subject: .english, prereq: true, grade: 9),
Course(name: "WGeo", subject: .history, prereq: false, grade: 9),
Course(name: "APHG", subject: .history, prereq: true, grade: 9),
Course(name: "APCalc", subject: .math, prereq: true, grade: 12),
Course(name: "Gov", subject: .history, prereq: false, grade: 12)]

This structure has a few different types of data being used: Boolean, String, Int, and the custom classType enumeration that was made to hold the different subject matter courses that were selectable. The objective of coding all of this different data is that we can sort out classes that are available to a certain grade level of learner in a given subject matter. 

Sorting Through the Data

The qualification for the function that the learners have to produce in their project is one that contains a parameter that allows for the function to be used in a variety of ways, a loop, and selects based off a criteria, most likely the parameter of the function. The function from the attached program can be seen below.

*Note: Some of the colors in the code segment below are not what they should be, but if copied into Xcode they will return to the proper coloring.

//Function for sorting catalog
    func sortCatalog(sub: classType, prereq: Bool, grade: Int) {
            sortedList = []
            for course in catalog {
                if course.subject == sub && course.prereq == prereq && course.grade == grade {
                    sortedList.append(course.name)
                }
                else {
                    resultsLabel.text = "No courses offered meet that criteria"
                }
            }
            resultsLabel.text = "There are \(sortedList.count) courses that meet your requested
criteria: \(sortedList.joined(separator: ", "))"
        }

The parameters in this program are what end up leading to the user-selected data to be shown on screen after the button has been pressed. In this case there are three of them being utilized but there only needs to be one of them to meet the requirements. But you can see after running the app that different courses will populate to a new list if there are any in the catalog that meet the requested information. There is also a null message that will pop up if there is not anything and it will let you know that there are 0 courses that meet that criteria. 

Make it Your Own

Change the Subject Matter

If sorting through a catalog of courses isn’t something that you or your learners would be into, you can turn this program into something that sorts through other material. You would need to adjust the information found on the Struct page of the program. Change the enum/struct to the information that you would like and then rebuild the list with information that reflects it. I’ve added a sample below of what a different topic might look like. 

*Note: Some of the colors in the code segment below are not what they should be, but if copied into Xcode they will return to the proper coloring.

//Park enum
enum parkType {
    case state
    case national
    case local
}

//Park Structure
struct Park {
    let name: String
    let type: parkType
    let location: String
    let size: Int
}


//Park List
let parkList = [Park(name: "Yosemite", type: .national, location: "CA", size: 759620),
Park(name: "Governor Thompson", type: .state, location: "WI", size: 2800),
Park(name: "High Cliff", type: .state, location: "WI", size: 1187),
Park(name: "Prospect Park", type: .local, location: "CA", size: 11),
Park(name: "Arches", type: .national, location: "UT", size: 76680)]

You will have to make adjustments to the sorting function however after doing this; making sure that the new enum and struct are properly represented in the sorting function found on the View Controller.

Fill Out the Catalog More

Tired of running the simulation to only see one or no options show up in the results? Feel free to add more options to the catalog list on the Struct page of the program. See if you can get options to show up for each setting or have multiple show up under a given setting.

Adjust the UI

Adjust the buttons and switches used on the storyboard! Change up the colors and text to make it look the way that you would like. This is your chance to enhance the user experience and make the app look however you envision it.

Attachments

1 reply

July 11, 2024

Really useful resource. Thank you for sharing!

This post contains content from YouTube.

If you choose to view this content, YouTube may collect and process certain personal data. You can view YouTube’s <a href="https://www.youtube.com/t/privacy" target="_blank">privacy policy here<span class="a11y">(opens in new window)</span>.</a>

This post contains content from YouTube.

You have rejected content from YouTube. If you want to change your consent, press the button below.