Xcode 14.2 应用程序委托问题 - 无法识别的选择器发送到实例 0x7f80740e4800

问题描述 投票:0回答:1

我有一个我无法解决的问题。 我声明以前它没有问题。 用 Xcode 14.2 打开它做一个小更新,现在当我尝试应用程序时,我总是在 AppDelegate 中遇到同样的问题:Xcode 14.2 应用程序委托问题 - 无法识别的选择器发送到实例 0x7f80740e4800。 我不知道我必须纠正什么,在“主要”中没有任何改变,插座连接等已被删除。 我认为这是新版本的问题。 任何人都知道我该如何解决它? 谢谢。

代码:

    @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        
        if !OneSignalAppId.isEmpty {
            let _ = OneSignal(launchOptions: launchOptions, appId: OneSignalAppId, handleNotification: nil)
            
            OneSignal.defaultClient().enable(inAppAlertNotification: true)
        }
        
        return true
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        // Saves changes in the application's managed object context before the application terminates.
        self.saveContext()
    }

    // MARK: - Core Data stack

    lazy var applicationDocumentsDirectory: URL = {
        // The directory the application uses to store the Core Data store file. This code uses a directory named "com.codemotionapps.Recipe" in the application's documents Application Support directory.
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return urls[urls.count-1] 
    }()

    lazy var managedObjectModel: NSManagedObjectModel = {
        // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
        let modelURL = Bundle.main.url(forResource: "Recipe", withExtension: "momd")!
        return NSManagedObjectModel(contentsOf: modelURL)!
    }()

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
        // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
        // Create the coordinator and store
        var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.appendingPathComponent("Recipe.sqlite")
        var error: NSError? = nil
        var failureReason = "There was an error creating or loading the application's saved data."
        do {
            try coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
        } catch var error1 as NSError {
            error = error1
            coordinator = nil
//            // Report any error we got.
//            let dict = NSMutableDictionary()
//            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
//            dict[NSLocalizedFailureReasonErrorKey] = failureReason
//            dict[NSUnderlyingErrorKey] = error
//            error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict as [NSObject : AnyObject])
//            // Replace this with code to handle the error appropriately.
//            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//            NSLog("Unresolved error \(error), \(error!.userInfo)")
            abort()
        } catch {
            fatalError()
        }
        
        return coordinator
    }()

    lazy var managedObjectContext: NSManagedObjectContext? = {
        // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
        let coordinator = self.persistentStoreCoordinator
        if coordinator == nil {
            return nil
        }
        var managedObjectContext = NSManagedObjectContext()
        managedObjectContext.persistentStoreCoordinator = coordinator
        return managedObjectContext
    }()

    // MARK: - Core Data Saving support

    func saveContext () {
        if let moc = self.managedObjectContext {
            var error: NSError? = nil
            if moc.hasChanges {
                do {
                    try moc.save()
                } catch let error1 as NSError {
                    error = error1
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    NSLog("Unresolved error \(String(describing: error)), \(error!.userInfo)")
                    abort()
                }
            }
        }
    }

}
ios appdelegate
1个回答
-1
投票

@罗布 我也试图理解错误,因为它始终有效并且没有进行任何更改。 自从我升级到 xCode 14.2 进行更新后,我收到了这个错误。 这是 recipeviewcontroller 的内容:

    import UIKit
import GoogleMobileAds

class RecipeViewController: AdvertisedViewController, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UIAlertViewDelegate {

    var recipe = Recipe()

    var imageName = ""
    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var summaryImageView: UIImageView!
    @IBOutlet weak var summaryLabel: UILabel!
    @IBOutlet weak var ingredientsImageView: UIImageView!
    @IBOutlet weak var ingredientsLabel: UILabel!
    @IBOutlet weak var recipeImageView: UIImageView!
    @IBOutlet weak var recipeLabel: UILabel!
    @IBOutlet weak var collectionViewBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var recipeBtn: UIButton!
    @IBOutlet weak var summaryButton: UIButton!
    
    var favorited = false;

    var selectedTab = 0;

    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = nil
        let favorites = UserDefaults.standard.array(forKey: "favorites") as! Array<Int>?
        let recipeID = recipe.recipeId

        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "nav_add_shopping_list"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(RecipeViewController.addToShoppingList(_:)))
        self.view.backgroundColor = .black // Control background color of viewcontroller container view, please use the same color for ad banner background
        self.tableView.estimatedRowHeight = 44.0
        
        imageName = "fav"
        if let f = favorites {
            if f.index(of: recipeID) != nil {
                imageName = "fav_added"
            }
        }
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        if self.selectedTab == 2 {
            recipeBtn.sendActions(for: .touchUpInside)
        } else if self.selectedTab == 0 {
                summaryButton.sendActions(for: .touchUpInside)
        }
        
        
        
    }

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var rows = 1
        if selectedTab == 0 {
            rows += 1
        } else if selectedTab == 1 {
            rows += recipe.ingredients.count
        } else if selectedTab == 2 {
            rows += recipe.steps.count
        }

        return rows
    }

    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell: RecipeMainCell = tableView.dequeueReusableCell(withIdentifier: String(describing: RecipeMainCell.self), for: indexPath) as! RecipeMainCell

