NSMutableArray containsObject returns true, but it shouldnt - objective-c

I found similar questions, but -containsObject is not working like I expect.
My problem is the NSMutableArray -containsObject method returns true when it shouldn't,
when trying to generate random UNIQUE colors and add to an array.
What is the best way to check if NSMutableArray contains an object with same values.
NSMutableArray *color_arr=[NSMutableArray array];
UIColor *t;
for(int i=0; i<100; i+=1)
{
int r = arc4random()%256;
int g = arc4random()%256;
int b = arc4random()%256;
t=[UIColor colorWithRed:r green:g blue:b alpha:255];
if (![color_arr containsObject:t])
[color_arr addObject:t];
//[t release];//is t need to be released here on non-arc project? well Im not sure.
}
NSLog(#"total:%d",[color_arr count]);
The NSLog() always says array count is 1.

New Edit:
The structure of your for() loop is wrong too. You are declaring the UIColor before the loop begins. You should be declaring the color AFTER the loop begins:
for (i=0;i<100;i++) {
int rInt = arc4random()%256;
float rFloat = (float)rInt/255.0f;
//same with gInt, bInt
//make gFloat and bFloat this way
UIColor *t = [UIColor colorWithRed:rFloat green:gFloat blue:bFloat alpha:1];
if (![color_arr containsObject:t]) {
[color_arr addObject:t];
}
NSLog(#"%i",color_arr.count);
}
UIColor doesn't use integer values, it uses float values. Try dividing your integer by 255 and then setting those as r, g, b.
Like:
int rInt = arc4random()%256;
float rFloat = (float)rInt/255.0f;
//same with gInt, bInt
//make gFloat and bFloat this way
t = [UIColor colorWithRed:rFloat green:gFloat blue:bFloat alpha:1];

Related

CGContextFillRects: No matching function for call

I'm trying to optimize the performance in one of my components. The component needs to draw some (10 to 200) rectangles in it's drawRect method, which is triggered about 20 times per second.
Everything works when I use the CGContextFillRect method on each CGRect separately. I want to test if grouping the drawing into one single call with CGContextFillRects on an array of CGRects would increase performance.
The method CGContextFillRects gives me a compiler error No matching function for call to 'CGContextFillRects'.
This code is inside a .mm file. Should I import something before the CGContextFillRects method can be used?
This is what i'm trying to do:
- (void) drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, self.fillColor.CGColor);
//check if some objects are present
if (self.leftDrawBuffer && self.rightDrawBuffer){
UInt32 xPosForRect = self.leftPadding;
NSMutableArray *rectsToFill = [[NSMutableArray alloc] init];
for (int drawBufferLRIndex = 0; drawBufferLRIndex < 2; drawBufferLRIndex++){
Float32 *drawBuffer_ptr = self.leftDrawBuffer;
if (drawBufferLRIndex > 0){
drawBuffer_ptr = self.rightDrawBuffer;
}
for (int i=0; i< kAmountOfBarsPerChannel; i=i+1){
Float32 amp = drawBuffer_ptr[i];
Float32 blockNumber = 1.0f;
UInt32 yPosForRect = self.bounds.size.height - self.heightPerBlock;
while (blockNumber <= self.blocksPerLine && blockNumber / self.blocksPerLine < amp){
CGRect rect= CGRectMake(xPosForRect, yPosForRect, self.widthPerBlock, self.heightPerBlock);
[rectsToFill addObject:[NSValue valueWithCGRect:rect]];
//Using the method below works and gives me the expected result
//CGContextFillRect(context, rect);
blockNumber++;
yPosForRect -= self.heightPerBlock + self.vPaddingPerBlock;
}
xPosForRect += self.widthPerBlock + self.hPaddingPerBlock;
}
}
//This is the added code where i try to use CGContextFillRects
//1 -> transform to a c array of CGRects
const CGRect *cRects[rectsToFill.count];
for (int i = 0; i < rectsToFill.count; ++i) {
CGRect rect = [[rectsToFill objectAtIndex:i] CGRectValue];
cRects[i] = &rect;
}
size_t size = rectsToFill.count;
//2 -> trigger the method to fill all rects at once
//this method gives me the compiler error 'No matching function for call to 'CGContextFillRects''
CGContextFillRects(context, cRects, size);
}
CGContextRestoreGState(context);
}
The problem is how you convert the rects to a C array. You make pointers to the rects that are temporarily stored on the stack. There are two problems with this. First, the rects are gone with each loop iteration, so you can't do that. Second, You should pass a pointer to an array of CGRects, not an array of pointers to CGRect.
This will likely solve it:
CGRect cRects[rectsToFill.count]; // Replace your lines from this
for (int i = 0; i < rectsToFill.count; ++i) {
CGRect rect = [[rectsToFill objectAtIndex:i] CGRectValue];
cRects[i] = rect;
}
size_t size = rectsToFill.count;
CGContextFillRects(context, cRects, size); // To this
Please note the re-declaration of the cRects array and the change in the assignment.

