func read()
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName: "Coupons")
var error: NSError?
let sortDescriptor = NSSortDescriptor(key: "coupon_id", ascending: true)
// Set the list of sort descriptors in the fetch request,
// so it includes the sort descriptor
fetchRequest.sortDescriptors = [sortDescriptor]
for var i = 0; i < fetch_coupon_category.count ; i++ {
//condition like here.
let predicate1 = NSPredicate(format: "coupon_categories == %@", fetch_coupon_category[i])
// Set the predicate on the fetch request
// Combine the two predicates above in to one compound predicate
//let the predicate on the fetch request
fetchRequest.predicate = predicate1
我正在尝试将此代码转换为swift2,尝试捕获功能
do{
let fetchedResults = try managedObjectContext.executeFetchRequest(fetchRequest)
as? [NSManagedObject]?
if let results = fetchedResults {
for (var i=0; i < results.count; i++) {
let single_result = results[i]
let out = single_result.valueForKey("coupon_name") as! String
tickets.append(single_result)
}
}
else
{
print("cannot read")
}
}catch let fetchError as NSError{
print("fetching error: \(fetchError.localizedDescription)")
// return nil
}
}
}
我在管理中遇到错误
'try managedObjectContext'使用未解析的标识符
如何将我的代码转换为swift 2.0,从而在转换代码时出现更多错误-.-
答案 0 :(得分:0)
使用try managedContext
代替try managedObjectContext
,因为实际上您已将managedObjectContext
的值分配到当前viewcontroller中的managedContext