Jonathan Thurft - 4 months ago 24

Objective-C Question

I am quite new to objective-c.

I am trying to create a function that takes inches or centimetres and transform the values into the oposite metric system.

I am currently struggling on how to do a mathematical equation in objective-c given that I do not know how to set the variables accordingly. As I always get some error from XCode saying that the variable is an int but shouldn't be or the pointer is wrong and other things... basicaly... i dont know how to make math...

The intention is that I pass the string

`5’11”`

`1.80 cm`

The code below attempts to do pass inches to cm.... if we pass 5’5” the mathematical equation should be

`((5 * 12) + 3) * 2.54`

`180.34`

`1.80 cm`

The following code is a far as I got but it just not working for me. if anyone could please tell me how to do this operation in my code I would appreciate it.

`- (NSString *)returnRightMetric:(NSString *)theMeasure typeOfMetricUsed:(NSString *)metricType`

{

if ([metricType isEqualToString:@"inches"]) {

NSArray* theConvertion = [theMeasure componentsSeparatedByCharactersInSet:

[NSCharacterSet characterSetWithCharactersInString:@"’”"]];

NSInteger* value1 = [theConvertion[0] intValue];

NSInteger* value2 = [theConvertion[1] intValue];

float *number = ((value1 * 12) + value2) * 2.54;

///.....

} else if ([metricType isEqualToString:@"cm"])

{

}

return result;

}

Answer

```
NSArray* theConvertion = [theMeasure componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"’”"]];
int value1 = [theConvertion[0] intValue];
int value2 = [theConvertion[1] intValue];
float number = ((value1 * 12) + value2) * 2.54;
NSString *formattedNumber = [NSString stringWithFormat:@"%.02f", (number/100)];
```

So in short you need to know the difference between native/primitive type's and reference types (type that use pointers this character --> *. This might help Link