开发者

Problems with casting (or rather not-casting)

开发者 https://www.devze.com 2023-03-03 10:44 出处:网络
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?

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 numberWithInteg开发者_JAVA百科er:newTabReference]];       

}

How should I do a cast?


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);


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];


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号