What is the cleanest/standard way for writing models in Swift?

I am still a beginner and am currently working on a SwiftUI project. I wanted to know what the standard practices or things to consider while writing models in Swift. I couldn’t find a detailed guide anywhere related to it and I don’t have an educational background in CS. I used a common protocol and three structs conforming to the protocol. I wanted to know if there was a better way for knowledge’s sake.

I have a search results screen with 4 different tabs, Player, Groups, Matches(the 3 categories) and, Recent(common), each with their own associated APIs and views to display the objects. The API responses contain an array of related objects. The Player and Matches APIs have only one type of models in their responses while the Groups array contains a single type of object, but some have an additional field. The Recent API has an array containing all three types of objects and some additional fields to the Player object.

The three screens have different views to show the objects, while the Recent screen’s list uses all three views, but are inside an enclosing view with an additional button to remove the object from the array.

Example of the APIs: (Shorter modified response for example purposes)

Player API

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"status": "SUCCESS",
"message": "Successful response.",
"data": [
{
"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"
}
]
}
</code>
<code>{ "status": "SUCCESS", "message": "Successful response.", "data": [ { "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" } ] } </code>
{
    "status": "SUCCESS",
    "message": "Successful response.",
    "data": [
        {
            "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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>"data": [
{
"id": 1,
"title": "Pool Match 1",
"poolId": 1,
"court": "Santiago Bernabeu",
"searchHistoryId": null,
"category": "MATCHES"
},
...
]
</code>
<code>"data": [ { "id": 1, "title": "Pool Match 1", "poolId": 1, "court": "Santiago Bernabeu", "searchHistoryId": null, "category": "MATCHES" }, ... ] </code>
"data": [
        {
            "id": 1,
            "title": "Pool Match 1",
            "poolId": 1,
            "court": "Santiago Bernabeu",
            "searchHistoryId": null,
            "category": "MATCHES"
        },
        ...
   ]

Groups API (Some objects have an additional field based on their subCategory).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>"data": [
{
"id": 1,
"title": "Mavericks",
"subCategory": "TEAM",
},
{
"id": 2,
"title": "East Side",
"subCategory": "cluster",
"subCategoryGroupID": 4
}
]
</code>
<code>"data": [ { "id": 1, "title": "Mavericks", "subCategory": "TEAM", }, { "id": 2, "title": "East Side", "subCategory": "cluster", "subCategoryGroupID": 4 } ] </code>
"data": [
        {
            "id": 1,
            "title": "Mavericks",
            "subCategory": "TEAM",
        },
        {
            "id": 2,
            "title": "East Side",
            "subCategory": "cluster",
            "subCategoryGroupID": 4
        }
   ]

Recent API:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>"data": [
{
"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
}
]
</code>
<code>"data": [ { "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 } ] </code>
"data": [
        {
            "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 common field and conforming structs in my code.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>protocol SearchItem {
id: Int { get set }
category: String { get set }
}
struct PlayerSearchItem: SearchItem {
id: Int
category: String
playerName: String
...
}
struct MatchesSearchItem: SearchItem {
...
</code>
<code>protocol SearchItem { id: Int { get set } category: String { get set } } struct PlayerSearchItem: SearchItem { id: Int category: String playerName: String ... } struct MatchesSearchItem: SearchItem { ... </code>
protocol SearchItem {
    id: Int { get set }
    category: String { get set }
}

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

struct MatchesSearchItem: SearchItem {
...

In the Recent screen, I stored the data in an array [SearchItem] and inside the container view, in a switch case based on the ‘category’ type, cast the object as! PlayerSearchItem or as! MatchesSearchItem and then pass it to the corresponding view.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> switch data.category {
case SearchCategory.Player.rawValue:
let playerData: PlayerSearchItem = data as? PlayerSearchItem
PlayerSearchItemView(data: playerData)
case SearchCategory.Matches.rawValue:
let matchData: MatchesSearchItem = data as? MatchesSearchItem
MatchesSearchItemView()
...
</code>
<code> switch data.category { case SearchCategory.Player.rawValue: let playerData: PlayerSearchItem = data as? PlayerSearchItem PlayerSearchItemView(data: playerData) case SearchCategory.Matches.rawValue: let matchData: MatchesSearchItem = data as? MatchesSearchItem MatchesSearchItemView() ... </code>
 switch data.category {
 case   SearchCategory.Player.rawValue:
        let playerData: PlayerSearchItem = data as? PlayerSearchItem
                    PlayerSearchItemView(data: playerData)
                    
 case SearchCategory.Matches.rawValue:
        let matchData: MatchesSearchItem = data as? MatchesSearchItem
        MatchesSearchItemView()
...         

It would be helpful if you could provide insights on how I could improve this. Also about crucial things to consider in general like while writing data models or handling API response models and their corresponding data models. Would using classes be better?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật