NSLog an array of CGPoints - objective-c

I have declared the following CGPoint :
CGPoint borderVertices[5000];
I have added all the values to the array if I may call it (or just a set), but now I was wondering if there is anyway I can NSLog these points or copy them to a file.
I have tried :
NSLog(#"vertices %#", NSStringFromCGPoint(borderVertices));
but I get an error.

What about:
for (NSUInteger i = 0; i < 5000; i++)
{
NSLog(#"vertices :%#", NSStringFromCGPoint(borderVertices[i]));
}

Arrays like in plain old c, needs to be iterated to print each value at it's index.
NSLog(#"vertices %#", NSStringFromCGPoint(borderVertices));
The above statement would have worked if borderVertices is of type CGPoint. But it is not, it is of type CGPoint[].

You could make an array like:
CGPoint borderVertices[5000];
float bVx[5000];
float bVy[5000];
And assign values to bVx and bVy with borderVertices.position.(x or y) in a loop and then whenever you need the coordinates... there you have it.

Related

Properly declare 2D array of Ints [duplicate]

I have the following code which works fine...
int testarr[3][3] = {
{1,1,1},
{1,0,1},
{1,1,1}
};
[self testCall: testarr];
Which calls this function:
- (void)testCall: (int[3][3]) arr {
NSLog(#"cell value is %u",arr[1][1]);
}
I need the array to be of variable length - What is the best way to declare the function?
Using blanks doesn't work:
- (void)testCall: (int[][]) arr {
Thanks for your help.
I would write this as:
- (void) testCall: (int *) aMatrice;
Doing so allows you to avoid multiple mallocs and the math to calculate a single offset in a linear array based on x, y coordinates in a 2D array is trivial. It also avoids the multiple mallocs implied by int** and the limitations of 2D array syntax perpetuated by the language.
So, if you wanted a 4x5 array, you might do:
#define WIDTH 4
#define HEIGHT 5
#define INDEXOF(x,y) ((y*WIDTH) + x)
int *myArray = malloc(sizeof(int) * 5 * ELEMS_PER_ROW);
You could then initialize the array linearly or with a nested for loop:
for(int x=0; x<width; x++)
for(int y=0; y<height; y++)
myArray[INDEXOF(x,y)] = ... some value ...;
And you would pass it to the method like:
[foo testCall: myArray];
Though you might want to also carry along the width and the height or, better yet, create a IntMatrix subclass of NSObject that wraps all of the pointer arithmetic and storage beyond a nice clean API.
(all code typed into SO)
C arrays can't be variable in more than one dimension.
You can't have this:
int testarr[][] = {
{1,1,1},
{1,0,1,2},
{1,1}
};
But you can have this:
int testarr[][3] = {
{1,1,1},
{1,0,1},
{1,1,1},
{4,5,6},
{7,8,9}
}
foo(testarr);
void foo(int param[][3])
{
printf("%d", param[3][1]); // prints 5
}
You can't use int[][] because the size of the second dimension affects how the array is laid out in memory. If you know the second dimension you can use int[][x], otherwise you'll have to use int** which can be accessed just like an array.
Why don't you just use NSArray or NSMutableArray with NSIntegers? Those array classes are of variable length, and much easier to use.
This would result in
- (void)testCall: (NSArray *) arr {
NSLog(#"cell value is %u", [[arr objectAtIndex:1] objectAtIndex:1]);
}
(Of course, you would also have to define testarr using NSArray.)
If you really want to use C arrays, making the method argument a pointer to an int with
- (void)testCall: (int*) arr {
will probably work (with the rest of the code staying the same).
call
int testarr[3][3] = {
{1,1,1},
{1,0,1},
{1,1,1}
};
[self testCall: (int *)testarr];
function
- (void)testCall: (int *) arr
{
int (*V_arr)[3] = (int(*)[3])arr;
NSLog(#"cell value is %u",V_arr[1][1]);
}

NSInteger = NSInteger -1?

I am trying to do a very simple thing but I can't figure out how;
NSInteger * a=10;
a=a-1;
NSlog(#"a=%d",a);
For some reason it's showing a=6.
How can it be?
Your problem is that you've declared the variable a as a pointer.
Most Objective-C variables are pointers, but NSInteger is an exception, because it's just typedef'd to int or long.
Your code should look like this:
NSInteger a=10;
a=a-1;
NSlog(#"a=%d",a);
When you do math on a pointer, you are actually moving the location in memory it points to. For example if the size of an NSInteger is 4 (sizeof(NSInteger) == 4), moving it -1, or in other words, a one structure size back, the pointer gets decreased by 4.
This mechanique is heavily used in C when iterating arrays of structures, e.g.
CGPoint myPoints[4];
CGPoint* point = myPoints; //get the first point
for (NSUInteger i = 0; i < 4; i++) {
CGPoint currentPoint = *point;
point++; //moves to the next point, adding sizeof(CGPoint)
}
This is called pointer arithmetic and you can write it in different ways, e.g. pointer + 1 but also point[1] or 1[point] (the last two are actually equal to *(pointer + 1)).

Getting x, y coordinate for objects in an array

i have a UIImageView object that i'm trying to get x coordinate of. I do this with code below
endingPoint.x = myObject.center.x;
Now, how can i do this if i have the same UIImageView inside of an array that i'm trying to loop through and get each object's x coordinate, some thing like below?
endingPoint.x = [posArray objectAtIndex:i].center.x;
I know it's a newbie question but i'm just starting with iOS.
You're very close. objectAtIndex: however, returns objects of type id (a generic pointer to anything), so you may not call .center (property) on it.
You must send it a message using the brackets symbols like this:
endingPoint.x = [[posArray objectAtIndex:i] center].x;
or cast the value to (UIImageView *) first:
endingPoint.x = ((UIImageView *)[posArray objectAtIndex:i]).center.x;
This would be done inside a for loop obviously.
You can do it either in an Objective-C fast enumeration loop or in a standard for loop.
Fast enumeration loop would look something like this.
for (UIImageView *image in posArray) {
endingPoint.x = image.center.x;
// Do everything else you want to do with the UIImageView inside the array.
}
This inside a loop should work:
endingPoint.x = [[posArray objectAtIndex:i] center].x;
try
endingPoint.x = [[posArray objectAtIndex:i] center].x;
or
endingPoint.x = ((UIImageView *)[posArray objectAtIndex:i]).center.x;

Access C Array within blocks (variable array count) Objective-C

Blocks are fine but what about writing C arrays?
Given this simplified situation:
CGPoint points[10];
[myArray forEachElementWithBlock:^(int idx) {
points[idx] = CGPointMake(10, 20); // error here
// Cannot refer to declaration with an array type inside block
}];
after searching a while found this possible solution, to put it in a struct:
__block struct {
CGPoint points[100];
} pointStruct;
[myArray forEachElementWithBlock:^(int idx) {
pointStruct.points[idx] = CGPointMake(10, 20);
}];
this would work but there is a little limitation I have to create the c array dynamically:
int count = [str countOccurencesOfString:#";"];
__block struct {
CGPoint points[count]; // error here
// Fields must have a constant size: 'variable length array in structure' extension will never be supported
} pointStruct;
How can I access my CGPoint array within a block?
OR
Is it even possible at all or do I have to rewrite the block method to get the full functionality?
Another simple answer which works for me is the following:
CGPoint points[10], *pointsPtr;
pointsPtr = points;
[myArray forEachElementWithBlock:^(int idx) {
pointsPtr[idx] = CGPointMake(10, 20);
}];
Maybe you can allocate the array on the heap?
// Allocates a plain C array on the heap. The array will have
// [myArray count] items, each sized to fit a CGPoint.
CGPoint *points = calloc([myArray count], sizeof(CGPoint));
// Make sure the allocation succeded, you might want to insert
// some more graceful error handling here.
NSParameterAssert(points != NULL);
// Loop over myArray, doing whatever you want
[myArray forEachElementWithBlock:^(int idx) {
points[idx] = …;
}];
// Free the memory taken by the C array. Of course you might
// want to do something else with the array, otherwise this
// excercise does not make much sense :)
free(points), points = NULL;

interacting with UIViews stored inside a NSMutableArray

a big noob needs help understanding things.
I have three UIViews stored inside a NSMutableArray
lanes = [[NSMutableArray arrayWithCapacity:3] retain];
- (void)registerLane:(Lane*)lane {
NSLog (#"registering lane:%i",lane);
[lanes addObject:lane];
}
in the NSLog I see: registering lane:89183264
The value displayed in the NSLog (89183264) is what I am after.
I'd like to be able to save that number in a variable to be able to reuse it elsewhere in the code.
The closest I could come up with was this:
NSString *lane0 = [lanes objectAtIndex:0];
NSString *description0 = [lane0 description];
NSLog (#"description0:%#",description0);
The problem is that description0 gets the whole UIView object, not just the single number (dec 89183264 is hex 0x550d420)
description0's content:
description0:<Lane: 0x550d420; frame = (127 0; 66 460); alpha = 0.5; opaque = NO; autoresize = RM+BM; tag = 2; layer = <CALayer: 0x550d350>>
what I don't get is why I get the correct decimal value with with NSLog so easily, but seem to be unable to get it out of the NSMutableArray any other way. I am sure I am missing some "basic knowledge" here, and I would appreciate if someone could take the time and explain what's going on here so I can finally move on. it's been a long day studying.
why can't I save the 89183264 number easily with something like:
NSInteger * mylane = lane.id;
or
NSInteger * mylane = lane;
thank you all
I'm really confused as to why you want to save the memory location of the view? Because that's what your '89183264' number is. It's the location of the pointer. When you are calling:
NSLog (#"registering lane:%i",lane);
...do you get what's actually being printed out there? What the number that's being printed means?
It seems like a really bad idea, especially when if you're subclassing UIView you've already got a lovely .tag property which you can assign an int of your choosing.
You're making life infinitely more complex than it needs to be. Just use a pointer. Say I have an array containing lots of UIViews:
UIView *viewToCompare = [myArray objectAtIndex:3];
for (id object in myArray) {
if (object == viewToCompare) {
NSLog(#"Found it!");
}
}
That does what you're trying to do - it compares two pointers - and doesn't need any faffing around with ints, etc.