2010-11-15 42 views
5

好吧,我对此很难过。我搜索了过去的一小时,并没有看到我做错了什么。我正在尝试使用发件人的当前标题,然后将其转换为整数,以便我可以在列表中调用它。初始化使得整型指针没有强制转换

NSString *str = [sender currentTitle]; 
NSInteger *nt = [str integerValue]; // this is where the error appears // 
NSString *nextScreen = [NSString stringWithFormat:@"Screen_%@.jpg", [screenList objectAtIndex:nt]]; 

我认为这件事情有没有被正确使用的[str integerValue]一点,但我无法找到工作的例子。

谢谢!

回答

16

让我们来分析错误信息:

初始化(NSInteger nt)时将整数([str integerValue])指针(*)不进行强制转换。

这意味着你正在试图非指针型([str integerValue],它返回一个NSInteger)的变量分配给一个变量指针类型。 (NSInteger *)。

获取NSInteger后摆脱*的,你应该没问题:

NSString *str = [sender currentTitle]; 
NSInteger nt = [str integerValue]; // this is where the error appears // 
NSString *nextScreen = [NSString stringWithFormat:@"Screen_%@.jpg", [screenList objectAtIndex:nt]]; 

NSInteger是机器相关的整型数据类型,它是像这样定义的类型包装:

#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 
+2

这是对的。也许用户从所有对象平台转换而来的一件事大部分都会感到困惑。 – 2010-11-15 18:21:51

+1

非常感谢这个快速答案。这样做的诀窍和你的故障帮助我更快地理解“为什么”! – Eric 2010-11-15 18:51:53

+0

@Eric你非常欢迎。这就是StackOverflow的用途! :) – 2010-11-15 18:53:31

相关问题