views:

1207

answers:

3

This sounds like a simple one but I can't find the solution anywhere.

How do you cast objects in cocoa objective c for the iPhone?

I'm trying to get the length of a string in an nsuinteger object like so:

        NSUInteger *phoneNumberLength = [phoneNumber length];

I get the warning initialization makes pointer from integer without a cast.

How do I cast this and all objects in general?

+5  A: 

NSUInteger is a int, not an object. You don't want a pointer to it probably, so use simply

NSUInteger phoneNumberLength = [phoneNumber length];
cobbal
+1  A: 

cobbal is right, NSUInteger is an primitive type, not an pointer. In this case the length method is just returning a primitive type, not a pointer to a primitive. See this header file:

(location may be different depending on where you installed the SDK)

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
Andy White
+2  A: 

cobbal and Andy are right, but for the future if you want to cast an object you would simply do Object1 *obj = (Object1 *)object2;

Marc Charbonneau