objective-c 为什么我不用 * 声明 NSInteger

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1014053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:44:40  来源:igfitidea点击:

Why don't I declare NSInteger with a *

objective-ccocoapointersintegernsinteger

提问by gargantuan

I'm trying my hand at the iPhone course from Stanford on iTunes U and I'm a bit confused about pointers. In the first assignment, I tried doing something like this

我正在尝试在 iTunes U 上斯坦福大学的 iPhone 课程,但我对指针有点困惑。在第一个作业中,我尝试做这样的事情

NSString *processName = [[NSProcessInfo processInfo] processName];
NSInteger *processID = [[NSProcessInfo processInfo] processIdentifier];

Which generated an error, after tinkeing around blindly, I discovered that it was the * in the NSInteger line that was causing the problem.

这产生了一个错误,在盲目地修补之后,我发现是 NSInteger 行中的 * 导致了问题。

So I obviously don't understand what's happening. I'll explain how I think it works and perhaps someone would be kind enough to point out the flaw.

所以我显然不明白发生了什么。我会解释我认为它是如何工作的,也许有人会友好地指出这个缺陷。

Unlike in web development, I now need to worry about memory, well, more so than in web development. So when I create a variable, it gets allocated a bit of memory somewhere (RAM I assume). Instead of passing the variable around, I pass a pointer to that bit of memory around. And pointers are declared by prefixing the variable name with *.

与 web 开发不同,我现在需要担心内存,嗯,比 web 开发更重要。因此,当我创建一个变量时,它会在某处分配一些内存(我假设为 RAM)。我没有传递变量,而是传递一个指向该位内存的指针。指针是通过在变量名前加上 * 来声明的。

Assuming I'm right, what puzzles me is why don't I need to do that for NSInteger?

假设我是对的,让我感到困惑的是为什么我不需要为 NSInteger 这样做?

回答by Dan Lorenc

NSIntegeris a primitive type, which means it can be stored locally on the stack. You don't need to use a pointer to access it, but you can if you want to. The line:

NSInteger是原始类型,这意味着它可以本地存储在堆栈上。您不需要使用指针来访问它,但如果您愿意,也可以使用。线路:

NSInteger *processID = [[NSProcessInfo processInfo] processIdentifier];

returns an actual variable, not its address. To fix this, you need to remove the *:

返回一个实际变量,而不是它的地址。要解决此问题,您需要删除*

NSInteger processID = [[NSProcessInfo processInfo] processIdentifier];

You can have a pointer to an NSIntegerif you really want one:

NSInteger如果你真的想要一个,你可以有一个指向 an 的指针:

NSInteger *pointerToProcessID = &processID;

The ampersand is the address of operator. It sets the pointer to the NSIntegerequal to the address of the variable in memory, rather than to the integer in the variable.

与号是操作员的地址。它将指针设置为NSInteger等于内存中变量的地址,而不是变量中的整数。

回答by Alex Rozanski

The reason that you don't declare NSIntegerwith a *is because it isn't an object. An NSInteger is simply an intor a long:

NSInteger使用 a声明的原因*是因为它不是对象。NSInteger 只是一个int或一个long

#if __LP64__
typedef long NSInteger;
#else
typedef int NSInteger;
endif

If it's being used in a 32-bit application, it's a 32-bit integer, and if it's being built in a 64-bit application, it's a 64-bit integer.

如果它在 32 位应用程序中使用,它是一个 32 位整数,如果它被构建在 64 位应用程序中,它是一个 64 位整数。

Of course, you canpass an NSIntegeras a pointer, but most functions simply take arguments as an NSIntegerand not a pointer to it.

当然,您可以将 anNSInteger作为指针传递,但大多数函数只是将参数作为 anNSInteger而不是指向它的指针。

Objects, on the other hand, can only be passed to other functions as pointers. This is because objects have memory dynamically allocated for them, and so cannot be declared on the stack. Since an intor longhas a fixed amount of memory allocated for them, this is not an issue.

另一方面,对象只能作为指针传递给其他函数。这是因为对象具有为它们动态分配的内存,因此不能在堆栈上声明。由于intorlong为它们分配了固定数量的内存,因此这不是问题。

回答by Peter Hosey

The *means “pointer”. The object variable holds a pointer to an object, so it has a *; the NSInteger variable holds an NSInteger, not a pointer to an NSInteger, so it does not have a *. Putting the *on that variable gives you at least a warning because you're putting an integer into a pointer variable.

*手段“指针”。object 变量持有一个指向对象的指针,因此它有一个*; NSInteger 变量保存一个 NSInteger,而不是一个指向 NSInteger 的指针,所以它没有*. 将*放在该变量上至少会给您一个警告,因为您将整数放入指针变量中。

回答by Andrei Tanasescu

NSInteger is just a typedef for int, AFAIK.

NSInteger 只是 int,AFAIK 的 typedef。

回答by yoAlex5

Working with pointers

使用指针

NSInteger integer1 = 1;
NSLog(@"1. integer1:%ld &integer1:%p", integer1, &integer1);
//1. integer1:1 &integer1:0x7ffee59e8a98

NSInteger *integer2 = &integer1;
NSLog(@"2. integer2:%p &integer2:%p *integer2:%ld", integer2, &integer2, *integer2);
//2. integer2:0x7ffee59e8a98 &integer2:0x7ffee59e8a90 *integer2:1

*integer2 = 2;
NSLog(@"3. integer2:%p &integer2:%p *integer2:%ld \t integer1:%ld &integer1:%p", integer2, &integer2, *integer2, integer1, &integer1);
//3. integer2:0x7ffee59e8a98 &integer2:0x7ffee59e8a90 *integer2:2    integer1:2 &integer1:0x7ffee59e8a98