Wednesday, June 26, 2024
HomeiOS Developmentios - What's the cleanest/commonplace method for writing fashions in Swift?

ios – What’s the cleanest/commonplace method for writing fashions in Swift?


I’m nonetheless a newbie and am presently engaged on a SwiftUI challenge. I wished to know what the usual practices or issues to contemplate whereas writing fashions in Swift. I could not discover a detailed information anyplace associated to it and I haven’t got an academic background in CS. I used a standard protocol and three structs conforming to the protocol. I wished to know if there was a greater method for information’s sake.

I’ve a search outcomes display screen with 4 completely different tabs, Participant, Teams, Matches(the three classes) and, Current(frequent), every with their very own related APIs and views to show the objects. The API responses include an array of associated objects. The Participant and Matches APIs have just one sort of fashions of their responses whereas the Teams array incorporates a single sort of object, however some have a further area. The Current API has an array containing all three varieties of objects and a few further fields to the Participant object.

The three screens have completely different views to point out the objects, whereas the Current display screen’s record makes use of all three views, however are inside an enclosing view with a further button to take away the item from the array.

Instance of the APIs: (Shorter modified response for instance functions)

Participant API

{
    "standing": "SUCCESS",
    "message": "Profitable response.",
    "information": [
        {
            "id": 7, //Mandatory field
            "playerName": "Ronaldo", //Mandatory field
            "nickname": "CR7",
            "positions": "Striker, Winger, Forward",
            "place": "Portugal",
            "height": null,
            "category": "PLAYER" //Mandatory field
        },
        {
            "id": 10,
            "playerName": "Messi",
            "nickname": null,
            "positions": "Winger, Forward",
            "place": "null",
            "height": null,
            "category": "PLAYER"
        }
    ]
}

** Matches** API

"information": [
        {
            "id": 1,
            "title": "Pool Match 1",
            "poolId": 1,
            "court": "Santiago Bernabeu",
            "searchHistoryId": null,
            "category": "MATCHES"
        },
        ...
   ]

Teams API (Some objects have a further area based mostly on their subCategory).

"information": [
        {
            "id": 1,
            "title": "Mavericks",
            "subCategory": "TEAM",
        },
        {
            "id": 2,
            "title": "East Side",
            "subCategory": "cluster",
            "subCategoryGroupID": 4
        }
   ]

Current API:

"information": [
        {
            "id": 1,
            "title": "Pool Match 1",
            "poolId": 1,
            "court": "Santiago Bernabeu",
            "searchHistoryId": null,
            "category": "MATCHES"
        },
        {
            "id": 1,
            "title": "Mavericks",
            "subCategory": "TEAM",
            "category": "GROUPS",
            "subCategoryGroupID": null,
            "subCategoryURL": null
        },
        {
            "id": 7,
            "playerName": "Ronaldo",
            "nickname": "CR7",
            "positions": "Striker, Winger, Forward",
            "place": "Portugal",
            "height": null,
            "category": "PLAYER",
            "injuryStatus": "Healthy", //Additional field
            "bloodGroup": null //Additional field
        }
   ]

I utilised a protocol with the frequent area and conforming structs in my code.

protocol SearchItem {
    id: Int { get set }
    class: String { get set }
}

struct PlayerSearchItem: SearchItem {
    id: Int
    class: String
    playerName: String
    ...
}

struct MatchesSearchItem: SearchItem {
...

Within the Current display screen, I saved the information in an array [SearchItem] and contained in the container view, in a swap case based mostly on the ‘class’ sort, forged the item as! PlayerSearchItem or as! MatchesSearchItem after which go it to the corresponding view.

 swap information.class {
 case   SearchCategory.Participant.rawValue:
        let playerData: PlayerSearchItem = information as? PlayerSearchItem
                    PlayerSearchItemView(information: playerData)
                    
 case SearchCategory.Matches.rawValue:
        let matchData: MatchesSearchItem = information as? MatchesSearchItem
        MatchesSearchItemView()
...         

It will be useful when you might present insights on how I might enhance this. Additionally about essential issues to contemplate normally like whereas writing information fashions or dealing with API response fashions and their corresponding information fashions. Would utilizing courses be higher?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments