我无法从函数获取 NSArray 作为返回值
我尝试从名为的函数中获取基于点的结构:
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[[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 theretain
call if you intend to keep the array around for longer than the current runloop pass.我想您已经出于问题的目的简化了示例,但您似乎做了很多不必要的工作。您问题中的代码可以重写为:
initWithCapacity
并且使用可变数组除了让人头疼之外并没有真正给您带来任何好处。如果你想使用可变数组,只需使用[NSMutableArray array]
创建,但看起来你并没有添加那么多对象,所以我建议的方法会更好。此方法返回一个自动释放的数组,因此您的调用语句可以是
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:
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