简体   繁体   English

正确将32位方法更新为64位支持

[英]Proper updating of a 32bit method to 64bit support

I have a method I am trying to update to 64-bit support for my app. 我有一种方法正在尝试更新对我的应用程序的64位支持。 I have lots of these warnings . 我有很多这样的warnings

The issue is with the below example. 问题出在以下示例中。 I get the warning: 我得到警告:

Implicit conversion loses integer precision NSInteger to int 隐式转换将整数精度NSInteger转换为int

- (int)  aMethod {
    NSUserDefaults *u = [NSUserDefaults standardUserDefaults];
    return [u integerForKey:@"someKey"];
}

If I change the return type to NSInteger I get 如果将返回类型更改为NSInteger我会得到

Conflicting return type in implementation of aMethod int vs NSInteger aka long aMethod int与NSInteger的实现中冲突的返回类型

-(NSInteger)  aMethod {
    NSUserDefaults *u = [NSUserDefaults standardUserDefaults];
    return [u integerForKey:@"someKey"];
}

I have tried casting the return type 我试过转换返回类型

return (NSInteger)[u integerForKey:@"someKey"];

or 要么

return (long)[u integerForKey:@"someKey"];

But I can't get the error to go away unless I cast it to (int) 但是除非将其强制转换为(int),否则我无法消除错误

return (int)[u integerForKey:@"someKey"];

I have read many SO questions but I can't seem to get a good answer. 我已经阅读了很多SO问题,但是我似乎找不到很好的答案。

What is the proper way of updating this method? 更新此方法的正确方法是什么?

The method: 方法:

- (int) aMethod ...

returns a value of type int . 返回int类型的值。 The method may first declared, probably in a header ( .h ) file, and then defined in an implementation ( .m ) file. 该方法可能首先在标头( .h )文件中声明,然后在实现( .m )文件中定义。

The statement: 该声明:

return [u integerForKey:@"someKey"];

returns the result of calling integerForKey: , which is a value of type NSInteger . 返回调用integerForKey:的结果,它是NSInteger类型的值。

NSInteger is not a type defined by the (Objective-)C language , rather it is a type alias ( typedef ) defined in the frameworks intended to represent the "natural" integer size on the platform. NSInteger不是(Objective-)C 语言定义的类型,而是在框架中定义的类型别名typedef ),用于表示平台上的“自然”整数大小。 It is an alias for int on 32-bit platforms and long on 64-bit. 它是一个别名int在32位的平台和long 64位。

So your compiler error is telling you that the (now) 64-bit value is being truncated to fit a 32-bit result. 因此,编译器错误告诉您(现在)64位值将被截断以适合32位结果。 There is no single right way to fix this, it depends on what the use of the value associated with @someKey is: 没有解决此问题的正确方法,这取决于与@someKey关联的值的@someKey是:

  • If the value should now be 64-bit then change the return type of aMethod ; 如果该值现在应为64位,则更改aMethod的返回类型; aMethod ,返回aMethod both in its separate declaration, if it has one, and in its implementation; 在单独的声明中(如果有的话)以及在执行中; to NSInteger . NSInteger You will also need to check that wherever the method is called that returning an NSInteger value is acceptable, and you may need to make follow-on changes to variable types etc. You also need to look at places where the value associated with the key is set. 您还需要检查在调用该方法的任何地方返回NSInteger值都是可接受的,并且可能需要对变量类型等进行后续更改。您还需要查看与键关联的值在哪里组。
  • If the value associated with the key may remain as 32-bit then you can use intForKey: instead of integerForKey and leave the return type as is. 如果与键关联的值可能仍为32位,则可以使用intForKey:而不是integerForKey并将返回类型保持原样。 You should also change where the key is set to store an int 您还应该更改将键设置为存储int

HTH HTH

Correction 更正

There is (no longer?) a convenience method intForKey: on NSUserDefaults . intForKey:上有一个(不再是)便捷方法intForKey: NSUserDefaults Alternatives include: integerForKey: and casting; 备选方案包括: integerForKey:和强制转换; objectForKey: to obtain an NSNumber reference and then intValue ; objectForKey:获取NSNumber引用,然后获取intValue etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM