我试图使用func我扩展了UIViewController。我将自我添加到不同的func的论点中,但是func没有给我"隐含地使用' self'关闭;使用' self。'使捕获语义明确"错误。甚至尝试应用弱自引用。 func似乎永远不会运行
宣布数组
var managedComicsArray = [NSManagedObject]()
在viewDidLoad中:
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
managedContext = appDelegate.persistentContainer.viewContext
按钮
@IBAction func addComicButtonTapped(_ sender: UIBarButtonItem) {
print("add button tapped")
weak var weakSelf = self // ADD THIS LINE AS WELL
let alert = UIAlertController(title: "Some Title", message: "Enter a text", preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = "Some default text"
textField.keyboardType = .numberPad
}
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
// let textField = alert.textFields![0] // Force unwrapping because we know it exists.
let textField = alert!.textFields![0]
print("Text from textField: \(textField.text)")
guard let grabbedNumber = Int16(textField.description) else {return}
//the func below gives the error
weakSelf?.addRecordIfnotPresent(newIssueNumber: grabbedNumber, theManagedContext: self.managedContext, theEntityName: self.kComicEntityName, managedArray: &self.managedComicsArray)
self.myTableView.reloadData()
}))
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
// 4. Present the alert.
self.present(alert, animated: true, completion: nil)
//end of button
}
扩展方法:
func addRecordIfnotPresent(newIssueNumber: Int16, theManagedContext: NSManagedObjectContext, theEntityName: String, managedArray: inout [NSManagedObject]) {
print("adding func started")
let comicFetch: NSFetchRequest<Comic> = Comic.fetchRequest()
var isPresent = true
do {
managedArray = try theManagedContext.fetch(comicFetch)
if !managedArray.isEmpty {
//*****************************************************************
for comic in managedArray {
var comicToCheck = Comic()
comicToCheck = comic as! Comic
if comicToCheck.wholeSeriesNumber == newIssueNumber {
print("number \(newIssueNumber) is alredy present")
return
} else {
isPresent = false
print("number not present, keep on!")
}
}
guard isPresent == false else {return}
saveSingleComicWithNumber(withIssueNumber: Int(newIssueNumber), theEntityName: theEntityName, theManagedContext: theManagedContext)
print("created Comic!")
try theManagedContext.save()
print("saved context!")
//*****************************************************************
} else {
saveSingleComicWithNumber(withIssueNumber: Int(newIssueNumber), theEntityName: theEntityName, theManagedContext: theManagedContext)
}
} catch let error as NSError {
print("Fetch error: \(error) description: \(error.userInfo)")
}
}
func saveSingleComicWithNumber(withIssueNumber: Int, theEntityName: String, theManagedContext: NSManagedObjectContext) {
let entity = NSEntityDescription.entity(forEntityName: theEntityName, in: theManagedContext)!
let comicToAdd = Comic(entity: entity, insertInto: theManagedContext)
comicToAdd.wholeSeriesNumber = Int16(withIssueNumber)
comicToAdd.issueInCollection = false
comicToAdd.titleOfItalianSerie = "title"
if comicToAdd.wholeSeriesNumber == 2 {
comicToAdd.issueInCollection = false
}
do {
try theManagedContext.save()
} catch let error as NSError {
print("could not save. \(error), \(error.userInfo)")
}
}