不兼容的指针类型发送&#ns; nsnumber *'参数类型' nsstring *'

时间:2015-02-11 09:24:56

标签: ios objective-c parse-platform nsstring nsnumber

我正试图让一个警告框显示一个随机生成的数字,但无论我怎么玩,我还没有让它工作,我仍然在网上搜索。这就是我所拥有的:

ticketNumber = [NSNumber numberWithInt:arc4random() %100000];

PFObject *people = [PFObject objectWithClassName:@"People"];
people[@"ticket"] = ticketNumber;
people[@"photo"] = chosenImage;
[people saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (succeeded) {
        // The object has been saved.
        UIAlertView *ticketNumberView = [[UIAlertView alloc] initWithTitle:@"Ticket Number"
                                                                   message:ticketNumber
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK, I got it."
                                                    otherButtonTitles: nil];

        [ticketNumberView show];

但是,我收到的错误是“不兼容的指针类型将'nsnumber *'发送到'nsstring *'类型的参数”。该应用程序仍在我的iPhone上进行编译和运行,但当它到达此代码区域时它会冻结并崩溃。我有什么不对吗?

3 个答案:

答案 0 :(得分:1)

[ticketNumber stringValue];

方法stringValue用于将ticketNumber表示为人类可读的字符串。

希望这有帮助

答案 1 :(得分:0)

您可以使用[ticketNumber stringValue]将NSNumber转换为NSString,并使用performSelectorOnMainThread显示警报视图。

[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];

答案 2 :(得分:0)

在UIAlertView类参考(https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html#//apple_ref/occ/instp/UIAlertView/message)中,"消息" property被定义为NSString类型。所以,试图发送" ticketNumber"这是一个NSNumber类型的对象(它继承自NSValue,而不是NSString)到UIAlertView的message属性中,这让编译器抱怨。要使用此初始值设定项,请首先使用其他答案中记录的[ticketNumber stringValue]方法调用将ticketNumber转换为NSString。