Post

Replies

Boosts

Views

Activity

Delay Button To Site
I have an alert that I want to go to a specific site but after the user presses okay and for it to show only once. I can't figure out the part where the site is delayed because the user did not press okay. var justOnce: Bool = true    @IBAction func playStats(_ sender: Any) {     //show alert only once     if justOnce {     let alert = UIAlertController(title: "Stats Important", message: "You can copy and paste the name here when you see the searchbar", preferredStyle: .alert)     alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))     self.present(alert, animated: true, completion: nil)     justOnce = false gotoSite()     }   }
1
0
599
Jul ’21
Getting The Right Data Back
So I have an app and it relies on Core Data to save and load favourite players. The issue I have is the data being loaded in my viewwillappear method is the max number of all the players regardless whether or not I save the data. When I access my favouritesVC I can't go back and add more players because they take up the max amount of storage. I need my app to retrieve the favourites I saved not all the players. class FavouritesVC {    //this is a button I save my favourite players   //the print statements are accurate   @IBAction func save(_ sender: UIBarButtonItem) {     let entity = NSEntityDescription.entity(forEntityName: "CurrentPlayers", in: context)!     let saveFav = CurrentPlayers(entity: entity, insertInto: context)     for o in prefArr {       saveFav.yahooName = o.yahooName       saveFav.team = o.team       saveFav.position = o.position       saveFav.photoUrl = o.photoUrl     do {       try context.save()       print("These are my saved objects: \(saveFav)")       print("how many saved objects: \(prefArr.count)")     } catch {       print("error is: \(error)")     }     }   } } //what I use to load the data    override func viewWillAppear(_ animated: Bool) {     super.viewWillAppear(animated)     let fetchRequest = NSFetchRequest<CurrentPlayers>(entityName: "CurrentPlayers")     do {       prefArr = try context.fetch(fetchRequest)       for p in prefArr {         if p.yahooName == "Jordan Gross" {           print("Jordan added")         }       }       print("There are this many saved favourites \(prefArr.count)")     } catch let error {       print("Could not fetch. \(error)")     }   } } //core data class file import Foundation import CoreData enum DecoderConfigurationError: Error {   case missingManagedObjectContext } extension CodingUserInfoKey {   static let managedObjectContext = CodingUserInfoKey(rawValue: "managedObjectContext")! } @objc(CurrentPlayers) public class CurrentPlayers: NSManagedObject, Decodable {       enum CodingKeys: String, CodingKey {       case photoUrl = "PhotoUrl"       case firstName = "FirstName"       case lastName = "LastName"       case position = "Position"       case team = "Team"       case yahooName = "YahooName"       case status = "Status"       case jerseyNumber = "Jersey"     }          public static var managedObjectContext: NSManagedObjectContext?          required public convenience init(from decoder: Decoder) throws {       guard let context = decoder.userInfo[.managedObjectContext] as? NSManagedObjectContext else {         throw DecoderConfigurationError.missingManagedObjectContext       }             self.init(context: context)       //...      let values = try decoder.container(keyedBy: CodingKeys.self)      photoUrl = try values.decode(String.self, forKey: CodingKeys.photoUrl)      firstName = try values.decode(String.self, forKey: CodingKeys.firstName)      lastName = try values.decode(String.self, forKey: CodingKeys.lastName)      position = try values.decode(String.self, forKey: CodingKeys.position)      team = try values.decode(String.self, forKey: CodingKeys.team)      yahooName = try values.decodeIfPresent(String.self, forKey: CodingKeys.yahooName)      status = try values.decode(String.self, forKey: CodingKeys.status)      jerseyNumber = try values.decodeIfPresent(Int64.self, forKey: CodingKeys.jerseyNumber) ?? 0     } }
1
0
418
Sep ’21