ios 初始化使指针从整数而不进行强制转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4187488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
initialization makes pointer from integer without a cast
提问by Eric
Okay, I am having a hard time with this. I've searched for the past hour on it and I don't get what I am doing wrong. I'm trying to take the currentTitle of a sender, then convert it to an integer so I can use it in a call to list.
好吧,我在这方面很难受。我已经搜索了过去一个小时,但我不明白我做错了什么。我正在尝试获取发件人的 currentTitle,然后将其转换为整数,以便我可以在对列表的调用中使用它。
NSString *str = [sender currentTitle];
NSInteger *nt = [str integerValue]; // this is where the error appears //
NSString *nextScreen = [NSString stringWithFormat:@"Screen_%@.jpg", [screenList objectAtIndex:nt]];
I assume it's something with the [str integerValue]
bit not being properly used, but I can't find an example that works.
我认为它的[str integerValue]
位没有被正确使用,但我找不到一个有效的例子。
Thanks!
谢谢!
回答by Jacob Relkin
Let's analyze the error message:
我们来分析一下错误信息:
Initialization (NSInteger nt
) makes pointer (*
) from integer ([str integerValue]
) without a cast.
初始化 ( NSInteger nt
)*
从整数 ( [str integerValue]
) 生成指针 ( ),无需强制转换。
This means that you are trying to assign a variable of non-pointer type ([str integerValue]
, which returns an NSInteger
) to a variable ofpointer type. (NSInteger *
).
这意味着你正在试图非指针型(的变量分配[str integerValue]
,它返回一个NSInteger
),以可变的指针类型。( NSInteger *
).
Get rid of the *
after NSInteger
and you should be okay:
摆脱*
之后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
is a type wrapper for the machine-dependent integral data type, which is defined like so:
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