简体   繁体   English

CGFloat作为财产

[英]CGFloat as Property

How can I store/retrieve a CGFloat as a property of a subclassed UIImageView ? 如何将CGFloat作为子类UIImageView的属性存储/检索? I can set it's value but when I retrieve it, it is corrupt. 我可以设置它的值,但是当我检索它时,它已损坏。 NSString properties work fine. NSString属性工作正常。

.h 。H

@property CGFloat myFloat;

.m .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. 发送者是一个UIPanGestureRecognizer,它附加到我遇到问题的UIImageview上。 The UIPanGestureRecognizer calls the selector move:(id)sender and this is where my problem is. UIPanGestureRecognizer调用选择器move:(id)sender,这就是我的问题所在。

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. 但是,如果不包括(id)self,我将无法设置或检索任何float属性。 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. 我有几个NSString属性,它们返回的很好,但CGFloats不会。 Could someone explain to me why somehow I achieve success with strings but the float gets lost here? 有人可以向我解释为什么我以某种方式获得成功,但是浮子在这里迷路了吗?

ANSWER #2: NSLog syntax? 答案2:NSLog语法?

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 (eg NSLog(@"new float value = %f", myFloat) . 如果您使用的是NSLog,请确保您具有正确的浮点格式代码(例如NSLog(@"new float value = %f", myFloat)

ORIGINAL ANSWER: wrong setter (was typo in question) 原始答案:错误的二传手(是打字错误)

You are calling the wrong setter method. 您正在调用错误的setter方法。 The correct setter is: 正确的设置器是:

`[sender setMyFloat:someFloat]`

EDIT: OP has updated his question to remove the typo 编辑:OP更新了他的问题,以消除错字

Try this: 尝试这个:

.h:

@interface SomeClass : UIImageView {
    CGFloat _someFloat;
}

@property (nonatomic) CGFloat someFloat;

@end

.m:

@implementation SomeClass
@synthesize someFloat = _someFloat;
...
...
...
@end

您应该在.m中编写@synthesize myFloat ,以为ivar创建getter / setter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM