我创建了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)")
}
}
}