开发者

Getting CGRect from array

开发者 https://www.devze.com 2023-02-19 22:41 出处:网络
For my application I am trying to store CGRect objects into an NSMutableArray. It is loading well and printing in the log statement, but trying to take the CGRects from the array shows an error. Here

For my application I am trying to store CGRect objects into an NSMutableArray. It is loading well and printing in the log statement, but trying to take the CGRects from the array shows an error. Here is a code snippet:

CGRect lineRact = CGRectMake([[attributeDict objectF开发者_高级运维orKey:@"x"] floatValue], 
  [[attributeDict objectForKey:@"y"] floatValue], 
  [[attributeDict objectForKey:@"width"] floatValue], 
  [[attributeDict objectForKey:@"height"] floatValue]);

  [lineRactangle addObject:NSStringFromCGRect(lineRact)];

How can I get the rects back from the array?


A CGRect is a struct, not an object, and thus cannot be stored in NSArrays or NSDictionaries. You can turn it into a string and turn that string back into a CGRect, but the best way is to encapsulate it via an NSValue:

NSValue *myValue = [NSValue valueWithCGRect:myCGRect];

You can then store this NSValue object in arrays and dictionaries. To turn it back into a CGRect, you'd do:

CGRect myOtherCGRect = [myValue CGRectValue];


[lineRactangle addObject:[NSValue valueWithCGRect:lineRect]];


Use NSValue to wrap CGRect thus store them in NSArrays.

For example:

CGRect r = CGRectMake(1,2,3,4);
NSValue *v = [NSValue valueWithCGRect:rect];
NSArray *a = [NSArray arrayWithObject:v];
CGRect r2 = [[a lastObject] CGRectValue];

See documentation for the other supported structures.


Actually, I don't think any of the answers thus far really address the question ajay asked. The short answer is: You need to supply CGRectMake with the intValue, rather than the floatValue of the dictionary item. If you need to do this for several CGRects, here's a suggested method:

- (CGRect) NSArrayToCGRect: (NSDictionary *) attributeDict
{
    int x = [[attributeDict objectForKey:@"x"] intValue];
    int y = [[attributeDict objectForKey:@"y"] intValue];
    int w = [[attributeDict objectForKey:@"width"] intValue];
    int h = [[attributeDict objectForKey:@"height"] intValue];

    return CGRectFromString([NSString stringWithFormat: @"{{%d,%d},{%d,%d}}", x, y, w, h]);
}

There may be a more elegant way to accomplish this, but the above code does work.


If your object can be set with ".frame" you could use:

// {{CGFloat x,CGFloat y}, {CGFloat width,CGFloat height}}
NSString *objectCoords = @"{{116,371},{85,42}}";
myObject.frame = CGRectFromString(objectcoords);

or for multiple objects:

NSArray *objectCoords = [NSArray arrayWithObjects:
                         @"{{116,371},{85,42}}",
                         @"{{173,43},{85,42}}",
                         @"{{145,200},{85,42}}",
                         nil];

myObject1.frame = CGRectFromString([objectCoords objectAtIndex:0]);
myObject2.frame = CGRectFromString([objectCoords objectAtIndex:1]);
myObject3.frame = CGRectFromString([objectCoords objectAtIndex:2]);


CGRect is a struct you cannot put it in an NSArray. You can only add objects to it.


or something more 'extreme'... creating an array that holds arrays of CGRect(s)

movPosTable = [[NSArray alloc] initWithObjects:
            [[NSArray alloc] initWithObjects: [NSValue valueWithCGRect:[GridAB frame]], [NSValue valueWithCGRect:[GridBA frame]], [NSValue valueWithCGRect:[GridBB frame]], nil],
            [[NSArray alloc] initWithObjects: [NSValue valueWithCGRect:[GridAA frame]], [NSValue valueWithCGRect:[GridAC frame]], [NSValue valueWithCGRect:[GridBA frame]], [NSValue valueWithCGRect:[GridBB frame]], [NSValue valueWithCGRect:[GridBC frame]], nil],
            [[NSArray alloc] initWithObjects: [NSValue valueWithCGRect:[GridAB frame]], [NSValue valueWithCGRect:[GridBB frame]], [NSValue valueWithCGRect:[GridBC frame]], nil], nil];

where 'GridAA', 'GridAB' etc. correspond to UIViews

0

精彩评论

暂无评论...
验证码 换一张
取 消