开发者

Type casting for Core Data attribute setting

开发者 https://www.devze.com 2022-12-20 00:44 出处:网络
I am trying to set a Core Data attribute but am getting incompatible type errors. I have a float attribute i开发者_JAVA技巧n a Core Data entity on iPhone 3.0. Core Data auto-generates an interface for

I am trying to set a Core Data attribute but am getting incompatible type errors. I have a float attribute i开发者_JAVA技巧n a Core Data entity on iPhone 3.0. Core Data auto-generates an interface for a managed data object that provides property access to it:

@property (nonatomic, retain) NSNumber * volume;

and an implementation for it:

@dynamic volume;

I create an instance of the managed data object, which I call attrVolume, and use that to access that Core data entity attribute through a Core Data managed object context:

[attrVolume setVolume:[txtVolume.text floatValue]];

The compilation error is:

incompatible type for argument 1 of 'setVolume:'

Any ideas how to cast that value and not get that compilation error? Is there a way to cast to NSNUmber?

Any help appreciated // :)


-floatValue returns a value of type float. You are then trying to set the value of volume, which takes an NSNumber to this float value, which fails.

You need to create an NSNumber from the float value of your string and assign that to volume:

NSNumber* volNum = [NSNumber numberWithFloat:[textVolume.text floatValue]];
[attrVolume setVolume:volNum];
0

精彩评论

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

关注公众号