Chellange: Warning for NSInteger


#1

Hello
I’m reading this book and I am by the chellange of chapter 5.
I have added a new controller class. In the .m file i have the following code:

//
//  StrinCountController.m
//  StringCount
//
//  Created by Roger Kreienbühl on 13.12.11.
//  Copyright (c) 2011 privat. All rights reserved.
//

#import "StrinCountController.h"

@implementation StrinCountController
@synthesize textField;
@synthesize outputLabel;

- (IBAction)countCharacters:(id)sender {
    
    // Get String of textField
    NSString *string = [NSString stringWithFormat:[textField stringValue]];
    
    // Count Characters of string
    NSInteger *characters = [string length];
    
    // Set output-string to outputLabel
    [outputLabel setStringValue:[NSString stringWithFormat:@"'%@' has %i characters.", string, characters]];
    
}
@end

The code works fine, but in Xcode i have a warning for the NSInteger: Incompatible integer to pointer conversion initializing ‘NSInteger *’ (aka ‘long *’) with an expression of type ‘NSUInteger’ (aka ‘unsigned long’)

What is this meaning?

Thanks


#2

So, I have searched the reference for NSInteger and found out, that NSInteger is only a datatype like integer, so I don’t have to set a *.


#3

Yeah, NSInteger and NSUInteger are a bit confusing at first. As you found out they are just primitives by another name.

It’s part of Apple’s transition to 64-bit. Rather than worrying about if you need an int or a long you can just say NSInteger and it’s all good. That said, you could also just say long…