【问题标题】:Creating a property from a CGFloat array - iOS从 CGFloat 数组创建属性 - iOS
【发布时间】:2012-07-22 18:20:58
【问题描述】:

试图创建一个属性,所以我可以在一个类的实例上设置 CGfloat 数组值。很难弄清楚如何去做。 CGFloat 需要具有“大小”[2] 变量和数字组件,如下所示:

CGFloat locations[2] = {1.0, 0.0};

这是在我创建属性以尝试从实例设置这些值之前它在类本身中的外观(这有效):

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);

我尝试在 .h 中创建如下所示的属性

@property (nonatomic, assign) CGFloat locations;

在.m中

@synthesize locations;

但我不知道如何正确设置 [size] 的值和实例中的组件。我在设置属性时也遇到错误:错误:“将'CGFloat'(又名'float')传递给不兼容类型'const CGFloat *'(又名'const float *')的参数;使用&“

你可以在这里得到这个项目,如果你想看看tinyurl.com/cmgy482任何帮助,非常感谢。

【问题讨论】:

标签: ios xcode properties cgfloat


【解决方案1】:

从此处找到的另一个答案:"C arrays are not one of the supported data types for properties". 我认为这是我的问题。我使用的解决方案是将属性设为 NSMutableArray 并将值转换为 CGFloat。

.h

@property (nonatomic, retain) NSMutableArray *locations;

.m

CGFloat locationsFloat[[locations count]];

    for (int i = 0; i < [locations count]; i++) {
        NSNumber *number = [locations objectAtIndex:i];
        locationsFloat[i] = [number floatValue];        
    } 

视图控制器

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

    [myInstance setLocations:arrayPoints];

...

【讨论】:

    【解决方案2】:

    理想情况下,您可以像这样声明您的财产:

    @property (nonatomic) CGFloat locations[2];
    

    唉,clang 不允许您创建具有数组类型的属性。

    另一种方法是使用指针类型。您可能想写一个评论来解释它指向多少个元素:

    #define kMyClassLocationCount 2
    
    // This is an array of kMyClassLocationCount elements.  The setter copies
    // the new elements into existing storage.
    @property (nonatomic) CGFloat *locations;
    

    要实现它,你需要创建实例变量并定义getter和setter:

    @implementation MyClass {
        CGFloat _locations[kMyClassLocationCount];
    }
    
    - (CGFloat *)locations {
        return _locations;
    }
    
    - (void)setLocations:(CGFloat *)locations {
        memcpy(_locations, locations, kMyClassLocationCount * sizeof *_locations);
    }
    

    请注意,您不需要@synthesize,因为您已经明确定义了@synthesize 可能生成的所有内容。

    当您使用该类的实例时,您可以像这样读取单个位置:

    CGFloat location1 = myInstance.locations[1];
    

    你可以像这样设置一个单独的位置:

    myInstance.locations[0] = 0.5;
    

    您可以像这样一次设置所有位置:

    CGFloat newLocations[2] = { 7, 11 };
    myInstance.locations = newLocations;
    

    【讨论】:

      【解决方案3】:

      我认为你最好是这样的代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多