首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >coreData in Swift2 -更新条目

coreData in Swift2 -更新条目
EN

Stack Overflow用户
提问于 2015-08-26 14:12:47
回答 1查看 89关注 0票数 0

在objC中,我经常这样做:

.h文件

代码语言:javascript
复制
@class Foo

@interface ViewController {
@private
Foo *foo;

  ...

}

@property (nonatomic, strong) Foo * foo;

.m文件

代码语言:javascript
复制
#import "Foo.h" 

@implementation ViewController
@synthesize foo;

然后我就做到了:

代码语言:javascript
复制
- (void)buttonTapped:(id)sender{

    NSString *value = @"ON";
    NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
    UIImage *unselectedImage = [UIImage imageNamed: @"foo.png"];
    UIImage *selectedImage = [UIImage imageNamed:@"doo.png"];

    if ([sender isSelected]) {
        [sender setImage:unselectedImage forState:UIControlStateNormal];
        [sender setSelected:NO];
        value = @"OFF";
        [userPreferences setObject:value forKey:@"stateOfButton"];

        foo.attribute = nil;


    }else {
        [sender setImage:selectedImage forState:UIControlStateSelected];
        [sender setSelected:YES];
        value = @"ON";
        [userPreferences setObject:value forKey:@"stateOfButton"];



         foo.attribute = @"someString";

    }

    NSManagedObjectContext *context = xray.managedObjectContext;
    NSError *error = nil;
    if (![context save:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    [userPreferences synchronize];

}

怎么会有人对swift2做同样的事情呢?从昨天起就一直困扰着我。这真的不应该那么难。应该这样吗?

编辑:

许多失败的尝试之一:

代码语言:javascript
复制
var foo : Foo = Foo()

  @IBAction func buttonTapped(sender: UIButton) {


      let foo = NSEntityDescription.insertNewObjectForEntityForName("EntityName", inManagedObjectContext: self.managedObjectContext!) as! Foo 
// if you comment this you get this error: fatal error: unexpectedly found nil while unwrapping an Optional value

        var value: NSString = "ON"
        let userPrefs: NSUserDefaults = NSUserDefaults()
        let unselectedImage = UIImage(named:"foo.png")
        let selectedImage = UIImage(named: "doo.png")



        if sender.selected {
            sender .setImage(unselectedImage, forState: .Normal)
            sender.selected = false
            value = "OFF"
            userPrefs.setObject(value, forKey: "stateOfButton")



            foo.attribute = nil
        }

        else {

            sender .setImage(selectedImage, forState: .Normal)
            sender.selected = true
            value = "ON"
            userPrefs.setObject(value, forKey: "stateOfButton")



           foo.attribute = "someString"
        }

         let context = foo.managedObjectContext

    do {
            try context!.save()
            } catch {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            //print("Unresolved error \(error), \(error.userInfo)")
            abort()
            }

        userPrefs.synchronize()

    }

这给了我一个错误:

代码语言:javascript
复制
An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-26 19:33:11

这似乎是其中的诀窍:

代码语言:javascript
复制
 self.detailItem?.attribute = "someString"

其中detailedItem

代码语言:javascript
复制
  var detailItem: Foo? 

var foo: Foo = Foo()

准备做好准备:

代码语言:javascript
复制
 var foo: Foo!
             foo = filteredObjects![indexPath.row] as Foo
                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = foo
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32229064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档