【问题标题】:initialization makes pointer from integer without a cast初始化使指针从整数而不进行强制转换
【发布时间】:2011-05-10 09:43:17
【问题描述】:

好的,我很难接受。我已经搜索了过去一个小时,但我不明白我做错了什么。我正在尝试获取发件人的 currentTitle,然后将其转换为整数,以便在调用列表时使用它。

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] 位未正确使用的问题,但我找不到有效的示例。

谢谢!

【问题讨论】:

    标签: objective-c pointers ios


    【解决方案1】:

    我们来分析一下错误信息:

    初始化 (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
    

    【讨论】:

    • 这是正确的。可能是用户从全对象平台过渡到大多数人感到困惑的事情之一。
    • 非常感谢您的快速回答。这就是诀窍,你的分解帮助我更快地理解了“为什么”!
    • @Eric 不客气。这就是 StackOverflow 的用途! :)
    猜你喜欢
    • 2012-10-30
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多