I have a randomSpeed variable, and I add those randomSpeeds every few seconds in an mutable array like this ( as object ) .
NSNumber *randomSpeed = [NSNumber numberWithFloat:(arc4random() % 12)-3];
[self.UfoSpeed addObject:randomSpeed];
Now I am getting the index of an Object of another array via
NSUInteger fooIndex = [self.Ufos indexOfObject: ufo];
and now I need the randomSpeed of the index of the Object I got before as a float to add it to another float later on.
For example I need:temp = self.UfoSpeed[fooIndex];
then i add ufo.position.x + temp;
But its not working like that because the randomSpeed is an object of NSNumber and not a float/int.
You can convert NSNumber object to float like this [num floatValue];
Related
I have the following object which contains an array of NSDictionary. I wonder how could I able to get the sum of Quantity from this object.
In Objective-C there is a very convenient way using KVC Collection Operators:
NSNumber *sum = [sharedData.orderItems valueForKeyPath:#"#sum.Quantity.integerValue"];
NSInteger integerSum = sum.integerValue;
The other simple operators are:
#count - count of objects
#avg - average value
#max - maximum value
#min - minimum value
int sum=0;
for(NSDictionary *item in sharedData.orderitems){
sum = sum + [[item objectForKey:#"Quantity"] intValue];
}
NSLog(#"Sum = %d",sum);
How would I get the nearest float in my array to a float of my choice? Here is my array:
[1.20, 1.50, 1.75, 1.95, 2.10]
For example, if my float was 1.60, I would like to produce the float 1.50.
Any ideas? Thanks in advance!
You can do it by sorting the array and finding the nearest one.
For this you can use sortDescriptors and then your algorithm will go.
Even you can loop through, by assuming first as the required value and store the minimum absolute (abs()) difference, if next difference is lesser than hold that value.
The working sample, however you need to handle other conditions like two similar values or your value is just between two value like 2 lies between 1 and 3.
NSArray *array = #[#1.20, #1.50, #1.75, #1.95, #2.10];
double my = 1.7;
NSNumber *nearest = array[0];
double diff = fabs(my - [array[0] doubleValue]);
for (NSNumber *num in array) {
double d = [num doubleValue];
if (diff > fabs(my - d) ) {
nearest = num;
diff = my - d;
}
}
NSLog(#"%#", nearest);
What is the difference between float[] and float*?
Also, how can I convert a float array to float *? I need to get a float * and open it, then apply a filter and send it as a float * into my FFT method, but I don't know how to do it because I don't know the real difference between them.
An array usually is a pointer to the first member of the list. When using Array[Identifier], you are accessing to *(p+Identifier).
Making a new array will define a series of pointer next to another, which will make it's use way easier.
You can set your float array in the following ways:
float array1[100] = { 0 };
float *dataArray = (float*)malloc(sizeof(float) * 100);
float *pointerToFloatArray = array1;
These points all relate to C:
the name of an array can be decomposed — i.e. implicitly converted — to a pointer to its first element;
in an array, elements are stored contiguously;
the syntax a[8] is just shorthand for *(a + 8); and
adding n to a pointer, p, is defined to add n * sizeof(*p).
So an array differs from a pointer by being a semantically different thing. But you can supply the name of an array anywhere a pointer is required as it'll be converted.
Separately, you can also add an offset to any pointer using subscript syntax.
Objective-C is a strict superset of C. So these rules also apply to the use of the primitive types in Objective-C.
To understand the distinction, think in terms of mutability. The following is invalid:
char array[];
char value;
array = &value;
You can't reassign array. It is the name of an array. array itself is not mutable at runtime, only the things within it are. Conversely the following is valid:
char *pointer;
char value;
pointer = &value;
You can reassign pointer as often as you like. There's a mutable pointer and you can use it to point to anything.
You can use C-style arrays, like described in this answer: https://stackoverflow.com/a/26263070/3399208,
but better way - is using Objective-C containers and Objective-C objects, for example NSNumber * :
NSArray *array = [#1, #2, #3];
or
NSMutableArray *array = [NSMutableArray array];
NSNumber *number1 = [NSNumber numberWithFloat:20.f];
NSNumber *number2 = #(20.f);
[array addObject:number1];
[array addObject:number2];
So I have a basic array:
NSMutableArray *answerButtonsArrayWithURL = [NSMutableArray arrayWithObjects:self.playView.coverURL1, self.playView.coverURL2, self.playView.coverURL3, self.playView.coverURL4, nil];
The objects inside are strings. I want to access a random object from that array
int rndValueForURLS = arc4random() % 3;
and assigning it a value. I've tried manny different approaches but my recent one is
[[answerButtonsArrayWithURL objectAtIndex:rndValueForURLS] stringByAppendingString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];
Any help will be much appreciated. Thanks
You need to assign it. You're already building the new value like that:
NSString *oldValue = answerButtonsArrayWithURL[rndValueForURLS];
NSString *newValue = [oldValue stringByAppendingString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];
The part you're missing :
answerButtonsArrayWithURL[rndValueForURLS] = newValue;
Above would be the way to replace the immutable string with another. If the strings are mutable, that is, they were created as NSMutableString, you could do:
NSMutableString *value = answerButtonsArrayWithURL[rndValueForURLS];
[value appendString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];
Note:
Everywhere I replace the notation :
[answerButtonsArrayWithURL objectAtIndex:rndValueForURLS];
with the new equivalent and IMO more readable:
answerButtonsArrayWithURL[rndValueForURLS];
I am trying to store a c-float array in an NSDictionary to use later.
I was initially using an NSArray to store the C-data but NSArray is to slow for my intentions.
I am using the following code to wrap the arrays in the NSDictionary:
[self.m_morphPositions setObject:[NSValue valueWithBytes:&positionBuff objCType:#encode(float[(self.m_countVertices * 3)])] forKey:fourCC];
And retrieving the C-Float array using:
float posMorphs[(self.m_countVertices*3)];
NSValue *posValues = [self.m_morphPositions objectForKey:name];
[posValues getValue:&posMorphs];
When I retireve the array, the values are all set to 0.0 for each index which is wrong.
How can I fix this?
I also think that NSData is probably the best solution here. But just if anybody is interested: You cannot use #encode with a variable sized array, because it is a compiler directive. Therefore
#encode(float[(self.m_countVertices * 3)])
cannot work. Actually the compiler creates the encoding for a float array of size zero here, which explains why you get nothing back when reading the NSValue.
But you can create the type encoding string at runtime. For a float array,
the encoding is [<count>^f] (see Type Encodings), so the following would work:
const char *enc = [[NSString stringWithFormat:#"[%d^f]", (self.m_countVertices * 3)] UTF8String];
NSValue *val = [NSValue valueWithBytes:positionBuff objCType:enc];
NSValue is probably intented for scalar values, not arrays for them. In this case, using NSData should be much easier
NSData* data = [NSData dataWithBytes:&positionBuff length:(self.m_countVertices * 3 * sizeof(float))];
[data getBytes:posMorphs length:(self.m_countVertices * 3 * sizeof(float))];
Another solution is to allocate the array on the heap and use NSValue to store the pointer.
You can feed the bytes into an NSDictionary if they're wrapped in an NSData.
If you want to skip that, you would use an NSMapTable and NSPointerFunctionsOpaqueMemory (or MallocMemory) for the value pointer functions.
I'm not sure if it is the way you are encoding your value, but it might help to encapsulate your array into a struct.
// Put this typedef in a header
typedef struct
{
float values[3];
} PosValues;
In your code:
// store to NSValue
PosValues p1 = { { 1.0, 2.0, 3.0 } };
NSValue *val = [NSValue valueWithBytes:&p1 objCType:#encode(PosValues)];
// retrieve from NSValue
PosValues p2;
[val getValue:&p2];
NSLog(#"%f, %f, %f", p2.values[0], p2.values[1]. p2.values[2]);
The benefit to this approach is that your array is kept as an array type. Also, these structures are assignable even though raw arrays are not:
PosValues p1 = { { 1.0, 2.0, 3.0 } };
PosValues p2;
p2 = p1;