How can I store/retrieve a CGFloat
as a property of a subclassed UIImageView
? I can set it's value but when I retrieve it, it is corrupt. NSString
properties work fine.
.h
@property CGFloat myFloat;
.m
[sender setMyFloat:someFloat];
[sender myFloat]; //<- corrupt?
********* UPDATE ***********
I wanted to thank you for all of your suggestions, they were all correct. My problem was more complex than I thought. The sender is a UIPanGestureRecognizer attached to the UIImageview I was having problems with. The UIPanGestureRecognizer calls the selector move:(id)sender and this is where my problem is.
Within move: I could successfully perform:
[[sender view] setMyString:@"test"];
and retrieve it back with
[[sender view] myString];
I could not however set or retrieve any float property at all without including (id)self. For example:
[[sender view] setMyString:@"test"]; //->works
[[sender view] setMyFloat:1.0f]; //->does not work
But this works:
[[[sender view]self] setMyFloat:1.0f]; //->works
This is the strange part which led me to believe I was having a casting problem with my float! I have a several NSString properties and they return just fine but the CGFloats would not. Could someone explain to me why somehow I achieve success with strings but the float gets lost here?
ANSWER #2: NSLog syntax?
How are you validating the value after it is set? If you are using NSLog make sure you have the right format code for a float (e.g. NSLog(@"new float value = %f", myFloat)
.
ORIGINAL ANSWER: wrong setter (was typo in question)
You are calling the wrong setter method. The correct setter is:
`[sender setMyFloat:someFloat]`
EDIT: OP has updated his question to remove the typo
Try this:
.h:
@interface SomeClass : UIImageView {
CGFloat _someFloat;
}
@property (nonatomic) CGFloat someFloat;
@end
.m:
@implementation SomeClass
@synthesize someFloat = _someFloat;
...
...
...
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With