从数组中获取CGRect

发布于 2024-10-26 10:03:02 字数 443 浏览 2 评论 0原文

对于我的应用程序,我尝试将 CGRect 对象存储到 NSMutableArray 中。它加载良好并在日志语句中打印,但尝试从数组中获取 CGRect 会显示错误。这是一个代码片段:

CGRect lineRact = CGRectMake([[attributeDict objectForKey:@"x"] floatValue], 
  [[attributeDict objectForKey:@"y"] floatValue], 
  [[attributeDict objectForKey:@"width"] floatValue], 
  [[attributeDict objectForKey:@"height"] floatValue]);

  [lineRactangle addObject:NSStringFromCGRect(lineRact)];

如何从数组中获取矩形?

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 objectForKey:@"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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

携余温的黄昏 2024-11-02 10:03:02

CGRect 是一个结构体,而不是一个对象,因此不能存储在 NSArrays 或 NSDictionaries 中。您可以将其转换为字符串,然后将该字符串转换回 CGRect,但最好的方法是通过 NSValue 封装它:

NSValue *myValue = [NSValue valueWithCGRect:myCGRect];

然后您可以将此 NSValue 对象存储在数组和字典中。要将其变回 CGRect,您需要执行以下操作:

CGRect myOtherCGRect = [myValue CGRectValue];

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];
妳是的陽光 2024-11-02 10:03:02
[lineRactangle addObject:[NSValue valueWithCGRect:lineRect]];
[lineRactangle addObject:[NSValue valueWithCGRect:lineRect]];
梦里泪两行 2024-11-02 10:03:02

使用 NSValue 包装 CGRect,从而将它们存储在 NSArray 中。

例如:

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

请参阅文档了解其他支撑结构。

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.

淡紫姑娘! 2024-11-02 10:03:02

事实上,我认为迄今为止的任何答案都没有真正解决阿杰提出的问题。简短的答案是:您需要为 CGRectMake 提供 intValue,而不是字典项的 floatValue。如果您需要对多个 CGRect 执行此操作,这里有一个建议的方法:

- (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]);
}

可能有一种更优雅的方法来完成此操作,但上面的代码确实有效。

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.

昨迟人 2024-11-02 10:03:02

如果您的对象可以使用“.frame”设置,您可以使用:

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

或 对于多个对象:

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

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]);
静水深流 2024-11-02 10:03:02

CGRect 是一个结构体,您不能将其放入 NSArray 中。您只能向其中添加对象。

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

墟烟 2024-11-02 10:03:02

或者更“极端”的东西...创建一个包含 CGRect 数组的数组

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

,其中“GridAA”、“GridAB”等对应于 UIViews

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文