3 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊
更新就像創(chuàng)建一個(gè)新的一樣簡(jiǎn)單。
要更新特定的對(duì)象,您需要設(shè)置一個(gè)NSFetchRequest。此類等效于SQL語(yǔ)言中的SELECT聲明。
這里有個(gè)簡(jiǎn)單的例子:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Favorits" inManagedObjectContext:moc]];
NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
// error handling code
該數(shù)組results包含sqlite文件中包含的所有托管對(duì)象。如果要獲取特定對(duì)象(或多個(gè)對(duì)象),則需要對(duì)該謂詞使用謂詞。例如:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title == %@", @"Some Title"];
[request setPredicate:predicate];
在這種情況下,results包含標(biāo)題等于的對(duì)象Some Title。設(shè)置謂詞等于將WHERE子句放在SQL語(yǔ)句中。
有關(guān)更多信息,建議您閱讀Core Data編程指南和NSFecthRequest類參考。
核心數(shù)據(jù)編程指南
NSFecthRequest類參考
希望能幫助到你。
編輯(可用于更新的代碼段)
// maybe some check before, to be sure results is not empty
Favorits* favoritsGrabbed = [results objectAtIndex:0];
favoritsGrabbed.title = @"My Title";
// save here the context
或者如果您不使用NSManagedObject子類。
// maybe some check before, to be sure results is not empty
NSManagedObject* favoritsGrabbed = [results objectAtIndex:0];
[favoritsGrabbed setValue:@"My title" forKey:@"title"];
// save here the context
在這兩種情況下,如果您save根據(jù)上下文進(jìn)行操作,數(shù)據(jù)都會(huì)被更新。

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
希望對(duì)您有幫助。因?yàn)樗鼘?duì)我有用。
NSMutableArray *results = [[NSMutableArray alloc]init];
int flag=0;
NSPredicate *pred;
if (self.txtCourseNo.text.length > 0) {
pred = [NSPredicate predicateWithFormat:@"courseno CONTAINS[cd] %@", self.txtCourseNo.text];
flag=1;
} else {
flag=0;
NSLog(@"Enter Corect Course number");
}
if (flag == 1) {
NSLog(@"predicate: %@",pred);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Course"];
[fetchRequest setPredicate:pred];
results = [[self.context executeFetchRequest:fetchRequest error:nil] mutableCopy];
if (results.count > 0) {
NSManagedObject* favoritsGrabbed = [results objectAtIndex:0];
[favoritsGrabbed setValue:self.txtCourseName.text forKey:@"coursename"];
[self.context save:nil];
[self showData];
} else {
NSLog(@"Enter Corect Course number");
}
}

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
如果您是一個(gè)敏捷的程序員,這可以為您提供幫助:
如果要?jiǎng)h除NSManagedObject
就我而言,ID是實(shí)體STUDENT的唯一屬性
/** for deleting items */
func Delete(identifier: String) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "STUDENT")
let predicate = NSPredicate(format: "ID = '\(identifier)'")
fetchRequest.predicate = predicate
do
{
let object = try context.fetch(fetchRequest)
if object.count == 1
{
let objectDelete = object.first as! NSManagedObject
context.delete(objectDelete)
}
}
catch
{
print(error)
}
}
如果要更新NSManagedObject:
/** for updating items */
func Update(identifier: String,name:String) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "STUDENT")
let predicate = NSPredicate(format: "ID = '\(identifier)'")
fetchRequest.predicate = predicate
do
{
let object = try context.fetch(fetchRequest)
if object.count == 1
{
let objectUpdate = object.first as! NSManagedObject
objectUpdate.setValue(name, forKey: "name")
do{
try context.save()
}
catch
{
print(error)
}
}
}
catch
{
print(error)
}
}
- 3 回答
- 0 關(guān)注
- 593 瀏覽
添加回答
舉報(bào)