Noob 질문은 여기에 있습니다.다른 클래스의 float 배열을 호출
배열 플로트 itemsPosition [20] [20]을 사용하는 클래스 A가 있고 그 클래스에 액세스 할 수있는 다른 클래스 B가있는 경우 어떻게해야합니까?
나는 보통은 클래스 A와 다른 개체에 대한하지만,이 경우 액세스를 ALLOC하기 위해 무엇을, 나는 클래스 A.
모든 아이디어를 내 float 배열을 합성 수없는 이유는 무엇입니까?
Noob 질문은 여기에 있습니다.다른 클래스의 float 배열을 호출
배열 플로트 itemsPosition [20] [20]을 사용하는 클래스 A가 있고 그 클래스에 액세스 할 수있는 다른 클래스 B가있는 경우 어떻게해야합니까?
나는 보통은 클래스 A와 다른 개체에 대한하지만,이 경우 액세스를 ALLOC하기 위해 무엇을, 나는 클래스 A.
모든 아이디어를 내 float 배열을 합성 수없는 이유는 무엇입니까?
@synthesize
NSValue
에는 배열에 대한 포인터가 있습니다.
@interface SomeObject : NSObject
@property (strong, nonatomic) NSValue *itemsPosition;
@end
@implementation SomeObject
@synthesize itemsPosition;
...
static float anArray[20][20];
...
- (void) someMethod
{
... add items to the array
[self setItemsPosition:[NSValue valueWithPointer:anArray]];
}
@end
@implementation SomeOtherObject
...
- (void) someOtherMethod
{
SomeObject *obj = [[SomeObject alloc] init];
...
float (*ary2)[20] = (float(*)[20])[obj.itemsPosition pointerValue];
...
}
+1 너무 ... NSValues는이 문제를 해결하기위한 매우 우아하고 매우 객관적인 C 방법입니다. –
NSValues에 대해 전혀 알지 못했습니다! 큰! 이것을 시도합니다 –
이것은 작동합니다! 그러나 만약 그 배열이 [20] [10]이라면? –
플로트는 C 유형이므로 일반 Objective C 속성을 사용하여 직접 액세스 할 수 없습니다.
가장 좋은 방법은 클래스 B가 첫 번째 배열 항목 "itemsPosition
"에 대한 포인터에 액세스 할 수있게 해주는 "접근 자"함수를 만드는 것입니다. E.G. "itemsPosition[0][0]
"클래스에서
A의 .H 파일 :
float itemsPosition[20][20];
- (float *) getItemsPosition;
과하는 .m 파일에
: 당신이 알고 있기 때문에- (float *) getItemsPosition
{
// return the location of the first item in the itemsPosition
// multidimensional array, a.k.a. itemsPosition[0][0]
return(&itemsPosition[0][0]);
}
와 클래스 B의
이 다차원 배열의 크기는 20 x 20이면 다음 배열 항목의 위치를 쉽게 찾을 수 있습니다.float * itemsPosition = [classA getItemsPosition];
for(int index = 0; index < 20; index++)
{
// this takes us to to the start of itemPosition[index]
float * itemsPositionAIndex = itemsPosition+(index*20);
for(int index2 = 0; index2 < 20; index2++)
{
float aFloat = *(itemsPositionAIndex+index2);
NSLog(@"float %d + %d is %4.2f", index, index2, aFloat);
}
}
}
Le 샘플 Xcode 프로젝트를 어딘가에 배치하는 것이 유용 할 지 안다.
고마워! 실제로 이것을 시도했지만 itemsPosition 대신 itemsPosition을 반환했습니다. 이것을 시도 할 것이다! –
합성 할 수없는 이유는 무엇입니까? 세터 사용은 어떻습니까? – brainray
플로트가 객체가 아니기 때문에 나는 생각한다 –