简体   繁体   English

如何使用Objective C返回CGPoints数组

[英]How to return an array of CGPoints using Objective C

I have a function in my project called doWorkOnSampleBuffer that returns a value of CGPoint. 我的项目中有一个名为doWorkOnSampleBuffer的函数,该函数返回CGPoint的值。

- (CGPoint)doWorkOnSampleBuffer:(CMSampleBufferRef)sampleBuffer inRects:(NSArray<NSValue *> *)rects {

    CGPoint mypoint[2];

    mypoint[0] = CGPointMake(-1, -1);
    mypoint[1] = CGPointMake(5, 5);

    return mypoint[1]
}

This function is called inside another swift file as follow: 在另一个swift文件中调用此函数,如下所示:

var myPoint = wrapper?.doWork(on: sampleBuffer, inRects: boundsArray)

Now, I would like to make the doWorkOnSampleBuffer function return the CGPoint array " mypoint ", instead of just one CGPoint value. 现在,我想让doWorkOnSampleBuffer函数返回CGPoint数组“ mypoint ”,而不只是一个CGPoint值。

So, how can I adapt my code to do that? 那么,如何修改我的代码来做到这一点?

Solved! 解决了!

The answer is: 答案是:

- (NSArray<NSValue *> *)doWorkOnSampleBuffer:(CMSampleBufferRef)sampleBuffer inRects:(NSArray<NSValue *> *)rects {

    CGPoint mypoint[2];

    mypoint[0] = CGPointMake(-1, -1);
    mypoint[1] = CGPointMake(5, 5);

    NSArray *myCGPointArray = @[[NSValue valueWithCGPoint:mypoint[0]],[NSValue valueWithCGPoint:mypoint[1]]];

    return myCGPointArray;
}

Now, if you call this function like: 现在,如果您像这样调用此函数:

var myPoint = wrapper?.doWork(on: sampleBuffer, inRects: boundsArray)

So you can access values as follows: 因此,您可以按以下方式访问值:

myPoint![0].cgPointValue.x
myPoint![0].cgPointValue.y
myPoint![1].cgPointValue.x
myPoint![1].cgPointValue.y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM