Post

Replies

Boosts

Views

Activity

Reply to Core Data Make Decodable
I'm having trouble adding this line to my request       decoder.userInfo[.managedObjectContext] = persistentContainer.viewContext() it tells me it doesn't find persistentContainer in scope even though I had persistent container declared in my appDelegate file.    // MARK: CoreData   lazy var persistentContainer: NSPersistentContainer = {     let container = NSPersistentContainer(name: "playerModel")     container.loadPersistentStores(completionHandler: { (storeDescription, error) in       if let error = error as NSError? {         fatalError("Unresolved error \(error), \(error.userInfo)")       }     })     return container   }()
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21
Reply to Core Data Make Decodable
This is the whole class and related methods. @main class AppDelegate: UIResponder, UIApplicationDelegate {     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) - Bool {     // Override point for customization after application launch.     return true   }   // MARK: UISceneSession Lifecycle   func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) - UISceneConfiguration {     return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)   }   func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: SetUISceneSession) {   }           // MARK: CoreData   lazy var persistentContainer: NSPersistentContainer = {     */     let container = NSPersistentContainer(name: "playerModel")     container.loadPersistentStores(completionHandler: { (storeDescription, error) in       if let error = error as NSError? {         fatalError("Unresolved error \(error), \(error.userInfo)")       }     })     return container   }()       func saveContext () {     let context = persistentContainer.viewContext     if context.hasChanges {       do {         try context.save()       } catch {         let nserror = error as NSError         fatalError("Unresolved error \(nserror), \(nserror.userInfo)")       }     }   } }
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21
Reply to Core Data Make Decodable
I can show you the method but I will have to remove the API key. func parseJSON(completed: @escaping () - ()) {     let url = URL(string: "WebsiteLink")     let decoder = JSONDecoder()     decoder.userInfo[.managedObjectContext] = persistentContainer.viewContext() //persistentContainer not in scope     URLSession.shared.dataTask(with: url!) { (data, response, error) in               if error == nil {         do {           self.cPlayerArr = try JSONDecoder().decode([CurrentPlayers].self, from: data!)           let json = try JSONSerialization.jsonObject(with: data!, options: [])           print(json)                       DispatchQueue.main.async {             completed()           }         } catch {           print("JSON Error: ", error)         }       }             }.resume()   }
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21
Reply to Remove Empty Names
I'm wondering how I can modify this function because it prints 0 for both arrays even though I call an API beforehand.    func filterArr() {     cPlayerArrB = cPlayerArr.filter(){       $0.yahooName != ""     }     print(cPlayerArr.count)     print(cPlayerArrB.count)   }
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21
Reply to Modify API to remove empty names.
I filter players based on a certain position using a segmented controller and I see that there are players that have no name in my collection view so I'm thinking since I used my original array to filter them that the problem must be with the API getting the empty names, the json has names with null.
Mar ’21
Reply to Modify API to remove empty names.
Okay just one more thing, I'm trying to handle the nil values in my jerseyNumber but when I do my guard statement it somehow blocks the json from being parsed. Is there another way to deal with nil values from jerseyNumber? struct ApiCurrentPlayers: Decodable {   var PhotoUrl: String?   var FirstName: String?   var LastName: String?   var Position: String?   var Team: String?   var YahooName: String?   var Status: String?   var JerseyNumber: Int64? } extension CurrentPlayers {   convenience init?(context: NSManagedObjectContext, apiCurrentPlayers: ApiCurrentPlayers) {     guard let yahooName = apiCurrentPlayers.YahooName else {       return nil     } guard apiCurrentPlayers.JerseyNumber != nil else {       return nil     }     self.init(context: context)     self.photoUrl = apiCurrentPlayers.PhotoUrl ?? ""     self.firstName = apiCurrentPlayers.FirstName ?? ""     self.lastName = apiCurrentPlayers.LastName ?? ""     self.position = apiCurrentPlayers.Position ?? ""     self.team = apiCurrentPlayers.Team ?? ""     self.yahooName = yahooName     self.status = apiCurrentPlayers.Status ?? ""     self.jerseyNumber = apiCurrentPlayers.JerseyNumber!   } }
Mar ’21
Reply to Modify API to remove empty names.
This is an example of a json object, I can print it but it just does not seem to parse anymore. I had to remove some url fields.  {     BirthCity = Pittsburgh;     BirthDate = "1993-07-11T00:00:00";     BirthState = PA;     Catches = "null";     DepthChartOrder = 5;     DepthChartPosition = C;     DraftKingsName = "Vincent Trocheck";     DraftKingsPlayerID = 607956;     FanDuelName = "Vincent Trocheck";     FanDuelPlayerID = 15283;     FantasyAlarmPlayerID = 400963;     FantasyDraftName = "Vincent Trocheck";     FantasyDraftPlayerID = 1818113;     FirstName = Vincent;     GlobalTeamID = 30000009;     Height = 70;     InjuryBodyPart = Scrambled;     InjuryNotes = Scrambled;     InjuryStartDate = "2021-03-18T00:00:00";     InjuryStatus = Scrambled;     Jersey = 16;     LastName = Trocheck;     PlayerID = 30000226;     Position = C;     RotoWirePlayerID = 3784;     RotoworldPlayerID = 3768;     Shoots = R;     SportRadarPlayerID = "16bf9f68-95f9-4789-a811-80fe6838e632";     SportsDirectPlayerID = 4433;     StatsPlayerID = 607956;     Status = Active;     Team = CAR;     TeamID = 9;     UsaTodayPlayerID = 8256334;     Weight = 183;     XmlTeamPlayerID = 19046;     YahooName = "Vincent Trocheck";     YahooPlayerID = 5431;   }
Mar ’21