swift 4扩展无法将不可变值作为inout参数传递:'self'是不可变的“错误消息

时间:2018-04-25 12:35:53

标签: swift core-data extension-methods

我创建了UIViewController的简单扩展,但是当我在内部调用该方法时,即使应用inout关键字,我也得到“无法将不可变值作为inout参数传递:'self'是不可变的”错误消息。怎么解决呢?

在我的VC中我有这个var

var asyncComicEntityArray : [NSManagedObject]! = []

在我的按钮中我以这种方式实现了扩展

searchIfOwnedInExtension(asynchArrayToUse: &asyncComicEntityArray, showWhereOwnedIs: true)

以下是单独的swift文件中的扩展名

EXTENSION 
import Foundation
import UIKit
import CoreData




extension UIViewController {



    //needs a     var asyncComicEntityArray : [NSManagedObject]! = []

    func searchIfOwnedInExtension(asynchArrayToUse: inout [NSManagedObject], showWhereOwnedIs: Bool) {

        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
        let managedContext = appDelegate.persistentContainer.viewContext



        let comicFetch : NSFetchRequest<Comic> = Comic.fetchRequest()
        let predicateForRequest = NSPredicate(format: "inCollection == %@", NSNumber(booleanLiteral: showWhereOwnedIs))
        comicFetch.predicate = predicateForRequest


        do {
            asynchArrayToUse = try managedContext.fetch(comicFetch)
        } catch let error as NSError {
            print("Fetch error: \(error) description: \(error.userInfo)")
        }

        guard asynchArrayToUse != nil else {return}
        for comic in asynchArrayToUse {
            var comicTouse = Comic()
            comicTouse = comic as! Comic
            print("comic title: \(comicTouse.comicTitle!), is it in collection? : \(comicTouse.inCollection)")
        }



    }





}

0 个答案:

没有答案