0

I am new to swift ui and am learning how to make an api call using alomofire.

I am from a csharp so bare with me if this is wrong I have a class that calls my api

My Understanding is I need a completion handler the data that is returned async so Im not sure what I need to do to extract the json properly and have a view model the following is my code

class webapilib{
var apiURLBase = "https://secreturl.com/api/"
init() {
        
}
func getFriends(completionHandler:  ) {
    performRequest()
}
func   performRequest () {
    let  request =  apiURLBase
    var url =  request + "Friends/GetAllParentsFriends?parentId=1"
    AF.request(url).responseData { response in
            switch response.result {
            case .success(let value):
                return String(data: value)
            case .failure(let error):
                print(error)
            }
}

Example data

{"parents":[{"id":1,"type":null,"firstName":"Murry","groupId":null,"children":null,"emmailAddress":null,"surname":"Goldberg","name":null,"isParent":null,"isDeleted":false,"isActive":true,"createdBy":null,"lastModifiedBy":null,"lastUpdatedDate":null,"createdDate":null}],"children":[{"id":24864,"type":null,"orginizationId":null,"personId":null,"coachId":null,"teamId":null,"playerLevel":"A","fullName":"Audrey Lind","firstName":"Audrey","surname":"Lind","year":2008,"weight":null,"gender":null,"photo":null,"orgId":null,"ageGroup":null,"age":null,"emailAddress":"tandklind@msn.com","conditioningWorkouts":null,"conditioningWorkout":null,"bookings":null,"bikeWorkOuts":null,"isDeleted":false,"notes":null,"defaultTB":150.00,"defaultOP":15.00,"defaultPU":0.00,"defaultPB":null,"defaultBP":15.00,"defaultTBReps":5.00,"defaultOPReps":0.00,"defaultPUReps":10.00,"defaultPBReps":null,"defaultBPReps":0.00,"defaultAdvancedPuReps":0.00,"defaultAdvancedPu":0.00,"defaultPuSeconds":null,"defaultBroadJumpFeet":null,"defaultBroadJumpInches":null,"defaultTwentyFiveYards":null,"defaultOneFityYards":null,"status":0,"isActive":true,"createdBy":"System Import","lastModifiedBy":"Brandon","lastUpdatedDate":"2022-03-29T15:00:28.944125","createdDate":"2021-12-17T01:07:01.1819126"},{"id":24866,"type":null,"orginizationId":null,"personId":null,"coachId":null,"teamId":null,"playerLevel":"A","fullName":"Ellarae Atkinson","firstName":"Ellarae","surname":"Atkinson","year":2009,"weight":null,"gender":null,"photo":null,"orgId":null,"ageGroup":null,"age":null,"emailAddress":"atkinson_06@comcast.net","conditioningWorkouts":null,"conditioningWorkout":null,"bookings":null,"bikeWorkOuts":null,"isDeleted":false,"notes":null,"defaultTB":55.00,"defaultOP":20.00,"defaultPU":0.00,"defaultPB":null,"defaultBP":15.00,"defaultTBReps":2399.00,"defaultOPReps":0.00,"defaultPUReps":0.00,"defaultPBReps":null,"defaultBPReps":0.00,"defaultAdvancedPuReps":0.00,"defaultAdvancedPu":0.00,"defaultPuSeconds":null,"defaultBroadJumpFeet":null,"defaultBroadJumpInches":null,"defaultTwentyFiveYards":null,"defaultOneFityYards":null,"status":0,"isActive":true,"createdBy":"System Import","lastModifiedBy":null,"lastUpdatedDate":"2022-02-09T07:09:32.994522","createdDate":"2021-12-17T01:07:09.6571143"}]}

This is how am calling my from my button I eventually want this data in a list of some kind with card views

func callApi()
{
  let api  = thehockeylabapi().self      
  let test: () = api.getFriends()
}

my button click handler

Button("getFriends", action: callApi)

Again sorry for any messy code this is my first full week of learning so thanks in advance

c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • Have a look at this info: https://stackoverflow.com/help/how-to-ask and the first few words are, `Search, and research`. Have you search for completion handler and found nothing at all? – workingdog support Ukraine Sep 10 '22 at 01:05
  • Since you come from C#, I guess you should have use `Action`(from what I guess: https://stackoverflow.com/questions/5823770/objective-c-code-blocks-equivalent-in-c-sharp )? In Objective-C, it's called `block` and in Swift, it's called `closure`. So I guess that the keyword you were missing is "closure" instead of "completion handler" to find equivalent in C# to understand the concept. – Larme Sep 10 '22 at 09:48

0 Answers0