Int 或 NSInteger 作为方法参数的对象。 Objective-C

发布于 2024-08-26 21:16:35 字数 413 浏览 10 评论 0 原文

我在传递数字作为方法的参数时遇到了一些麻烦:

- (void)meth2:(int)next_int;

要调用该方法,我需要这个:

int next_int = 1;
[self performSelectorOnMainThread:@selector(meth2:) withObject:next_int waitUntilDone:NO];
//update next_int and call meth2 again

此时,我收到一个“来自整数的指针,没有强制转换”错误,并且与 NSIntegerNSNumber 没有用,因为它是不可变的,而且我需要不断更改该值。 知道我该怎么做吗?

谢谢。

I'm having some trouble passing a number as an argument for a method:

- (void)meth2:(int)next_int;

And to call that method I need this:

int next_int = 1;
[self performSelectorOnMainThread:@selector(meth2:) withObject:next_int waitUntilDone:NO];
//update next_int and call meth2 again

at this point, I get a "pointer from integer without a cast" error and would happen the same with a NSInteger. An NSNumber is not useful because it's immutable and I need to change the value constantly.
Any idea how can I do this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑,眼淚并存 2024-09-02 21:16:35

如果您只是尝试调用该方法,则可以使用标准语法:

[self meth2:next_int];

如果您确实需要使用 performSelectorOnMainThread:,您可以将数字包装在 NSNumber 中的电话。你说你不能这样做,因为你需要更改数字,但你可以直接拉出一个 int 并更改它:

[self performSelectorOnMainThread:@selector(meth2:) withObject:[NSNumber numberWithInt:next_int] waitUntilDone:NO];
// ... later ...
- (void)meth2:(NSNumber *)number {
  int myInt = [number intValue];
  // do stuff with myInt
}

但也许你的意思是你想要获取数字的值作为调用 < 的输出代码>meth2。如果这就是你的意思,那么你可以传入一个双指针,这样你就可以接收一个新的对象:

- (void)meth2:(NSNumber **)number {
  int myInt = [*number intValue];
  // do stuff with myInt
  *number = [NSNumber numberWithInt:myInt];
}
// The caller can now operate like this:
NSNumber *number = [NSNumber numberWithInt:next_int];
[self performSelectorOnMainThread:@selector(meth2:) withObject:&number waitUntilDone:YES];
int returnInt = [*number intValue];

当然,这并不是真正的线程安全,所以如果你正在使用多个线程做事情,我建议使用 < code>@synchronized 关键字用于访问多线程访问的变量,或设置原子属性(即未声明为非原子的属性)。

另外,冰毒对你不好!哈哈

If you're just trying to call the method, you could use the standard syntax:

[self meth2:next_int];

If you really need to use the performSelectorOnMainThread: you could wrap the number in an NSNumber for the call. You say you can't do this because you need to change the number, but you can just pull an int out and change that:

[self performSelectorOnMainThread:@selector(meth2:) withObject:[NSNumber numberWithInt:next_int] waitUntilDone:NO];
// ... later ...
- (void)meth2:(NSNumber *)number {
  int myInt = [number intValue];
  // do stuff with myInt
}

But maybe you mean that you want to get the value of the number as an output from your call to meth2. If that's what you mean, then you could pass in a double pointer so you can receive a new object back:

- (void)meth2:(NSNumber **)number {
  int myInt = [*number intValue];
  // do stuff with myInt
  *number = [NSNumber numberWithInt:myInt];
}
// The caller can now operate like this:
NSNumber *number = [NSNumber numberWithInt:next_int];
[self performSelectorOnMainThread:@selector(meth2:) withObject:&number waitUntilDone:YES];
int returnInt = [*number intValue];

Of course, that's not really thread-safe, so if you're doing stuff with multiple threads, I would advise using the @synchronized keyword for access to multi-thread-accessed variables, or setting up atomic properties (i.e. properties not declared as nonatomic).

Also, meth is bad for you!! haha

ら栖息 2024-09-02 21:16:35

在传递之前将整数包装在 NSNumber 中:

int next_int = 1
NSNumber *theNumber = [NSNumber numberWithInt:next_int];
[self performSelectorOnMainThread:@selector(meth2:) withObject:theNumber waitUntilDone:NO];

然后您的 -meth2: 方法可能如下所示:

- (void)meth2:(NSNumber*)theNumber
{
    int next_int = [theNumber intValue];
    // do whatever
}

Wrap the integer in an NSNumber before passing it:

int next_int = 1
NSNumber *theNumber = [NSNumber numberWithInt:next_int];
[self performSelectorOnMainThread:@selector(meth2:) withObject:theNumber waitUntilDone:NO];

Then your -meth2: method could look something like this:

- (void)meth2:(NSNumber*)theNumber
{
    int next_int = [theNumber intValue];
    // do whatever
}
深巷少女 2024-09-02 21:16:35

这有点像黑客,但这在 ARC 下有效:

int next_int = 1;
[self performSelectorOnMainThread:@selector(meth2:) 
                       withObject:(__bridge id)(void*)next_int 
                    waitUntilDone:NO];

__bridge 关键字将告诉编译器在 ARC 下忽略引用计数,但它需要一个指针,因此您首先必须将 int 转换为 a C 风格的 void 指针。当您的方法收到消息时,它将将该对象指针视为整数。

注意:如果您可以更改方法以采用 NSNumber 而不是整数,那么这将是“正确的”修复。不幸的是,这并不总是可能的。

It's a bit of a hack, but this works under ARC:

int next_int = 1;
[self performSelectorOnMainThread:@selector(meth2:) 
                       withObject:(__bridge id)(void*)next_int 
                    waitUntilDone:NO];

The __bridge keyword will tell the compiler to ignore reference counting under ARC, but it requires a pointer, so you first have to cast the int to a C style void pointer. When your method receives the message it will treat that object pointer as if it's an integer.

Note: If you can change the method to take an NSNumber instead of an integer, then that would be the "proper" fix. Unfortunately that's not always possible.

-黛色若梦 2024-09-02 21:16:35

您不能将 next_int 用作 withObject:,因为它不是对象。

将您的呼叫更改为:

[self performSelectorOnMainThread:@selector(meth2:) 
      withObject:[NSNumber numberWithInt:next_int] waitUntilDone:NO];

编辑:
并将 meth2 更改为需要 NSNumber 而不是 int

You can't use next_int as the withObject: because it's not an Object.

Change your call to:

[self performSelectorOnMainThread:@selector(meth2:) 
      withObject:[NSNumber numberWithInt:next_int] waitUntilDone:NO];

EDIT:
And change meth2 to expect an NSNumber instead of an int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文