-2

I noticed that in Swift you can pass multiple values into a CGFloat thing that is not possible in Objective c.

Here's an example: let array: [CGFloat] = [0.0, 0.0, 0.0, 0.0]

I need to get multiple values for a NSLayoutConstraint.constant, can you tell me if there is an equivalent function in Objective C? sorry the stupid question, I hope to have explained to you well

I do not know ... maybe using a NSMutableArray

kAiN
  • 2,559
  • 1
  • 26
  • 54
  • It depends on what you actually need to do with `array` after. Often the O-C way is to create either an array or mutable array and fill it with `NSNumber` objects, using `+ (NSNumber *)numberWithFloat:(float)value;`. – Phillip Mills Sep 27 '17 at 17:45
  • @PhillipMills I have a collectionView with a Custom cell. In my custom cell I have a constraint that will have to be modified several times in the collectionView by calling cell.barHeight.constant = [ARRAY objectAtIndex: indexPath.item]; the problem is that barHeight.constant obviously does not accept an NSArray – kAiN Sep 27 '17 at 17:47
  • If you use my suggestion, the objectAtIndex is a NSNumber. You would get it the way you describe and then use `floatValue` for the actual constant. – Phillip Mills Sep 27 '17 at 17:54
  • @PhillipMills Can I ask you kindly if you can give me a sample code to better understand the correct setting of your suggestion? – kAiN Sep 27 '17 at 17:56
  • Swift does *not* allow the use of an array of floats where a single one is required, there is no "multiple values into a CGFloat thing". Objective-C supports both arrays of float values and arrays of `NSNumber` objects, but the array kinds are different. The code in your comment `cell.barHeight.constant = [ARRAY objectAtIndex: indexPath.item]` is valid if `ARRAY` is an array of `T` and `constant` is of type `T`. You need to clearer about what you are trying to do and what the error you are getting is. – CRD Sep 27 '17 at 17:58
  • @CRD this is my error Assigning to 'CGFloat' (aka 'double') from incompatible type 'id' when cell.barHeight.constant = [ARRAY objectAtIndex: indexPath.item] – kAiN Sep 27 '17 at 18:02
  • With that error the best **guess** would be the array contains `NSNumber` *objects* and you need to call `doubleValue` to obtain a *value* you can assign to your property. However unless you edit your question and show how you create and use your array this is only a guess. – CRD Sep 27 '17 at 19:00

2 Answers2

1

Code example to show saving float values in an Objective-C NSArray object:

NSArray *numbers = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:2.0], [NSNumber numberWithFloat:7.0], nil];
for (int i = 0; i < [numbers count]; i++) {
    NSNumber *next = [numbers objectAtIndex:i];
    NSLog(@"Next number: %f", [next floatValue]);
}
Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • Thanks a lot you have been very kind, I tried your suggestion but this sets the value for barHeight.constant for each cell ... instead I needed each cell to match the exact value .. example we have created an array with three NSNumber elements the first value NSNumber 0.0 should match the barHeight.constant of the first cell the second NSNumber 2.0 value should match the barHeight.constant of the second cell – kAiN Sep 27 '17 at 18:15
  • So, why is that a problem? Can't you simply replace the values I used with the ones you want? – Phillip Mills Sep 27 '17 at 18:22
  • This is not a problem ... The problem that occurs is that barHeight.constant only keeps the last NSNumber of the array not all. When I launch my app the log returns that barHeight.constant = 7.0 in fact the last NSNumber in our arrau has a value [NSNumber numberWithFloat: 7.0] .. It does not even store the other two. Each cell has a barHeight.constant = 7.0 this is the problem – kAiN Sep 27 '17 at 18:30
  • It may be a problem but it's also the definition of what the **constant** means. You originally asked about putting float values into an array...now I'm not sure what the question has changed into. Are you trying to redefine how constraints work? – Phillip Mills Sep 27 '17 at 20:09
1

You can define your constants in an NSArray like this:

self.constants = @[ @8.0, @8.0, @5.0 ];

Then later on your will need to retrieve a value like this

cell.barHeight.constant = [self.constants[indexPath.row] doubleValue];
Paul.s
  • 38,494
  • 5
  • 70
  • 88