//            if recipe.icons.count > 0 {
//                let icon = recipe.icons[0]
//                if RemoteXMLUrl.characters.count > 0 {
//                    cell.recipeImage.hnk_setImageFromURL(NSURL(string: icon)!)
//                } else {
//                    cell.recipeImage.image = UIImage(named: icon)
//                }
//            }

            cell.favoriteButton .setImage(UIImage(named: imageName), for: UIControlState())
            cell.titleLabel.text = recipe.name
            cell.countryLabel.text = recipe.summary.origin
            cell.prepLabel.text = recipe.summary.preparationTime
            cell.cookLabel.text = recipe.summary.cookingTime
            cell.mealLabel.text = recipe.summary.portions
            cell.caloriesLabel.text = recipe.summary.calories

            return cell
        }
        if selectedTab == 0 {
            let cell: RecipeSummaryCell = tableView.dequeueReusableCell(withIdentifier: String(describing: RecipeSummaryCell.self), for: indexPath) as! RecipeSummaryCell

            cell.summaryLabel.text = recipe.summary.desc
            
            return cell

        } else if selectedTab == 1 {
            let cell: RecipeIngredientCell = tableView.dequeueReusableCell(withIdentifier: String(describing: RecipeIngredientCell.self), for: indexPath) as! RecipeIngredientCell

            let ingredient = recipe.ingredients[indexPath.row - 1]
            
            let ingredientsString = ingredient.quantity + "\n" + ingredient.name as NSString
            
            let ingredientsAttributedString = NSMutableAttributedString(string: ingredientsString as String, attributes: [NSAttributedStringKey.font:  UIFont(name: "HelveticaNeue-Light", size: 17.0)!])
            
            ingredientsAttributedString.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "HelveticaNeue-Medium", size: 17.0)!, range: ingredientsString.range(of: ingredient.quantity))
            
            cell.ingredientsLabel.attributedText = ingredientsAttributedString
            
            return cell
            
        } else if selectedTab == 2 {
            let step = recipe.steps[indexPath.row - 1]
            
            if step.stepImage.isEmpty && step.videoURL.isEmpty {
                let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: RecipeInstructionCell.self), for: indexPath) as! RecipeInstructionCell
                
                cell.titleLabel.text = step.name
                cell.bodyLabel.text = step.desc
                
                return cell
            }
            else if step.stepImage.isEmpty && !step.videoURL.isEmpty {
                let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: RecipeInstructionVideoCell.self), for: indexPath) as! RecipeInstructionVideoCell
                
                cell.titleLabel.text = step.name
                cell.bodyLabel.text = step.desc
                cell.videoView.loadVideoURL(URL(string: step.videoURL)!)
                
                return cell
            }
            else if !step.stepImage.isEmpty && step.videoURL.isEmpty {
                let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: RecipeInstructionImageCell.self), for: indexPath) as! RecipeInstructionImageCell
                
                cell.titleLabel.text = step.name
                cell.bodyLabel.text = step.desc
                
                if step.stepImage.hasPrefix("http") {
                    cell.stepImageView.hnk_setImageFromURL(URL(string: step.stepImage)!)
                } else {
                    cell.stepImageView.image = UIImage(named: step.stepImage)
                }
                
                return cell
            }
            else {
                let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: RecipeInstructionImageVideoCell.self), for: indexPath) as! RecipeInstructionImageVideoCell
                
                cell.titleLabel.text = step.name
                cell.bodyLabel.text = step.desc
               
                if step.stepImage.hasPrefix("http") {
                    cell.stepImageView.hnk_setImageFromURL(URL(string: step.stepImage)!)
                } else {
                    cell.stepImageView.image = UIImage(named: step.stepImage)
                }
                
                cell.videoView.loadVideoURL(URL(string: step.videoURL)!)
                
                return cell
            }
        }

        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: String(describing: RecipeSummaryCell.self), for: indexPath)

        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (indexPath.row == 0) {
            return 280
        }

        return UITableViewAutomaticDimension
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
        // Set cell width to 100%
        return collectionView.frame.size
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return recipe.icons.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell : RecipeCell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecipeCell", for: indexPath) as! RecipeCell

        if recipe.icons.count > 0 {
            let icon = recipe.icons[indexPath.row]
            if RemoteXMLUrl.characters.count > 0 {
                cell.imageView.hnk_setImageFromURL(URL(string: icon)!)
            } else {
                cell.imageView.image = UIImage(named: icon)
            }
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }


    @IBAction func summaryButtonTapped(_ sender: AnyObject) {
        summaryImageView.image = UIImage(named: "tab-bar-summary_active");
        ingredientsImageView.image = UIImage(named: "tab-bar-ingredients");
        recipeImageView.image = UIImage(named: "tab-bar-recipe");

        selectedTab = 0

        summaryLabel.textColor = UIColor(red: 86/255, green: 165/255, blue: 92/255, alpha: 1.0)
        ingredientsLabel.textColor = UIColor(red: 146/255, green: 146/255, blue: 146/266, alpha: 1.0)
        recipeLabel.textColor = UIColor(red: 146/255, green: 146/255, blue: 146/266, alpha: 1.0)

        tableView.reloadData()
    }

    @IBAction func ingredientsButtonTapped(_ sender: AnyObject) {
        summaryImageView.image = UIImage(named: "tab-bar-summary");
        ingredientsImageView.image = UIImage(named: "tab-bar-ingredients_active");
        recipeImageView.image = UIImage(named: "tab-bar-recipe");

        selectedTab = 1

        summaryLabel.textColor = UIColor(red: 146/255, green: 146/255, blue: 146/266, alpha: 1.0)
        ingredientsLabel.textColor = UIColor(red: 86/255, green: 165/255, blue: 92/255, alpha: 1.0)
        recipeLabel.textColor = UIColor(red: 146/255, green: 146/255, blue: 146/266, alpha: 1.0)

        tableView.reloadData()
    }

    @IBAction func recipeButtonTapped(_ sender: AnyObject) {
        summaryImageView.image = UIImage(named: "tab-bar-summary");
        ingredientsImageView.image = UIImage(named: "tab-bar-ingredients");
        recipeImageView.image = UIImage(named: "tab-bar-recipe_active");
        selectedTab = 2

        summaryLabel.textColor = UIColor(red: 146/255, green: 146/255, blue: 146/266, alpha: 1.0)
        ingredientsLabel.textColor = UIColor(red: 146/255, green: 146/255, blue: 146/266, alpha: 1.0)
        recipeLabel.textColor = UIColor(red: 86/255, green: 165/255, blue: 92/255, alpha: 1.0)
        
        tableView.reloadData()
    }

    @IBAction func favoriteButtonTapped(_ sender: AnyObject) {
        let recipeID = recipe.recipeId

        var favorites = UserDefaults.standard.array(forKey: "favorites") as! Array<Int>?

        imageName = "fav_added"
        if let f = favorites {
            if let i = f.index(of: recipeID) {
                imageName = "fav"
                favorites?.remove(at: i)
            } else {
                favorites?.append(recipeID)
            }
        } else {
            favorites = [recipeID]
        }

        self.tableView.reloadData()

        UserDefaults.standard.set(favorites, forKey: "favorites")
        UserDefaults.standard.synchronize()
    }

    @IBAction func shareRecipe(_ sender: AnyObject) {
        let activity = UIActivityViewController(activityItems: [recipe.name, AppStoreUrl], applicationActivities: nil)

        present(activity, animated: true, completion: nil)
    }

    override func adViewDidReceiveAd(_ bannerView: GADBannerView!) {
        super.adViewDidReceiveAd(bannerView)
        collectionViewBottomConstraint.constant = bannerView.frame.height
    }
    
    override func adView(_ bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
        super.adView(bannerView, didFailToReceiveAdWithError: error)
        collectionViewBottomConstraint.constant = 0
    }

    @IBAction func addToShoppingList(_ sender: AnyObject) {
        UIAlertView(title: "Do you want to add the the result achieved into the Goal List?", message: "", delegate: self, cancelButtonTitle: "NO", otherButtonTitles: "YES").show()
    }

    func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
        if buttonIndex == 1 {

            if let ingredients = UserDefaults.standard.object(forKey: "shoppingList") as? Array<Data> {

                var ingredientsArray =  (NSKeyedUnarchiver.unarchiveObject(with: ingredients.first!) as? Array<RecipeIngredient>)!

                ingredientsArray = ingredientsArray + recipe.ingredients
                UserDefaults.standard.set([NSKeyedArchiver.archivedData(withRootObject: ingredientsArray)], forKey: "shoppingList")
                UserDefaults.standard.synchronize()
            } else {
                UserDefaults.standard.set([NSKeyedArchiver.archivedData(withRootObject: recipe.ingredients)], forKey: "shoppingList")
                UserDefaults.standard.synchronize()
                print("ingredients")
            }

            print(UserDefaults.standard.object(forKey: "shoppingList")!)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.