转换为 swift 2.0 在 managedObjectContext 中出错
converting into swift 2.0 got error in managedObjectContext
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 以实现一些 try catch 功能
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' use unresolved identifier
如何将我的代码转换为 swift 2.0,在转换我的代码时出现更多错误 -.-
使用 try managedContext
而不是 try managedObjectContext
因为实际上你在当前 viewcontroller
中将 managedObjectContext
的值赋给了 managedContext
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 以实现一些 try catch 功能
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' use unresolved identifier
如何将我的代码转换为 swift 2.0,在转换我的代码时出现更多错误 -.-
使用 try managedContext
而不是 try managedObjectContext
因为实际上你在当前 viewcontroller
managedObjectContext
的值赋给了 managedContext