我无法从函数获取 NSArray 作为返回值

发布于 2024-12-14 05:35:32 字数 951 浏览 2 评论 0原文

我尝试从名为的函数中获取基于点的结构:

-(NSArray *) calcRose : (float) theta
{
    //first calculate x and y 
    //we need to get width and height of uiscreen

    //myPoint[0] = [UIScreen mainScreen].applicationFrame.size.width;

    NSMutableArray *Points = [[NSMutableArray alloc ] arrayWithCapacity:2];

    float angle = [self radians:theta];
    float side = cos(n * angle);
    int cWidth = 320;
    int cHeight = 240;
    float width = cWidth * side * sin(angle) / 2 + cWidth /2;
    float height = cHeight * side * cos(angle) / 2 + cHeight /2;

    [Points addObject:[NSNumber numberWithFloat:cWidth]];
    [Points addObject:[NSNumber numberWithFloat:cHeight]];
    NSArray *myarr = [[[NSArray alloc] initWithArray:Points ]autorelease ];

    return myarr;
}

我使用下面的代码从函数中检索数据:

NSArray *tt = [[ NSArray alloc] initWithArray:[self calcRose:3]     ];

但每次编译程序时都会出现一些错误。

我该如何解决这个问题?

I'm try to get a point based structure from a function called:

-(NSArray *) calcRose : (float) theta
{
    //first calculate x and y 
    //we need to get width and height of uiscreen

    //myPoint[0] = [UIScreen mainScreen].applicationFrame.size.width;

    NSMutableArray *Points = [[NSMutableArray alloc ] arrayWithCapacity:2];

    float angle = [self radians:theta];
    float side = cos(n * angle);
    int cWidth = 320;
    int cHeight = 240;
    float width = cWidth * side * sin(angle) / 2 + cWidth /2;
    float height = cHeight * side * cos(angle) / 2 + cHeight /2;

    [Points addObject:[NSNumber numberWithFloat:cWidth]];
    [Points addObject:[NSNumber numberWithFloat:cHeight]];
    NSArray *myarr = [[[NSArray alloc] initWithArray:Points ]autorelease ];

    return myarr;
}

I use below code for retrieving data from the function:

NSArray *tt = [[ NSArray alloc] initWithArray:[self calcRose:3]     ];

But every time I compile the program it gives me some error.

How can I solve this problem?

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

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

发布评论

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

评论(2

过期情话 2024-12-21 05:35:32

[[NSMutableArray alloc] arrayWithCapacity:2] 绝对是错误的。尝试使用 [NSMutableArray arrayWithCapacity:2] 代替。另外,您可以只使用 [[self calcRose:3]retain] 而不是 [[NSArray alloc] initWithArray:[self calcRose:3]],并且您只需要如果您打算将数组保留的时间比当前运行循环传递的时间长,则需要调用 retain

[[NSMutableArray alloc ] arrayWithCapacity:2] definitely is wrong. Try [NSMutableArray arrayWithCapacity:2] instead. Also, you can just use [[self calcRose:3] retain] rather than [[NSArray alloc] initWithArray:[self calcRose:3]], and you only need the retain call if you intend to keep the array around for longer than the current runloop pass.

风吹雪碎 2024-12-21 05:35:32

我想您已经出于问题的目的简化了示例,但您似乎做了很多不必要的工作。您问题中的代码可以重写为:

-(NSArray *) calcRose : (float) theta 
{   
    int cWidth = 320;     
    int cHeight = 240;     

    return [NSArray arrayWithObjects:[NSNumber numberWithFloat:cWidth],[NSNumber numberWithFloat:cHeight],nil];        
} 

initWithCapacity 并且使用可变数组除了让人头疼之外并没有真正给您带来任何好处。如果你想使用可变数组,只需使用 [NSMutableArray array] 创建,但看起来你并没有添加那么多对象,所以我建议的方法会更好。

此方法返回一个自动释放的数组,因此您的调用语句可以是

NSArray *tt = [self calcRose:3];

I guess you've simplified your sample for the purposes of the question but you seem to be doing a lot of unnecessary work. The code in your question could be rewritten as:

-(NSArray *) calcRose : (float) theta 
{   
    int cWidth = 320;     
    int cHeight = 240;     

    return [NSArray arrayWithObjects:[NSNumber numberWithFloat:cWidth],[NSNumber numberWithFloat:cHeight],nil];        
} 

initWithCapacity and using a mutable array is not really giving you anything besides a headache. If you want to use a mutable array, just create with [NSMutableArray array], but it doesn't look like you are adding that many objects so the method I have suggested would be better.

This method returns an autoreleased array, so your calling statement can just be

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