I will close this thread I have a better idea.
Post
Replies
Boosts
Views
Activity
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)
}
The error went away on it's own I think it might be a bug.
Alright thank you I was able to fix my other issues so I can close this thread.
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)")
}
}
}
}
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()
}
It's in another file I call PlayersVC. I have it written in my get request method in the beginning before the receive data process.
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
}()
Okay so I have to modify my get request then.
I tried your solution but I can only get it to scroll vertically. Even if I were to make the leading of the scrollview larger it still would not give me horizontal scroll.
I'm going to close this thread.
I can transfer the data using the prepare method like so in my hockeyDetailVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "r" {
let destinationController = segue.destination as! FavouritesVC
destinationController.currentFav = item!
}
}
but I am still curious about why my protocol is not transferring the data I want to send. I copied
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "r" {
print("segue identifier", segue.identifier)
let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
hockeyDetailVC.delegate = self
print("destination HockeyDetailVC is", hockeyDetailVC)
}
}
It does not print the segue.identifier it prints the delegate is nil and then it just crashes again. It does not matter what I change my segue.identifier too.
Even if I place it outside the r it still does not print I may have to do this a different way.
This is how I define the function
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "r" {
print("segue identifier", segue.identifier)
let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
hockeyDetailVC.delegate = self
print("destination HockeyDetailVC is", hockeyDetailVC)
}
}
I don't use an IB but I have an identifier I named r in the storyboard.
Okay I can't do segue.destination as? HockeyDetailVC but is (segue.destination as? HockeyDetailVC)! okay?
print("segue identifier", segue.identifier)
does not give me any response on the console.