0

I have a core data model set up and I am using a text entry box to record data. My code looks like this:

NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
NSNumber *timetickNumber = [NSNumber numberWithInt:timeTick];
[newDevice setValue:timetickNumber forKey:@"name"];
[newDevice setValue:self.versionTextField.text forKey:@"version"];
[newDevice setValue:self.companyTextField.text forKey:@"company"];

For the second entry title 'version' I am trying to add text in front of what ever is typed. For example, it would say 'This is' and then show the text that was typed next to it. Any thoughts? Thanks in advance!

4

2 回答 2

0

Sounds to me like you are trying to concatenate a string. Maybe something like this:

NSString *versionString  = [NSString stringWithFormat:@"This is: %@", self.companyTextField.text]

The value of versionString would be "This is: [insert text from text field here]".

Then to save to core data you would just do:

[newDevice setValue:versionString forKey:@"company"];
于 2013-11-01T04:13:22.933 回答
0

混合格式和数据不是一个好习惯。(还记得 Model-View-Controller 模式吗?)如果存储“This is”,它将很快变得多余,并且无论如何都无法以不同的语言正确显示。从许多角度来看,这是一个相当有问题的设计。

相反,您应该像在代码中一样将值存储在 Core Data 中。如果您需要显示这些数据,例如在 a 中,UILabel您可以添加您需要的任何文本(您可以稍后轻松更改它)。

label.text = [NSString stringWithFormat:@"This is %@.", device.company];

甚至更好

label.text = [NSString stringWithFormat:@"%@%@", 
    NSLocalisedString(@"This is ", "string to present the company"), 
    device.company];
于 2013-11-01T16:01:41.153 回答