我接管了一个项目,并使用枚举来标记API调用。
它可以通过对枚举值进行OR运算来过滤它们,这样以前的开发人员就可以进行一些位移。
typedef enum {
APICallTimeSeriesSearchTimeSeries = (1LL << 1),
APICallTimeSeriesGetTimeSeriesInfo = (1LL << 2),
APICallTimeSeriesGetSeriesInfo = (1LL << 3),
....
APICallFavouritesAddNewsFavourite = (1LL << 79),
APICallFavouritesRemoveNewsFavourite = (1LL << 80),
APICallFavouritesGetAllFavourites = (1LL << 81)
} APICallType;
然后我们可以做像
这样的事情filter(APICallTimeSeriesGetTimeSeriesInfo | APICallTimeSeriesGetSeriesInfo | APICallTimeSeriesGetSeriesData);
但是最近我注意到类似的枚举映射到相同的值。 我倾倒了整个枚举列表
NSLog(@"%d",APICallTimeSeriesSearchTimeSeries);
NSLog(@"%d",APICallTimeSeriesGetTimeSeriesInfo);
NSLog(@"%d",APICallTimeSeriesGetSeriesInfo);
NSLog(@"%d",APICallTimeSeriesGetSeriesData);
NSLog(@"%d",APICallFleetSearchVessels);
NSLog(@"%d",APICallFleetGetVesselInfo);
etc.
我们可以看到行动中的位移
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184
34359738368
68719476736
问题是在(1LL <&lt; 63)之后生成相同的值
1152921504606846976 APICallFavouritesAddMarketFavourite = (1LL << 60)
2305843009213693952 APICallFavouritesRemoveMarketFavourite = (1LL << 61)
4611686018427387904 APICallFavouritesGetFutureFavourites = (1LL << 62)
-9223372036854775808 APICallFavouritesAddFutureFavourite = (1LL << 63)
-9223372036854775808 APICallFavouritesRemoveFutureFavourite = (1LL << 64)
-9223372036854775808 APICallFavouritesRemoveVesselFavourite = (1LL << 65)
所有新枚举重复等相同值(-9223372036854775808)
来自这个帖子 Types in objective-c on iPhone
因为-9223372036854775808是LLONG_MIN
NSLog(@"LLONG_MIN: %lli", LLONG_MIN); // signed long long int
我注意到有更高的价值
NSLog(@"LLONG_MAX: %lli", LLONG_MAX);
NSLog(@"ULLONG_MAX: %llu", ULLONG_MAX); // unsigned long long int
LLONG_MIN: -9223372036854775808
LLONG_MAX: 9223372036854775807
ULLONG_MAX: 18446744073709551615
但无法找到ULLONG_MIN
我们如何解决这个问题?
我尝试将原始存储从1LL更改为1ULL但没有效果。
FROM:
typedef enum {
APICallTimeSeriesSearchTimeSeries = (1LL << 1),
APICallTimeSeriesGetTimeSeriesInfo = (1LL << 2),
APICallTimeSeriesGetSeriesInfo = (1LL << 3),
...
TO:
typedef enum {
APICallTimeSeriesSearchTimeSeries = (1ULL << 1),
APICallTimeSeriesGetTimeSeriesInfo = (1ULL << 2),
APICallTimeSeriesGetSeriesInfo = (1ULL << 3),
...
欢呼声
答案 0 :(得分:1)
ULLONG_MIN为0. ULLONG表示无符号 long long,即由64位表示的正值。
如果您想要正数和负数,则只能表示可以用相同位宽的无符号类型表示的那组值的一半。这就是ULLONG_MAX更大的原因。
答案 1 :(得分:0)
我选择将APICallType拆分为另一个枚举。就是这个或重新连接一个3岁的项目,并且必须测试每个API调用。
APICallTimeSeriesSearchTimeSeries = (1LL << 1),
APICallTimeSeriesGetTimeSeriesInfo = (1LL << 2),
APICallTimeSeriesGetSeriesInfo = (1LL << 3),
....
APICallFavouritesAddNewsFavourite = (1LL << 79),
APICallFavouritesRemoveNewsFavourite = (1LL << 80),
APICallFavouritesGetAllFavourites = (1LL << 81)
} APICallType;
已改为
typedef enum {
APICallTimeSeriesSearchTimeSeries = (1LL << 1),
APICallTimeSeriesGetTimeSeriesInfo = (1LL << 2),
APICallTimeSeriesGetSeriesInfo = (1LL << 3),
APICallTimeSeriesGetSeriesData = (1LL << 4),
APICallFleetSearchVessels = (1LL << 5),
APICallFleetGetVesselInfo = (1LL << 6),
APICallNewsGetNewsItems = (1LL << 7),
APICallNewsGetPublicationItems = (1LL << 8),
APICallPodcastGetPodcastItems = (1LL << 9),
APICallUserLoginAuthenticateUser = (1LL << 10),
APICallFFASearchFFARoutes = (1LL << 11),
APICallFFAGetRouteData = (1LL << 12),
APICallOwnerSearchOwners = (1LL << 13),
APICallOwnerGetOwnerFleet = (1LL << 14),
APICallIndicesSearchIndices = (1LL << 15),
APICallIndicesGetIndicesData = (1LL << 16),
APICallEquitiesGetEquitiesData = (1LL << 17),
APICallEquitiesGetEquityPreviews = (1LL << 18),
APICallEquitiesSearchBrokingStocks = (1LL << 19),
APICallEquitiesSearchIndices = (1LL << 20),
APICallEquitiesSearchCurrencies = (1LL << 21),
APICallEquitiesGetRelatedNewsItems = (1LL << 22),
APICallEquitiesGetCurrencyList = (1LL << 23),
APICallSandPVesselsSearch = (1LL << 24),
APICallSandPVesselSalesSearch = (1LL << 25),
APICallSandPGetCommercialData = (1LL << 26),
APICallSandPGetSimilarSales = (1LL << 27),
APICallSandPGetFullVesselDetails = (1LL << 28),
APICallSandPEnquiriesSearch = (1LL << 29),
APICallTimeSeriesGetCategories = (1LL << 30),
APICallTimeSeriesGetShipTypes = (1LL << 31),
APICallTimeSeriesGetDataTypes = (1LL << 32),
APICallTimeSeriesSearchTimeSeriesWithCategories = (1LL << 33),
APICallHomeGetComments = (1LL << 34),
APICallEquitiesGetRelatedNewsItemsWithIds = (1LL << 35),
APICallEquitiesSearchEquitiesBrokingStocks = (1LL << 36),
APICallTimeSeriesGetFutureShipTypes = (1LL << 37),
APICallTimeSeriesGetFutureDataTypes = (1LL << 38),
APICallTimeSeriesGetOHLCData = (1LL << 39),
APICallTimeSeriesGetSeriesDataMaxPoints = (1LL << 40),
APICallTimeSeriesGetSeriesOHLCDataMaxPoints = (1LL << 41),
APICallClientSearchClients = (1LL << 42),
APICallClientGetClientListForBroker = (1LL << 43),
APICallClientSearchDivisionsAndDesks = (1LL << 44),
APICallClientGetDivisionsAndDesks = (1LL << 45),
APICallClientSaveEmployeeDetails = (1LL << 46),
APICallTimeSeriesSearchWithSeriesId = (1LL << 47),
APICallHomeGetCommentsCargo = (1LL << 48),
APICallHomeGetCommentsSandP = (1LL << 49),
APICallHomeGetCommentsTanker = (1LL << 50),
APICallClientGetSandPRoutes = (1LL << 51),
APICallClientGetSandP2ndHandValues = (1LL << 52),
APICallHomeGetCommentsSpecialised = (1LL << 53),
APICallClientGetSpecialised = (1LL << 54),
APICallClientGetGas = (1LL << 55),
APICallClientGetGasBunkerPrices = (1LL << 56),
APICallFFAGetAllWhiteboardSpreadTypes = (1LL << 57),
APICallFFAGetWhiteboardSpreadPricesForType = (1LL << 58),
APICallFavourites = (1LL << 59)
/*
APICallFavouritesGetMarketFavourites = (1LL << 59),
APICallFavouritesAddMarketFavourite = (1LL << 60),
APICallFavouritesRemoveMarketFavourite = (1LL << 61),
APICallFavouritesGetFutureFavourites = (1LL << 62),
//Enum bit shifted greater than 63 will have same value as MAX INT reached
//moved these to
APICallFavouritesAddFutureFavourite = (1LL << 63),
APICallFavouritesRemoveFutureFavourite = (1LL << 64),
APICallFavouritesGetVesselOwnerFavourites = (1LL << 65),
APICallFavouritesGetVesselFavourites = (1LL << 66),
APICallFavouritesAddVesselFavourite = (1LL << 67),
APICallFavouritesRemoveVesselFavourite = (1LL << 68),
APICallFavouritesGetOwnerFavourites = (1LL << 69),
APICallFavouritesAddOwnerFavourite = (1LL << 70),
APICallFavouritesRemoveOwnerFavourite = (1LL << 71),
APICallFavouritesGetShippingStockFavourites = (1LL << 72),
APICallFavouritesAddShippingStockFavourite = (1LL << 73),
APICallFavouritesRemoveShippingStockFavourite = (1LL << 74),
APICallFavouritesGetIndicieFavourites = (1LL << 75),
APICallFavouritesAddIndicieFavourite = (1LL << 76),
APICallFavouritesRemoveIndicieFavourite = (1LL << 77),
APICallFavouritesGetNewsFavourites = (1LL << 78),
APICallFavouritesAddNewsFavourite = (1LL << 79),
APICallFavouritesRemoveNewsFavourite = (1LL << 80),
APICallFavouritesGetAllFavourites = (1LL << 81)
*/
} APICallType;
AND ADDED APICallSubType
typedef enum {
/* for use with APICallType:APICallFavourites */
APICallFavouritesGetMarketFavourites = (1LL << 1),
APICallFavouritesAddMarketFavourite = (1LL << 2),
APICallFavouritesRemoveMarketFavourite = (1LL << 3),
APICallFavouritesGetFutureFavourites = (1LL << 4),
APICallFavouritesAddFutureFavourite = (1LL << 5),
APICallFavouritesRemoveFutureFavourite = (1LL << 6),
APICallFavouritesGetVesselOwnerFavourites = (1LL << 7),
APICallFavouritesGetVesselFavourites = (1LL << 8),
APICallFavouritesAddVesselFavourite = (1LL << 9),
APICallFavouritesRemoveVesselFavourite = (1LL << 10),
APICallFavouritesGetOwnerFavourites = (1LL << 11),
APICallFavouritesAddOwnerFavourite = (1LL << 12),
APICallFavouritesRemoveOwnerFavourite = (1LL << 13),
APICallFavouritesGetShippingStockFavourites = (1LL << 14),
APICallFavouritesAddShippingStockFavourite = (1LL << 15),
APICallFavouritesRemoveShippingStockFavourite = (1LL << 16),
APICallFavouritesGetIndicieFavourites = (1LL << 17),
APICallFavouritesAddIndicieFavourite = (1LL << 18),
APICallFavouritesRemoveIndicieFavourite = (1LL << 19),
APICallFavouritesGetNewsFavourites = (1LL << 20),
APICallFavouritesAddNewsFavourite = (1LL << 21),
APICallFavouritesRemoveNewsFavourite = (1LL << 22),
APICallFavouritesGetAllFavourites = (1LL << 23)
//do not go over (1LL << 63) - enum values start at MAX INT after that
} APICallSubType;
在任何地方我检查了我必须检查子类型
之后的类型filter(APICallFavourites);
filterSubType(APICallFavouritesAddVesselFavourite |
APICallFavouritesRemoveVesselFavourite);
if (responseOfSubType(APICallFavouritesAddVesselFavourite))
{
NSLog(@"%s APICallFavouritesAddVesselFavourite response not handled", __PRETTY_FUNCTION__);
}
if (responseOfSubType(APICallFavouritesRemoveVesselFavourite))
{
NSLog(@"%s APICallFavouritesRemoveVesselFavourite response not handled", __PRETTY_FUNCTION__);
}