簡體   English   中英

如何識別int代碼值屬於枚舉中的哪個常量

[英]How to identify which constant in an enum an int code value belongs to

更新1

+(PKMainWeatherType)getWeatherTypeWithWeatherId:(NSUInteger)weatherId{

    if(weatherId >= PKMainWeatherTypeAdditional) return PKMainWeatherTypeAdditional;
    if(weatherId >= PKMainWeatherTypeExtreme) return PKMainWeatherTypeExtreme;
    if(weatherId >= PKMainWeatherTypeClouds) return PKMainWeatherTypeClouds;
    if(weatherId >= PKMainWeatherTypeAtmosphere) return PKMainWeatherTypeAtmosphere;
    if(weatherId >= PKMainWeatherTypeSnow) return PKMainWeatherTypeSnow;
    if(weatherId >= PKMainWeatherTypeRain) return PKMainWeatherTypeRain;
    if(weatherId >= PKMainWeatherTypeDrizzle) return PKMainWeatherTypeDrizzle;
    if(weatherId >= PKMainWeatherTypeThunderstorm) return PKMainWeatherTypeThunderstorm;
    return PKMainWeatherTypeNone;
}

這是獲得適當常數的最佳方法嗎?


原始帖子

鑒於以下枚舉

typedef NS_ENUM(NSUInteger, PKMainWeatherType)
{
    PKMainWeatherTypeNotFound = 0,
    PKMainWeatherTypeThunderstorm = 200,
    PKMainWeatherTypeDrizzle = 300,
    PKMainWeatherTypeRain = 500,
    PKMainWeatherTypeSnow = 600,
    PKMainWeatherTypeAtmosphere = 700,
    PKMainWeatherTypeClouds = 800,
    PKMainWeatherTypeExtreme = 900,
    PKMainWeatherTypeAdditional = 951,
};

我如何確定哪個常數說代碼編號224屬於? 在這種情況下,它將屬於PKMainWeatherTypeThunderstorm

在這種情況下,我希望所有落入范圍內的數字都與其常數相關聯。 查看此鏈接以查看代碼值及其關聯。

我想的是flooring的INT碼值,但隨后因為最后兩個常量共享相同的900域名,這將產生最后一個恆定的問題,從而地板會導致上述951的所有代碼值在不斷的有地板這之前進行分類價值900。

誰能提供干凈的解決方案?

如果PKMainWeatherTypeAdditional是唯一不遵循此模式的PKMainWeatherTypeAdditional ,則只需要處理該特定情況,然后計算其余的情況即可。

嚴格提供了提供的鏈接中提供的代碼:

+ (PKMainWeatherType)getWeatherTypeWithWeatherId:(NSUInteger)weatherId {

    if (weatherId >= PKMainWeatherTypeAdditional) return PKMainWeatherTypeAdditional;

    return (PKMainWeatherType)(weatherId / 100 * 100);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM