铸造问题(或者更确切地说,不铸造问题)

发布于 2024-11-05 05:48:18 字数 991 浏览 1 评论 0原文

我想这是非常基本的。

我从 xCode 收到两个相关警告。两者都说我正在尝试从整数创建一个指针而不进行强制转换。我怎样才能满足xCode?

这是我的代码:

int tempCurrentPage = currentPageCounter;
[self tabAdd:@"New tab!" inColour:@"Red" withReference:tempCurrentPage];

注意:currentPageCounter 是一个 NSUInteger currentPageCounter;

我的方法如下所示:

-(void)tabAdd:(NSString *)newTabTitle inColour:(NSString *)newTabColour withReference:(int *)newTabReference
{

    NSLog(@"#string about to be added:%@", newTabTitle);
    [[[self.myLibrary objectAtIndex:currentBookNumber] tabTitles] addObject:newTabTitle];

    NSLog(@"#string about to be added:%@", newTabColour);
    [[[self.myLibrary objectAtIndex:currentBookNumber] tabColours] addObject:newTabColour];

    NSLog(@"#string about to be added:%@", newTabReference);
    [[[self.myLibrary objectAtIndex:currentBookNumber] tabReference] addObject:[NSNumber numberWithInteger:newTabReference]];       

}

我应该如何进行转换?

I guess this is very basic.

I get two related warnings from xCode. Both say that I'm trying to make a pointer from integer without a cast. How can I satisfy xCode?

This is my code:

int tempCurrentPage = currentPageCounter;
[self tabAdd:@"New tab!" inColour:@"Red" withReference:tempCurrentPage];

Note: currentPageCounter is an NSUInteger currentPageCounter;.

My method looks like this:

-(void)tabAdd:(NSString *)newTabTitle inColour:(NSString *)newTabColour withReference:(int *)newTabReference
{

    NSLog(@"#string about to be added:%@", newTabTitle);
    [[[self.myLibrary objectAtIndex:currentBookNumber] tabTitles] addObject:newTabTitle];

    NSLog(@"#string about to be added:%@", newTabColour);
    [[[self.myLibrary objectAtIndex:currentBookNumber] tabColours] addObject:newTabColour];

    NSLog(@"#string about to be added:%@", newTabReference);
    [[[self.myLibrary objectAtIndex:currentBookNumber] tabReference] addObject:[NSNumber numberWithInteger:newTabReference]];       

}

How should I do a cast?

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

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

发布评论

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

评论(3

说好的呢 2024-11-12 05:48:18

withReference 需要一个 int *,但您正在传递一个 int。这是一个潜在的错误,可能会导致您的程序崩溃。看来你不需要在方法中指向整数的指针,只需传递一个整数就可以了。

// remove the pointer for newTabRefenerce
-(void)tabAdd:(NSString *)newTabTitle inColour:(NSString *)newTabColour withReference:(int)newTabReference

// newTabReference is integer, do use %d. %@ is for NSString
NSLog(@"#string about to be added:%d", newTabReference);

withReference is expecting a int *, but you are passing an int. This is a potential bug and may crash your program. It seems that you don't need a pointer to integer in the method, just passing a integer is fine.

// remove the pointer for newTabRefenerce
-(void)tabAdd:(NSString *)newTabTitle inColour:(NSString *)newTabColour withReference:(int)newTabReference

// newTabReference is integer, do use %d. %@ is for NSString
NSLog(@"#string about to be added:%d", newTabReference);
凉月流沐 2024-11-12 05:48:18

您正在传递一个指向整数的指针:

-(void)tabAdd:(NSString *)newTabTitle inColour:(NSString *)newTabColour withReference:(int *)newTabReference

将其更改为:

-(void)tabAdd:(NSString *)newTabTitle inColour:(NSString *)newTabColour withReference:(NSUInteger)newTabReference

在选择器调用中,直接传递 currentPageCounter:

[self tabAdd:@"New tab!" inColour:@"Red" withReference:currentPageCounter];

You're passing in a pointer to integer at:

-(void)tabAdd:(NSString *)newTabTitle inColour:(NSString *)newTabColour withReference:(int *)newTabReference

Change this for:

-(void)tabAdd:(NSString *)newTabTitle inColour:(NSString *)newTabColour withReference:(NSUInteger)newTabReference

and in the selector call, pass the currentPageCounter directly:

[self tabAdd:@"New tab!" inColour:@"Red" withReference:currentPageCounter];
秋日私语 2024-11-12 05:48:18

传入的强制转换应该是 (int) 而不是 (int*)。当您记录 newTabReference 时,使用 %d 而不是 %@ 进行记录。

The incoming cast should be (int) and not (int*). And when you log newTabReference log it using %d and not %@.

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