开发者

Issue with transferring and editing integer info stored in coredata

I'm looking for some help. I've tried searching this site and have tried amending my code but I'm stuck. My issue is with displaying and editing an integer attribute stored in coredata. I have a detailvie开发者_运维百科w which uses a UITableView. It seems to display the integer correctly in the cell, using the following code:

cell.textLabel.text = @"Set target";
cell.detailTextLabel.text = [match.set_target stringValue];

but, when I try and edit the value by passing it to a UITextField on an editing view, it displays the integer incorrectly (for example 3 is displayed as 53916). I'm passing the value to the UITextField with this code: (note, editedObject is NSManagedObject, numField is UITextField, editedFieldKey is NSString).

[numField setText:[NSString stringWithFormat:@"%d", editedFieldKey]];

The values are passed to the editing view from the detail view using this code:

controller.editedFieldKey = @"set_target";
controller.editedFieldName = NSLocalizedString(@"Number of sets to win", @"set_legs");

I can display, edit and save strings and dates but I can't figure out Integers. Any help would be appreciated.

EDIT 1

I have save and cancel buttons on my edit view. The save button invokes:

- (IBAction)save {

    // Set the action name for the undo operation.
    NSUndoManager * undoManager = [[editedObject managedObjectContext] undoManager];
    [undoManager setActionName:[NSString stringWithFormat:@"%@", editedFieldName]];
    if (editingDate) {
        [editedObject setValue:dateField.date forKey:editedFieldKey];
    }
    else if (editingNum) {
        [editedObject setValue: [NSNumber numberWithInteger: [numField.text integerValue]] forKey: editedFieldKey];
    }
    else {
        [editedObject setValue: textField.text forKey:editedFieldKey];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

With the code for displaying the integer set as:

[numField setText:[NSString stringWithFormat:"%d", [editedObject valueForKey:editedFieldKey]]];

I have a warning about the line: Passing argument 1 of 'StringwithFormat' from incompatible pointer type. On running it crashes with: +[NSString WithFormat:]: unrecognized selector sent to class 0x211d60'


You are passing a string to numberWithInteger, when instead you want the integer value from the string. Try changing it to this:

[editedObject setValue: [NSNumber numberWithInteger: [numField.text integerValue]] forKey: editedFieldKey];  .


"editedFieldKey is NSString"

[numField setText:[NSString stringWithFormat:@"%d", editedFieldKey]];

replace the formatter with %@

[numField setText:[NSString stringWithFormat:@"%@", editedFieldKey]];

EDIT:

Since you are not adding anything to the string, the stringWithFormat: message is unnecessary. The following will product the same result.

[numField setText:editedFieldKey];

And for those who prefer '.' syntax for property accessors

numField.text = editedFieldKey;

EDIT 2

I will assume that you are passing in your managed object and context to the editing controller...

To display the value of the "set_target" attribute of your managed object.

[numField setText:[[editedObject valueForKey:editedFieldKey] stringValue]]; // set_target is returned as an NSNumber
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