gpt4 book ai didi

ios - iPhone 上的负值数据使用情况?

转载 作者:行者123 更新时间:2023-11-28 19:56:51 25 4
gpt4 key购买 nike

我正在使用以下方法检查我的用户数据消耗,该方法取自 here .它在大多数时间都运行良好,但对于某些用户来说,它会返回一个负值。换句话说,WWANReceived 是负数。

怎么可能呢?有解决办法吗?

+ (NSArray *)getDataCounters {

BOOL success;
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
const struct if_data *networkStatisc;

int WiFiSent = 0;
int WiFiReceived = 0;
int WWANSent = 0;
int WWANReceived = 0;

NSString *name = [[NSString alloc]init];

// getifmaddrs
success = getifaddrs(&addrs) == 0;
if (success)
{
cursor = addrs;
while (cursor != NULL)
{
name = [NSString stringWithFormat:@"%s",cursor->ifa_name];
// names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN

if (cursor->ifa_addr->sa_family == AF_LINK)
{
if ([name hasPrefix:@"en"])
{
networkStatisc = (const struct if_data *) cursor->ifa_data;
WiFiSent += networkStatisc->ifi_obytes;
WiFiReceived += networkStatisc->ifi_ibytes;
}

if ([name hasPrefix:@"pdp_ip"])
{
networkStatisc = (const struct if_data *) cursor->ifa_data;
WWANSent += networkStatisc->ifi_obytes;
WWANReceived += networkStatisc->ifi_ibytes;
}
}

cursor = cursor->ifa_next;
}

freeifaddrs(addrs);
}

return [NSArray arrayWithObjects:[NSNumber numberWithInt:WiFiSent], [NSNumber numberWithInt:WiFiReceived],[NSNumber numberWithInt:WWANSent],[NSNumber numberWithInt:WWANReceived], nil];
}

最佳答案

也许您已经解决了这个问题,我只是在测试同一个示例并遇到了这个问题。

您必须将类型从 int 更改为 long long 才能处理更大的值。您看到的是 2GB 流量后发生的整数溢出。

关于ios - iPhone 上的负值数据使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26037378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com