Hi加下面的功能在您的NSManagedObject擴展
1)使用ID取從coredata數據預測
class func Search(PredictName:String, Uid:String, entityDescription: String,managedObjectContext: NSManagedObjectContext = appDelegate.managedObjectContext) -> [AnyObject]?
{
var ar:[AnyObject] = []
do
{
let predicate = NSPredicate(format: "\(PredictName) == %@", Uid)
let request = NSFetchRequest(entityName: entityDescription)
request.predicate = predicate
let result = try managedObjectContext.executeFetchRequest(request)
ar = result
}
catch(_)
{
ar = []
}
return ar as [AnyObject]
}
Function Call :
var lists:[ModelClassName]? = AdvertisementDataList.findByTypeInContext("user_id", url: self.Uid, entityDescription: "CoredataEntityName") as? [AdvertisementDataList]
它會回報你的行ID預測列名數據
2)從coredata獲取所有數據
class func Search(PredictName:String, entityDescription: String,managedObjectContext: NSManagedObjectContext = appDelegate.managedObjectContext) -> [AnyObject]?
{
var ar:[AnyObject] = []
do
{
let predicate = NSPredicate(format: "\(PredictName)")
let request = NSFetchRequest(entityName: entityDescription)
request.predicate = predicate
let result = try managedObjectContext.executeFetchRequest(request)
ar = result
}
catch(_)
{
ar = []
}
return ar as [AnyObject]
}
函數調用:
var lists:[ModelClassName]? = AdvertisementDataList.findByTypeInContext("user_id", entityDescription: "CoredataEntityName") as? [AdvertisementDataList]
它將通過預測名稱返回所有數據。