NSMutableArray array isn't created

On line that is marked with a comment "RIGHT HERE" (the last if statement) compiler tells me "index 0 beyond bounds for empty array" which I interpret as - the array wasn't created.
The idea is - in that last loop I'm going to sum up areas of already existing triangles with calc areas.
NSMutableArray *xCoordinate = [NSMutableArray array];
NSMutableArray *yCoordinate = [NSMutableArray array];
// some code in here...
int t;
int g = [xCoordinate count];
if (g<3) {
printf("Please enter at least 3 value pairs to form a polygon\n");
return 0;
}
NSMutableArray *arrayOfCorners = [NSMutableArray array];
NSMutableArray *arrayOfTriangls = [NSMutableArray array];
for (t=0; t < g; t++) {
float x = [[xCoordinate objectAtIndex:t] floatValue];
float y = [[yCoordinate objectAtIndex:t] floatValue];
RectangleCorner *corner = [[RectangleCorner alloc] initWithX:x andY:y];
// 3. add this corner to an array.
[arrayOfCorners addObject:corner];
if (t>=2) {
// 4. forming a triangle.
Triangle *triangle = [[Triangle alloc] init];
// 5. calc its sides length. Calculate lengths and assignes those values to side1, side2, side3 properties of the triangle.
[triangle sideLengthWithVert:arrayOfCorners[t] vert2:arrayOfCorners[t+1] vert3:arrayOfCorners[t+2]];
// 6. calc triangle area.
[triangle calcArea];
// 7. adding this triangle's area to our array
[arrayOfTriangls addObject:triangle];
}
// 8. adding up areas of triangles (if we have an array of them)
int i = 0;
NSInteger nsi = (NSInteger) i;
// RIGHT HERE.
Triangle *testingTriangle = [arrayOfTriangls objectAtIndex:nsi];
if (testingTriangle)
{
int y = [arrayOfTriangls count];
int r;
for (r=0; r<=y; r++) {
float p;
int q = r;
NSInteger ndi = (NSInteger) q;
Triangle *triangle = [arrayOfTriangls objectAtIndex:ndi];
p +=triangle.area;
printf("Polygon's Area is %f", p);
}
}
}
Lets walk through your code:
if (g<3) {
printf("Please enter at least 3 value pairs to form a polygon\n");
return 0;
}
NSMutableArray *arrayOfCorners = [NSMutableArray array];
NSMutableArray *arrayOfTriangls = [NSMutableArray array];
for (t=0; t < g; t++) {
You create array of triangles before the loop, then go with the for loop with at least g = 3.
Now lets start with t = 0, and go through the loop:
float x = [[xCoordinate objectAtIndex:t] floatValue];
float y = [[yCoordinate objectAtIndex:t] floatValue];
RectangleCorner *corner = [[RectangleCorner alloc] initWithX:x andY:y];
// 3. add this corner to an array.
[arrayOfCorners addObject:corner];
if (t>=2) {
// 4. forming a triangle.
Triangle *triangle = [[Triangle alloc] init];
// 5. calc its sides length. Calculate lengths and assignes those values to side1, side2, side3 properties of the triangle.
[triangle sideLengthWithVert:arrayOfCorners[t] vert2:arrayOfCorners[t+1] vert3:arrayOfCorners[t+2]];
// 6. calc triangle area.
[triangle calcArea];
// 7. adding this triangle's area to our array
[arrayOfTriangls addObject:triangle];
}
The if is not true at this point, so you didn't add a triangle yet to the array. The triangle array contains now 0 objects. Lets move on:
int i = 0;
NSInteger nsi = (NSInteger) i;
// RIGHT HERE.
Triangle *testingTriangle = [arrayOfTriangls objectAtIndex:nsi];
Now you try to get the object at index 0, but the array contains 0 objects. I suppose for t >= 2 your code works, but for t = 0, 1 your code crashes.
If your array was not created, arrayOfTriangls would be nil, and calling objectAtIndex: would return nil and not crash.
Instead, "index 0 beyond bounds for empty array" means that you tried to access the first element of an empty (but initialized) array.
Your problem is that your code 7. is not executed for the first two loop iteration (if t>=2), hence the empty array.

Am I using Objective-C collections properly here?

I'm attempting to write an iPhone game. This function is intended to apply gravitational force to several objects. I'm porting it from Python and I'm wondering if my use of dictionaries and arrays as tuples makes sense and is typical/idiomatic in Objective C. Any comments on the code appreciated.
+ (void)updateBodies:(NSMutableArray*)bodies {
NSMutableDictionary* totals = [NSMutableDictionary dictionaryWithCapacity:[bodies count]];
for (Body* body in bodies) {
if (body.fixed) {
continue;
}
float tx;
float ty;
for (Body* other in bodies) {
if (other == body) {
continue;
}
float dx = other.x - body.x;
float dy = other.y - body.y;
float dist2 = pow(dx, 2) + pow(dy, 2);
float dist = sqrt(dist2);
float mass = pow(other.radius, 3);
float magnitude = G * mass / dist2;
float ux = dx / dist;
float uy = dy / dist;
tx += ux * magnitude;
ty += uy * magnitude;
}
NSNumber* ntx = [NSNumber numberWithFloat:tx];
NSNumber* nty = [NSNumber numberWithFloat:ty];
NSArray* tuple = [NSArray arrayWithObjects:ntx, nty, nil];
[totals setObject:tuple forKey:body];
}
for (Body* body in [totals allKeys]) {
NSArray* tuple = [totals objectForKey:body];
float tx = [[tuple objectAtIndex:0] floatValue];
float ty = [[tuple objectAtIndex:1] floatValue];
body.dx += tx;
body.dy += ty;
}
}
The only problem you should be aware of is that NSDictionary copies its keys. So Body needs to implement NSCopying and the instances of Body in totals are not necessarily the same instances in the passed in bodies array depending on how you implement NSCopying.
The approach I would use would be to consider velocity as a property of the body. That way you don't need a dictionary to associate the body to its velocity, you can just iterate through the array itself.
Talking of iterating. You can halve the number of iterations and some calculations by calculating the velocity of the other body at the same time as the first body. i.e. your inner loop would only iterate through the bodies that come after the outer loop body in the array.
It would mean you can't use fast iteration, so you'd have to profile to figure out which approach is faster.
On a minor note, I think
for ....
{
if (!condition)
{
continue;
}
// do stuff
}
is really ugly. What's wrong with:
for ....
{
if (condition)
{
// do stuff
}
}
You could used block enumeration for final update:
[totals enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
Body* body = key;
NSArray* tuple = key;
body.dx += [[tuple objectAtIndex:0] floatValue];
body.dy += [[tuple objectAtIndex:1] floatValue];
}];
An other solution could be to not used NSDictionary and NSArray and use a C array. It should be faster than using (and create) objects.

