I have an NSMutableArray and I want to test it against multiple "Win" cases.
Meaning, I want to test each 3 integer C array, against the NSMutableArray and see if the ints found in the C array are found in the NSMutablearray
These are my C arrays:
// horizontal
int winCaseHorizontal1[3] = {9, 1, 2};
int winCaseHorizontal2[3] = {3, 4, 5};
int winCaseHorizontal3[3] = {6, 7, 8};
// verticle
int winCaseVerticle1[3] = {9, 3, 6};
int winCaseVerticle2[3] = {1, 4, 7};
int winCaseVerticle3[3] = {2, 5, 8};
// diaganol
int winCaseDiagonal1[3] = {9, 4, 8};
int winCaseDiagonal2[3] = {2, 4, 6};
I want to test the numbers say in the array winCaseHorizontal1, and see if those numbers are found in my NSMutableArray.
I've read about testing if two NSMutableArrays are equal, but this is quite different.
One; this is comparing a c array with an NSMutable,
Two; I don't care if they are equal, I just want to see if the c array ints are found in the NSMutable.
Thanks in advance for the help!
You can loop through your arrays and can find if element exists in NSMutableArray.
-(BOOL)elementFound{
for(NSNumber *number in mutableArray)
{
for(int i = 0; i < sizeof(winCaseHorizontal1) / sizeof(int); i++)
{
if([number integerValue] == winCaseHorizontal1[i]){
return YES;
}
}
for(int i = 0; i < sizeof(winCaseHorizontal2) / sizeof(int); i++)
{
if([number integerValue] == winCaseHorizontal2[i]){
return YES;
}
}
for(int i = 0; i < sizeof(winCaseHorizontal3) / sizeof(int); i++)
{
if([number integerValue] == winCaseHorizontal3[i]){
return YES;
}
}
//other arrays
}
return NO;
}
If you want index or elements which found in your c arrays than you can make another array and add the index or elements in that array and return it at end of function.
EDIT: As BryanChen suggested a faster way to do that is make and NSSet from c array and check if mutableArray element exist of not.
NSMutableSet *winCaseHorizontalSet1 = [[NSMutableSet alloc] init];
for(int i = 0; i < sizeof(winCaseHorizontal1) / sizeof(int); i++)
{
[winCaseHorizontalSet1 addObject:#(winCaseHorizontal1[i])];
}
for(NSNumber *number in mutableArray)
{
BOOL isContain = [winCaseHorizontalSet1 containsObject:number];
if(isContain) return YES;
//other set of c arrays
}
Related
I'm very, very new to Objective-C (this is for a React Native app), and I am trying to pare down a list of colors and remove duplicates.
I've got this so far:
... code related to pulling pixels from an image
float flexibility = 2;
float range = 60;
NSMutableArray * colors = [NSMutableArray new];
float x = 0;
float y = 0;
for (int n = 0; n<(width*height); n++){
int index = (bytesPerRow * y) + x * bytesPerPixel;
int red = rawData[index];
int green = rawData[index + 1];
int blue = rawData[index + 2];
int alpha = rawData[index + 3];
NSArray * a = [NSArray arrayWithObjects:[NSString stringWithFormat:#"%i",red],[NSString stringWithFormat:#"%i",green],[NSString stringWithFormat:#"%i",blue],[NSString stringWithFormat:#"%i",alpha], nil];
[colors addObject:a];
y++;
if (y==height){
y=0;
x++;
}
}
free(rawData);
Now the script I am basing this off of has some code to pare down the list:
NSMutableDictionary * colourCounter = [NSMutableDictionary new];
//count the occurences in the array
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:colors];
for (NSString *item in countedSet) {
NSUInteger count = [countedSet countForObject:item];
[colourCounter setValue:[NSNumber numberWithInteger:count] forKey:item];
}
//sort keys highest occurrence to lowest
NSArray *orderedKeys = [colourCounter keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){
return [obj2 compare:obj1];
}];
//checks if the colour is similar to another one already included
NSMutableArray * ranges = [NSMutableArray new];
for (NSString * key in orderedKeys){
NSArray * rgb = [key componentsSeparatedByString:#","];
int r = [rgb[0] intValue];
int g = [rgb[1] intValue];
int b = [rgb[2] intValue];
bool exclude = false;
for (NSString * ranged_key in ranges){
NSArray * ranged_rgb = [ranged_key componentsSeparatedByString:#","];
int ranged_r = [ranged_rgb[0] intValue];
int ranged_g = [ranged_rgb[1] intValue];
int ranged_b = [ranged_rgb[2] intValue];
if (r>= ranged_r-range && r<= ranged_r+range){
if (g>= ranged_g-range && g<= ranged_g+range){
if (b>= ranged_b-range && b<= ranged_b+range){
exclude = true;
}
}
}
}
if (!exclude){ [ranges addObject:key]; }
}
//return ranges array here if you just want the ordered colours high to low
NSMutableArray * colourArray = [NSMutableArray new];
for (NSString * key in ranges){
NSArray * rgb = [key componentsSeparatedByString:#","];
float r = [rgb[0] floatValue];
float g = [rgb[1] floatValue];
float b = [rgb[2] floatValue];
UIColor * colour = [UIColor colorWithRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0f];
[colourArray addObject:colour];
}
//if you just want an array of images of most common to least, return here
//return [NSDictionary dictionaryWithObject:colourArray forKey:#"colours"];
I ultimately want to return this colourArray (the original library uses colour, I use color, so you'll sometimes see one or the other spelling - I'll probably standardize it to color when finished).
However, whenever I use this code, RN generates an error:
As far as I can tell, this is due to calling a function that doesn't exist? Is there something I am missing here, or calling incorrectly?
I am creating a client socket using POSIX socket. Since this process needs an NSArray to use the data, I wrote the following two loops to convert an unsigned char array to an NSArray and vise versa. I checked the memory situation and noticed that memory usage increases slowly when running through these functions multiple times. Can anyone spot any potential memory leaks?
//read code run in background
NSMutableArray * arr = [[NSMutableArray alloc] init];
NSUInteger i = 0;
NSNumber *aUChar = 0;
for(;;)
{
n = read(sockfd,buffer,SOCKET_SIZE);
if (n > 0)
{
[arr removeAllObjects];
for (i = 0; i < n; i++)
{
aUChar = [NSNumber numberWithUnsignedChar:buffer[i]];
[arr addObject:aUChar];
}
//do sth with arr
}
}
//write code run in main thread when function called
int n=0;
unsigned char buffer[SOCKET_SIZE];
NSUInteger i = 0;
NSNumber *tmpnum;
NSArray *data_arr = [command.arguments objectAtIndex : 0];
for (i = 0; i < SOCKET_SIZE; i++)
{
tmpnum = [data_arr objectAtIndex:i];
buffer[i] = [tmpnum charValue];
}
n = write(sockfd,buffer,TCP_SOCKET_SIZE);
How to put condition for an array to display the images. if it encounters last element of array it should go to previous order i.e., consider array have some 5 elements. 0 1 2 3 4 i need to traverse from 0 1 2 3 4 after encounter 5th object now to it should traverse to 4 3 2 1 0. How to put this logic?
I just put my logic
for(int i = 0 ; i < myArray.count; i ++)
{
NSLog(#"%d", i);
if(i == myArray.count -1)
{
for(int j = myArray.count-1 ; j >= 0; j --)
{
NSLog(#"%d", j);
}
}
}
Try with it, its working well. :)
The easiest way is doing 2 loops.
for (NSInteger i = 0;i < [array count]; i++)
{
NSLog(#"%#", [array objectAtIndex:i]);
}
for (NSInteger i = [array count] - 1;i >= 0; i--)
{
NSLog(#"%#", [array objectAtIndex:i]);
}
I am trying to fill an array of points for graphing. Here is an example of the data returned:
(
{
precipIntensity = 0;
precipProbability = 0;
time = 1398123660;
},
{
precipIntensity = 0;
precipProbability = 0;
time = 1398123720;
},
{
precipIntensity = 0;
precipProbability = 0;
time = 1398123780;
})
I can access the individual values by using:
responseArray[i][#"precipProbability"]
where i is equal to the index.
I am trying to fill an array with these points but I can't seem to get it to work. Can someone help?
I have tried:
if (!responseArray || !responseArray.count){
} else {
for (int i=0; i > [responseArray count]; i++) {
ArrayOfValues[i] = responseArray[i][#"precipProbability"];
}
and
if (!responseArray || !responseArray.count){
} else {
for (int i=0; i > [responseArray count]; i++) {
[ArrayOfValues insertObject:responseArray[i][#"precipProbability"] atIndex:i];
}
and the ArrayOfValues is always empty (null).
Your for loop condition is backwards. You want:
if (!responseArray || !responseArray.count){
} else {
for (NSUInteger i = 0; i < [responseArray count]; i++) {
[ArrayOfValues addObject:responseArray[i][#"precipProbability"]];
}
Also note the proper data type for i.
it looks like your data is already in an array. if you'd like to copy them to a mutable array you can call
NSMutableArray *mut = [responseArray mutableCopy];
if your real goal is to parse the JSON objects into an Obj-C model object, you probably want to read up on <NSFastEnumeration>
How about this:
NSArray *array = [responseArray valueForKey:#"precipProbability"];
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Non repeating random numbers in Objective-C
How to generate non repeating random numbers?
I saw this on many sites but they give in main.c file code.
When I use the main.c file the code working is fine, but when I try to convert in to my.m file it is not working.
example:
I need to get all the numbers between 0-10 randomly.and the numbers should not repeat again.
srand(time(NULL));
int s[10];
BOOL fl = 1;
for (int i = 0; i<10; i++) {
while (fl) {
s[i] = rand()%10;
fl = 0;
for (int j = 0; j < i; j++) {
if (s[j] == s[i]) {fl = 1; j = i+1;}
}
}
}
int n = 10;
NSMutableArray *numbers = [NSMutableArray array];
for (int i = 0; i <= n; i++) {
[numbers addObject:[NSNumber numberWithInt:i]];
}
NSMutableArray *result = [NSMutableArray array];
while ([numbers count] > 0) {
int r = arc4random() % [numbers count];
NSNumber *randomElement = [numbers objectAtIndex:r];
[result addObject:randomElement];
[numbers removeObjectAtIndex:r];
}
NSLog(#"%#", result);
Hope the below lines of code will help you
srand(time(NULL));
int randomIndex = rand() % limit;
[reportIDTextField setText:[NSString stringWithFormat:#"%i", randomIndex]];
Benefit of this code is it won't allow repetitive random numbers. Here limit for random numbers.
Hope this is what you are looking for.
Enjoy Coding :)
NSMutableArray *storeArray = [[NSMutableArray alloc] init];
BOOL record = NO;
int x;
for (int i=0; [storeArray count] < 10; i++) //Loop for generate different random values
{
x = arc4random() % 10;//generating random number
if(i==0)//for first time
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
else
{
for (int j=0; j<= [storeArray count]-1; j++)
{
if (x ==[[storeArray objectAtIndex:j] intValue])
record = YES;
}
if (record == YES)
{
record = NO;
}
else
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
}
}
NSLog(#" Non Repeated Random Numbers : %#",storeArray);
can u try this code may be it's use full to you
Try this:-
This will produce numbers between 0 to 10 randomly.
srand(time(NULL));
int n = 11;
int card[n];
for (int y=0; y<n; y++) {
card[y] = y;
}
for(int q= 0 ;q<3;q++) {
for (int y=0; y<(n-1); y++) {
int r = y + (arc4random() % (n-y));
int temp = card[y]; card[y] = card[r]; card[r] = temp;
}
}
for (int g=0;g<n ; g++) {
int w = card[g];
NSLog(#"%i",w);
}
You can do something like this
number = (arc4random()%10)+1; //Generates Number from 1 to 10.
save it to an Array
Then you can put a condition,That will check the number whether it is present or not in the array.
if not then add it to the array ,if it is already present then simply discard it.
A NOTE: "Never, ever add or multiply random numbers in an attempt to get 'better' randomness
:)