第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何更新核心數(shù)據(jù)中的現(xiàn)有對(duì)象?

如何更新核心數(shù)據(jù)中的現(xiàn)有對(duì)象?

德瑪西亞99 2019-10-25 15:04:17
當(dāng)我插入新對(duì)象時(shí),我將使用以下代碼:NSManagedObjectContext *context = [appDelegate managedObjectContext];Favorits *favorits = [NSEntityDescription insertNewObjectForEntityForName:@"Favorits" inManagedObjectContext:context];favorits.title = @"Some title";NSError *error;                    if (![context save:&error]) {    NSLog(@"Whoops");}如何更新核心數(shù)據(jù)中的現(xiàn)有對(duì)象?
查看完整描述

3 回答

?
慕桂英4014372

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ì)被更新。


查看完整回答
反對(duì) 回復(fù) 2019-10-25
?
眼眸繁星

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");

    }




}


查看完整回答
反對(duì) 回復(fù) 2019-10-25
?
慕田峪4524236

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)

    }

}


查看完整回答
反對(duì) 回復(fù) 2019-10-25
  • 3 回答
  • 0 關(guān)注
  • 593 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)