简体   繁体   English

从CGFloat数组创建属性 - iOS

[英]Creating a property from a CGFloat array - iOS

Trying to create a property, so I can set CGfloat array values on an instance of a class. 尝试创建属性,因此我可以在类的实例上设置CGfloat数组值。 Having a hard time figuring out how to do it. 很难搞清楚如何做到这一点。 The CGFloat needs to have "size" [2] variable and numeric components like below: CGFloat需要具有“size”[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. 但我无法弄清楚如何正确设置[size]和实例中的组件的值。 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 &" 我在设置属性时也遇到错误:错误:“将'CGFloat'(又名'浮动')传递给不兼容类型'const CGFloat *'(又名'const float *')的参数;将地址带为&

you can get the project here is you feel like taking a look tinyurl.com/cmgy482 Any help much appreciated. 你可以在这里得到这个项目你想看看tinyurl.com/cmgy482任何帮助非常感谢。

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;

From another answer found here: "C arrays are not one of the supported data types for properties". 从这里找到的另一个答案: “C数组不是属性支持的数据类型之一”。 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. 我使用的解决方案是使属性成为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 的viewController

- (void)viewDidLoad
{
    NSMutableArray *arrayPoints = [[NSMutableArray alloc]init];    
    [arrayPoints addObject:[NSNumber numberWithFloat:1.0]];
    [arrayPoints addObject:[NSNumber numberWithFloat:0.0]];

    [myInstance setLocations:arrayPoints];

...

我认为你有更好的代码:

NSArray * locations = [[NSArray alloc ] initWithObjects:[NSNumber numberWithFloat:1.0f],[NSNumber numberWithFloat:0.0f],nil];

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

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