xcode CGFloat 作为属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8171962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CGFloat as Property
提问by user973984
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.
如何将 aCGFloat
作为子类的属性存储/检索UIImageView
?我可以设置它的值,但是当我检索它时,它已损坏。 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. The UIPanGestureRecognizer calls the selector move:(id)sender and this is where my problem is.
我想感谢你的所有建议,他们都是正确的。我的问题比我想象的要复杂。发件人是一个 UIPanGestureRecognizer,附加到我遇到问题的 UIImageview 上。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. For example:
但是,如果不包括 (id)self,我根本无法设置或检索任何浮动属性。例如:
[[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?
这是奇怪的部分,让我相信我的漂浮物有铸造问题!我有几个 NSString 属性,它们返回得很好,但 CGFloats 不会。有人可以向我解释为什么我用字符串取得了成功,但浮点数在这里丢失了吗?
采纳答案by XJones
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 (e.g. 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. The correct setter is:
您正在调用错误的 setter 方法。正确的设置器是:
`[sender setMyFloat:someFloat]`
EDIT: OP has updated his question to remove the typo
编辑:OP 更新了他的问题以删除错字
回答by DanZimm
Try this:
尝试这个:
.h:
@interface SomeClass : UIImageView {
CGFloat _someFloat;
}
@property (nonatomic) CGFloat someFloat;
@end
.m:
@implementation SomeClass
@synthesize someFloat = _someFloat;
...
...
...
@end
回答by d.lebedev
You should write @synthesize myFloat
in your .m to create getter/setter for ivar
你应该@synthesize myFloat
在你的 .m 中写入为 ivar 创建 getter/setter