0

I need a float type array; don't know the size of array initially. When I will get the value of size, I want to add some float values in it by a for loop. This array need to be global because I want to update it from another view controller class. How can I do this?

For this I declared a NSMutableArray type object and add float value by a for loop.

@property (nonatomic, strong) NSMutableArray   *speedRate;

for (NSInteger i = 0; i < self.assetArray.count; i++){
   [self.speedRate addObject:[NSNumber numberWithFloat:1.0f]];
    }

NSLog(@"speedRate.count =%lu",(unsigned long)self.speedRate.count);
NSLog(@"value =%f",[[self.speedRate objectAtIndex:1]floatValue]);

But I got speedRate.count =0 and value =0.000 . I want speedRate.count will be same as self.assetArray.count and value =1.0 . How can I achieve this?

1 Answer 1

1

Your code never actually allocates the array. Add

self.speedRate = [[NSMutableArray alloc] init];

before the loop.

Sign up to request clarification or add additional context in comments.

3 Comments

I am greatful for your response. It works but I am facing another problem now. I am trying to update self.speedRate like "self.speedRate[position] = rate;" . But getting an error like " Sending 'float' to parameter of incompatible type 'id _Nonnull' " . Can you help me about this?
Please ask this as a separate question
If rate is a float, it is not an object. An NSArray can only contain objects. Try @(rate) which will make it an NSNumber object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.