xcode 从 CGFloat 数组创建属性 - iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11630755/
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
Creating a property from a CGFloat array - iOS
提问by Mrwolfy
Trying to create a property, so I can set CGfloat array values on an instance of a class. Having a hard time figuring out how to do it. The CGFloat needs to have "size" [2] variable and numeric components like below:
尝试创建一个属性,以便我可以在类的实例上设置 CGfloat 数组值。很难弄清楚如何去做。CGFloat 需要具有“大小”[2] 变量和数字组件,如下所示:
CGFloat locations[2] = {1.0, 0.0};
Here is how it looks in the class itself (this works), before I created the property in an attempt to set these values from an instance (this creates a gradient BTW via drawRect in a UIView sunclass):
这是它在类本身中的外观(这有效),在我创建属性以尝试从实例设置这些值之前(这通过 UIView sunclass 中的 drawRect 创建了渐变 BTW):
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef myColorspace=CGColorSpaceCreateDeviceRGB();
CGFloat locations[2] = {1.0, 0.0};
CGFloat components[8] = { 1.0, 0.0, 0.0, 1.0, 0.5, 0.25, 1.0, 1.0 };
CGGradientRef myGradient = CGGradientCreateWithColorComponents(myColorspace, components,locations, num_locations);
CGContextDrawLinearGradient (context, myGradient, myStartPoint, myEndPoint, 0);
I tried creating a property like below in the .h
我尝试在 .h 中创建如下所示的属性
@property (nonatomic, assign) CGFloat locations;
and in the .m
并在.m
@synthesize locations;
But I cant figure out how to properly set the values for the [size] and the components from the instance. Also I get errors when I set up the property: ERROR: "Passing 'CGFloat' (aka 'float') to parameter of incompatible type 'const CGFloat *' (aka 'const float *'); take the address with &"
但我无法弄清楚如何正确设置 [size] 和实例中的组件的值。我在设置属性时也遇到错误:错误:“将 'CGFloat'(又名 'float')传递给不兼容类型的参数'const CGFloat *'(又名'const float *');使用 & 获取地址”
you can get the project here is you feel like taking a look tinyurl.com/cmgy482Any help much appreciated.
你可以在这里得到这个项目,你想看看tinyurl.com/cmgy482非常感谢任何帮助。
采纳答案by Mrwolfy
From another answer found here: "C arrays are not one of the supported data types for properties".I think that was my issue. The solution I used, is to make the property an NSMutableArray and to convert the values to a CGFloat.
从此处找到的另一个答案:“C 数组不是属性支持的数据类型之一”。我想那是我的问题。我使用的解决方案是将属性设为 NSMutableArray 并将值转换为 CGFloat。
.h
。H
@property (nonatomic, retain) NSMutableArray *locations;
.m
.m
CGFloat locationsFloat[[locations count]];
for (int i = 0; i < [locations count]; i++) {
NSNumber *number = [locations objectAtIndex:i];
locationsFloat[i] = [number floatValue];
}
viewController
视图控制器
- (void)viewDidLoad
{
NSMutableArray *arrayPoints = [[NSMutableArray alloc]init];
[arrayPoints addObject:[NSNumber numberWithFloat:1.0]];
[arrayPoints addObject:[NSNumber numberWithFloat:0.0]];
[myInstance setLocations:arrayPoints];
...
回答by rob mayoff
Ideally you would be able to just declare your property like this:
理想情况下,您可以像这样声明您的财产:
@property (nonatomic) CGFloat locations[2];
Alas, clang does not allow you to create a property with an array type.
唉,clang 不允许您创建具有数组类型的属性。
One alternative is to use a pointer type instead. You probably want to write a comment to explain how many elements it points to:
一种替代方法是改用指针类型。您可能想写一条注释来解释它指向多少个元素:
#define kMyClassLocationCount 2
// This is an array of kMyClassLocationCount elements. The setter copies
// the new elements into existing storage.
@property (nonatomic) CGFloat *locations;
To implement it, you need to create the instance variable and define the getter and setter:
要实现它,您需要创建实例变量并定义 getter 和 setter:
@implementation MyClass {
CGFloat _locations[kMyClassLocationCount];
}
- (CGFloat *)locations {
return _locations;
}
- (void)setLocations:(CGFloat *)locations {
memcpy(_locations, locations, kMyClassLocationCount * sizeof *_locations);
}
Note that you don't need @synthesize
, because you have explicitly defined everything that @synthesize
might generate.
请注意,您不需要@synthesize
,因为您已经明确定义了@synthesize
可能生成的所有内容。
When you use an instance of the class, you can read an individual location like this:
当您使用类的实例时,您可以像这样读取单个位置:
CGFloat location1 = myInstance.locations[1];
and you can set an individual location like this:
你可以像这样设置一个单独的位置:
myInstance.locations[0] = 0.5;
and you can set all of the locations at once like this:
您可以像这样一次设置所有位置:
CGFloat newLocations[2] = { 7, 11 };
myInstance.locations = newLocations;
回答by cloosen
i think you had better code like this:
我认为你有更好的代码是这样的:
NSArray * locations = [[NSArray alloc ] initWithObjects:[NSNumber numberWithFloat:1.0f],[NSNumber numberWithFloat:0.0f],nil];