我正在尝试将Swift函数转换为Objective-C。这是函数的签名
Swift:
func hammingWeight(_ n: Int) -> IntObjective-C
-(int) hammingWeight:(int) number我将这个值传递给function/method:
00000000000000000000000000001011在Swift的情况下,如果我打印n的值,它将打印以下内容:
p n
(Int) $R2 = 1011在Objective-C的情况下,打印如下:
p number
(int) $0 = 521我的问题是,为什么Objective-C改变了我通过的值。这对我来说毫无意义。你们中有谁知道为什么会发生这种情况,或者是否有办法绕过这一点?
我将非常感谢你在这方面的帮助。
这是完整的Objective-C实现:
@interface DoSomething : NSObject
@end
@implementation DoSomething
-(int) hammingWeight:(int) number {
NSLog(@"number %d", number);
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
DoSomething *doIt = [DoSomething new];
[doIt hammingWeight:00000000000000000000000000001011];
}
}发布于 2020-07-24 07:48:51
不同之处在于,在(Objective-)c中,由于前缀0,您给定的数字被解释为八进制,而在Swift中,它被解释为十进制。
对于Swift前缀0o中的八进制Int文本。
有关Swift documentation中的八进制整数文字的信息
c documentation中有关八进制整型数的信息
// Swift:
(lldb) po 0o0000000000000000000000000001011;
521
(lldb) po 00000000000000000000000000001011;
1011
// Objective-C
(lldb) po 1011
1011
(lldb) po 01011
521https://stackoverflow.com/questions/63064372
复制相似问题