Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGFloat as Property

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?

like image 874
user973984 Avatar asked Nov 17 '11 17:11

user973984


2 Answers

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

like image 178
XJones Avatar answered Sep 27 '22 15:09

XJones


Try this:

.h:

@interface SomeClass : UIImageView {
    CGFloat _someFloat;
}

@property (nonatomic) CGFloat someFloat;

@end

.m:

@implementation SomeClass
@synthesize someFloat = _someFloat;
...
...
...
@end
like image 21
DanZimm Avatar answered Sep 27 '22 17:09

DanZimm