Created
January 21, 2014 09:21
-
-
Save upkarlidder/8536914 to your computer and use it in GitHub Desktop.
Use enum in Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
enum TEMPERATURE_TYPE{ | |
CELCIUS, | |
FAHRENHEIT, | |
}; | |
@interface SFTemperatureModel : NSObject | |
@property (readwrite, assign) float value; | |
@property (nonatomic, assign) enum TEMPERATURE_TYPE temperatureType; | |
- (SFTemperatureModel*) initWithTemperature:(enum TEMPERATURE_TYPE)tempType value:(float)valueIn; | |
- (SFTemperatureModel*) convertTemperature:(enum TEMPERATURE_TYPE)to; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SFTemperatureModel.h" | |
@implementation SFTemperatureModel | |
- (SFTemperatureModel*) initWithTemperature:(enum TEMPERATURE_TYPE)tempType value:(float)valueIn{ | |
self = [super init]; | |
if(self) | |
{ | |
self.temperatureType = tempType; | |
self.value = valueIn; | |
} | |
return self; | |
} | |
- (SFTemperatureModel*) convertTemperature:(enum TEMPERATURE_TYPE)to{ | |
enum TEMPERATURE_TYPE from = self.temperatureType; | |
float valueIn = self.value; | |
SFTemperatureModel *result = [[SFTemperatureModel alloc]initWithTemperature:from value:valueIn]; | |
switch (from) { | |
case CELCIUS: | |
{ | |
if(to == FAHRENHEIT){ | |
float out = ((valueIn*9)/5)+32; | |
result.value = ceil(2.0f*out)/2.0f; | |
result.temperatureType = FAHRENHEIT; | |
} | |
break; | |
} | |
case FAHRENHEIT: | |
{ | |
if(to == CELCIUS){ | |
float out = ((valueIn - 32))*5/9; | |
result.value = ceil(2.0f*out)/2.0f; | |
result.temperatureType = CELCIUS; | |
} | |
break; | |
} | |
default: | |
break; | |
} | |
return result; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment