Newb-ish iOS developer here...
Newb-ish iOS开发人员在这里......
I have a viewController that contains a property that is an object of a custom class. We'll call that custom class ClassA. The object of ClassA has a property that is an NSMutableArray of objects of another custom class we'll call ClassB. ClassB has a property that is also an NSMutableArray of objects of type CLLocation.
我有一个viewController,它包含一个属性,它是一个自定义类的对象。我们称之为自定义类ClassA。 ClassA的对象有一个属性,它是我们称之为ClassB的另一个自定义类的对象的NSMutableArray。 ClassB有一个属性,它也是CLLocation类型的对象的NSMutableArray。
From inside a method in the viewController I need to create a C array of CLLocationCoordinate2D structs (CLLocationCoordinate2D is a property of CLLocation). Each of these CLLocationCoordinate2D's needs to come from all of the CLLocation objects held by all of the objects in ClassB and ClassA. If I'm understanding what I've wrought, I believe I have a 3-D array.
从viewController中的方法内部我需要创建一个CLLocationCoordinate2D结构的C数组(CLLocationCoordinate2D是CLLocation的一个属性)。这些CLLocationCoordinate2D中的每一个都需要来自ClassB和ClassA中所有对象所拥有的所有CLLocation对象。如果我理解我所做的事情,我相信我有一个三维阵列。
I'm stuck on exactly how to go about assembling this array of structs. If it were just one array I would do something like this:
我一直坚持如何组装这个结构数组。如果它只是一个数组我会做这样的事情:
NSUInteger numberOfSteps = [objectOfClassX count];
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [objectOfClassX objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
However I'm struggling with the syntax of getting each object in the first array, then inside that each object in the second array, then inside that the CLLocationCoordinate2D.
然而,我正在努力解决在第一个数组中获取每个对象的语法,然后在第二个数组中的每个对象内部,然后在CLLocationCoordinate2D内部。
Any help is appreciated.
任何帮助表示赞赏。
Thanks,
n
谢谢,n
2 个解决方案
#1
2
Iterate once to get the total count of coordinates, then iterate again to copy them into a newly-allocated array.
迭代一次以获得坐标的总数,然后再次迭代以将它们复制到新分配的数组中。
NSInteger coordCount = 0, coordIndex = 0;
for(ClassA *a in collectionOfA)
for(ClassB *b in a.collectionOfB)
coordCount += [b.locations count];
CLLocationCoordinate2D coords[coordCount];
for(ClassA *a in collectionOfA)
for(ClassB *b in a.collectionOfB)
for(CLLocation *location in b.locations)
coords[coordIndex++] = location.coordinate;
#2
-1
just have a try : coordinates = [NSMutableArray arrayWithArray: objectOfClassX];
试试看:coordinates = [NSMutableArray arrayWithArray:objectOfClassX];
#1
2
Iterate once to get the total count of coordinates, then iterate again to copy them into a newly-allocated array.
迭代一次以获得坐标的总数,然后再次迭代以将它们复制到新分配的数组中。
NSInteger coordCount = 0, coordIndex = 0;
for(ClassA *a in collectionOfA)
for(ClassB *b in a.collectionOfB)
coordCount += [b.locations count];
CLLocationCoordinate2D coords[coordCount];
for(ClassA *a in collectionOfA)
for(ClassB *b in a.collectionOfB)
for(CLLocation *location in b.locations)
coords[coordIndex++] = location.coordinate;
#2
-1
just have a try : coordinates = [NSMutableArray arrayWithArray: objectOfClassX];
试试看:coordinates = [NSMutableArray arrayWithArray:objectOfClassX];