I'm trying to find a way to remove instances with names as empty strings from my array that receives the data. how do I modify my filter to do that.
Code Block self.cPlayerArr = fillPlayers.filter(){ $0.yahooName != "" }
Code Block func parseJSON(completed: @escaping () -> ()) { let url = URL(string: "") let decoder = JSONDecoder() let appDelegate = UIApplication.shared.delegate as! AppDelegate //<- Intentionally using forced casting. decoder.userInfo[.managedObjectContext] = appDelegate.persistentContainer.viewContext URLSession.shared.dataTask(with: url!) { (data, response, error) in if error == nil { do { let fillPlayers = try decoder.decode([CurrentPlayers].self, from: data!) self.cPlayerArr = fillPlayers.filter(){ $0.yahooName != "" } let json = try JSONSerialization.jsonObject(with: data!, options: []) print(json) DispatchQueue.main.async { completed() } } catch { print("JSON Error: ", error) } } }.resume() }
Code Block extension CurrentPlayers { @nonobjc public class func fetchRequest() -> NSFetchRequest<CurrentPlayers> { return NSFetchRequest<CurrentPlayers>(entityName: "CurrentPlayers") } @NSManaged public var photoUrl: String? @NSManaged public var firstName: String? @NSManaged public var lastName: String? @NSManaged public var position: String? @NSManaged public var team: String? @NSManaged public var yahooName: String? @NSManaged public var status: String? @NSManaged public var jerseyNumber: Int64 } extension CurrentPlayers : Identifiable { }
Thanks for showing the example, but unfortunately, it is shown in plist-like format, not JSON text. That may lose some info compared to the original JSON text.This is an example of a json object,
When you want to get the original JSON text, you may need to put a print statement after guard let data = data else {...}:
Code Block //... guard let data = data else { print("data is nil") return } print(String(data: data, encoding: .utf8) ?? "*Unknown encoding*") //...
But as far as I can see, there is no entry for key JerseyNumber in the example.
Isn't it Jersey that you want to get as an Int64?
Code Block struct ApiCurrentPlayers: Decodable { //# Using Capitalized names to make this `Decodable` easily var PhotoUrl: String? var FirstName: String? var LastName: String? var Position: String? var Team: String? var YahooName: String? var Status: String? var Jersey: Int64? //# Fit the property name to the key name of the JSON } extension CurrentPlayers { convenience init?(context: NSManagedObjectContext, apiCurrentPlayers: ApiCurrentPlayers) { guard let yahooName = apiCurrentPlayers.YahooName else { return nil } guard let jerseyNumber = apiCurrentPlayers.Jersey 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 = jerseyNumber } }