0

When I try to get a string value from an NSManagedObject, I get this

<Entity: 0x1e043140> (entity: Entity; id: 0x1e041c30 <x-coredata://8F48C331-B879-47B4-B257-4802A13ED12C/Entity/p4> ; data: {
number = "<UITextField: 0x1d8b7cc0; frame = (159 183; 161 30); text = 'test'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x1d8b3c10>; layer = <CALayer: 0x1d892a30>> : ";
})

how do I get the string from this (it's text = 'test';)

i get the object using this

NSString *rowValue = [self.fetchedResultsController objectAtIndexPath:indexPath];

ok, well the nsmanaged object is this and sets a uitableview cell to its string

NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];

cell.textLabel.text = [[object valueForKey:@"number"] description];

The reason it's showing what it does is because, as you can see, I am getting it's description. I can't find a property that would return the text value, so does anyone know how? Thanks.

4

3 回答 3

1

我假设当您在托管对象中存储值时已经发生错误。也许你会做类似的事情

[myObject setValue:[aTextField description] forKey:@"number"]

而不是存储文本字段的文本内容:

[myObject setValue:[aTextField text] forKey:@"number"]

更新:正如您在评论中所写,托管对象值存储为

NSString *string = [NSString stringWithFormat:@"%@ : %@", self.numtext, self.comtext];
[newManagedObject setValue:string forKey:@"number"];

但是a本身%@的格式被扩展为文本字段描述,而不是文本内容。因此已经看起来像UITextField string

"<UITextField: 0x1d8b7cc0; frame = (159 183; 161 30); text = 'test'; .... "

并且此字符串作为“数字”属性存储在托管对象中。您应该改用以下代码:

NSString *string = [NSString stringWithFormat:@"%@ : %@", self.numtext.text, self.comtext.text];
[newManagedObject setValue:string forKey:@"number"];

请注意,您必须在测试时删除旧数据库以消除已经存在的错误条目。

于 2013-02-09T22:48:56.907 回答
0
NSString *str = managedObject.objectID.URIRepresentation.absoluteString
于 2014-06-18T08:50:04.493 回答
-1

你应该写:

Entity *entity = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *rowValue = entity.text;

编辑:

好的,那试试object.number.text;

于 2013-02-09T22:39:31.143 回答