Declare Dynamic Array

How can I declare dynamic array? For example:
int k=5;
I want to have an array like below:
int myArray[k];
if i read the question right.. (unlikely at this point)
NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:k];
Sometimes true arrays (not NSArray) are really needed. See for example indexPathWithIndexes:length: in NSIndexPath, it take array of uintegers as parameter. For array allocation you should use the following approach:
NSUInteger *arr = (NSUInteger*)malloc(elementsCount * sizeof(NSUInteger) );
arr[0] = 100;
free(arr);
In Objective-C, the standard way to do this is to use the NSMutableArray class. This is a container that can hold any object (note that int is not an object! You'll have to wrap your integers in NSNumber.) Quick example:
NSMutableArray* someIntegers = [[NSMutableArray alloc] initWithCapacity:1];
[someIntegers addObject:[NSNumber numberWithInt:2]];
//I've added one thing to my array.
[someIntegers addObject:[NSNumber numberWithInt:4]];
//See how I can put more objects in than my capacity allows?
//The array will automatically expand if needed.
//The array now contains 2 (at index 0) and 4 (at index 1)
int secondInteger = [[someIntegers objectAtIndex:1] intValue];
//Retrieving an item. -intValue is needed because I stored it as NSNumber,
//which was necessary, because NSMutableArray holds objects, not primitives.
Well in my book it's ok to use VLAs in Objective-C.
So something like
int foo = 10;
int bar[foo];
is allowed. Of course this is not a dynamic array as in automatically adjusting its size. But if you only need a native array on the stack that's fine.
You can use Objetive-C++.
First rename your class like this: MyClass.mm the ".mm" extension tells Xcode that this clas is a Objetive-C++ class, not a Objetive-C class.
then you can use dynamics C++ arrays like this:
int *pixels = new int[self.view.size.width];
for (int offset = 0; offset = self.view.size.width; offset++) {
pixeles[offset] = rawData[offset];
}
then you can pass "pixels" in a method:
Scan *myScan = [[Scan alloc] initWhithArray:pixels];
the method "initWithScan" is declared like this:
-(id)initWithArray:int[]pixels;
the "initWithScan" implementation is like this:
-(id)initWithScan:int[]pixels {
if (self = [super init]) {
for (int i = 0; i < self.myView.size.width; i++) {
NSLog(#"Pixel: %i", pixels[i];
}
}
return self;
}
I hoppe this was useful.

Does iPhone SDK Objective C support functions inside of functions?

I know that javascript, for example supports functions inside of functions, like so:
function doSomething(){
function doAnothingThing(){
//this function is redefined every time doSomething() is called and only exists inside doSomething()
}
//you can also stick it inside of conditions
if(yes){
function doSomethingElse(){
//this function only exists if yes is true
}
}
}
Does objective-c support this? Theoretical example:
-(void) doSomething:(id) sender{
-(void) respondToEvent: (id) sender{
//theoretically? ... please?
}
}
BONUS: What is the proper term for a "local" function?
A bit late, but you can place an inline block into a function, which kind of acts like your nested function questions.
-(int)addNumbers:(int)a withB:(int)b withC:(int)c {
// inline block
int(^inlineaddNumbers)(int, int) = ^(int a, int b) {
return a + b;
};
if( a == 0 ) return inlineaddNumbers(b,c);
else return inlineaddNumbers(a,c);
}
It's a bit messy, but it works!
The usual term is nested function. gcc supports nested functions as an extension to C (disabled by default). I don't think this option is available with Objective-C (or C++) with gcc though, and even if it were it's probably not a good idea to use it (portability etc).
gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
By default Xcode disallows nested functions.
If you want to switch them on, open up the Info for your project, go to the Build tab, and set "Other C flags" (under the section titled "GCC 4.2 - Language") to "-fnested-functions".
(This is stored in your project.pbxproj file as "OTHER_CFLAGS = "-fnested-functions";"
Expanding the answer provided by Gui13 a little bit, with object parameters.
The following code snippet demonstrates how to draw an 11x5 set of UILabels.
// inline block - to be called as a private function
UILabel *(^createLabel)(CGRect, NSString *, UIColor *) = ^UILabel *(CGRect rect, NSString *txt, UIColor *color) {
UILabel *lbl = [UILabel new];
lbl.frame = rect;
lbl.textColor = color;
lbl.backgroundColor = [UIColor whiteColor];
lbl.font = [UIFont systemFontOfSize:30.f];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.text = txt;
return lbl;
};
// loop to create 11 rows of 5 columns over the whole screen
float w = CGRectGetWidth([UIScreen mainScreen].bounds);
float h = CGRectGetHeight([UIScreen mainScreen].bounds);
float top = h / 10; //start at 10% from top
float vOffset = h / 13; //space between rows: 7.6% of screen height
NSArray *xFrom, *xTo; //columns to start at 5%, 20%, 40%, 60%, 80%
xFrom = #[#(1.f/20), #(1.f/5), #(2.f/5), #(3.f/5), #(4.f/5)];
xTo = #[#(1.f/5-1.f/16), #(2.f/5-1.f/16), #(3.f/5-1.f/16), #(4.f/5-1.f/16), #(19.f/20)];
#define SFMT(format...) [NSString stringWithFormat:format]
for (int row=0; row<11; row++) {
for (int col=0; col<5; col++) {
CGRect rect = CGRectMake([xFrom[col] floatValue]*w, top+row*vOffset, [xTo[col] floatValue]*w-[xFrom[col] floatValue]*w, vOffset*0.9);
UILabel *lbl = createLabel(rect, SFMT(#"%i-%i", row, col), [UIColor blueColor]);
[<my-view> addSubview:lbl];
}
}
Here is the output for this code:
#Moshe You cannot actually provide the nested functions inside the Objective C. Instead you can use the feature in latest Swift 3 which enables this feature. It will be like below:
func someFunction(input:String)->String
{
var inputString = input;
func complexFunctionOnString()
{
inputString = "Hello" + input;
}
complexFunctionOnString();
return inputString;
}
someFunction("User");