为什么我的代码输出*nil description*

发布于 2024-12-14 06:41:45 字数 4055 浏览 0 评论 0原文

代码:

LotteryEntry.h

#import <Foundation/Foundation.h>

@interface LotteryEntry : NSObject
{
    NSCalendarDate *entryDate;
    int firstNumber;
    int secondNumber;
}

- (void) prepareRandomNumbers;
- (void) setEntryDate:(NSCalendarDate *)date;
- (NSCalendarDate*)entryDate;
- (int) firstNumber;
- (int) secondNumber;

@end

LotteryEntry.m

#import "LotteryEntry.h"

@implementation LotteryEntry

- (void) prepareRandomNumbers
{
    firstNumber = random() % 100 + 1;
    secondNumber = random() % 100 + 1;
}

- (void) setEntryDate:(NSCalendarDate *)date
{
    entryDate = date;
}

- (NSCalendarDate *) entryDate
{
    return entryDate;
}

- (int)firstNumber
{
    return firstNumber;
}

- (int)secondNumber
{
    return secondNumber;
}

- (NSString *)description
{
    NSString *result;
    result = [[NSString alloc] initWithFormat:@"%@ = %d and %d", 
              [entryDate descriptionWithCalendarFormat:@"%b %d %Y"],
              firstNumber, secondNumber];
}
@end

lottery.m

#import  <Foundation/Foundation.h>
#import "LotteryEntry.h";

int main (int argc, const char * argv[])
{        
    @autoreleasepool
    {      
        //create the date object
        NSCalendarDate *now = [[NSCalendarDate alloc] init];

        //see the random number generator
        srandom(time(NULL));
        NSMutableArray *array;
        array = [[NSMutableArray alloc] init];

        int i;
        for( i = 0; i < 10; i++) 
        {
            //create date time object that is i weeks from now
            NSCalendarDate *iWeeksFromNow;
            iWeeksFromNow = [now dateByAddingYears:0
                                            months:0
                                              days:(i * 7)
                                             hours:0
                                           minutes:0
                                           seconds:0];

            //create a new isntand of lottery entry
            LotteryEntry *newEntry = [[LotteryEntry alloc] init];
            [newEntry prepareRandomNumbers];
            [newEntry setEntryDate:iWeeksFromNow];

            //add the lottery entry object to the array
            [array addObject:newEntry];

            /*
            NSNumber *newNumber;
            newNumber = [[NSNumber alloc] initWithInt:(i * 3)];
            //[array addObject:newNumber];

            @try 
            {

                [array addObject:newNumber];
            } 
            @catch (NSException *exception) 
            {
                NSLog(@"catched error: %@", exception.description);
            }
             */
        }


        for (LotteryEntry *entryToPrint in array)
        {
            //display entry
            NSLog(@"%@", entryToPrint);
        }


    }
    return 0;
}

输出:

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug  8 20:32:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
[Switching to process 898 thread 0x0]
2011-11-08 11:16:21.193 lottery[898:707] *nil description*
2011-11-08 11:16:21.196 lottery[898:707] *nil description*
2011-11-08 11:16:21.198 lottery[898:707] *nil description*
2011-11-08 11:16:21.199 lottery[898:707] *nil description*
2011-11-08 11:16:21.200 lottery[898:707] *nil description*
2011-11-08 11:16:21.201 lottery[898:707] *nil description*
2011-11-08 11:16:21.202 lottery[898:707] *nil description*
2011-11-08 11:16:21.204 lottery[898:707] *nil description*
2011-11-08 11:16:21.205 lottery[898:707] *nil description*
2011-11-08 11:16:21.206 lottery[898:707] *nil description*
Program ended with exit code: 0           

Code:

LotteryEntry.h

#import <Foundation/Foundation.h>

@interface LotteryEntry : NSObject
{
    NSCalendarDate *entryDate;
    int firstNumber;
    int secondNumber;
}

- (void) prepareRandomNumbers;
- (void) setEntryDate:(NSCalendarDate *)date;
- (NSCalendarDate*)entryDate;
- (int) firstNumber;
- (int) secondNumber;

@end

LotteryEntry.m

#import "LotteryEntry.h"

@implementation LotteryEntry

- (void) prepareRandomNumbers
{
    firstNumber = random() % 100 + 1;
    secondNumber = random() % 100 + 1;
}

- (void) setEntryDate:(NSCalendarDate *)date
{
    entryDate = date;
}

- (NSCalendarDate *) entryDate
{
    return entryDate;
}

- (int)firstNumber
{
    return firstNumber;
}

- (int)secondNumber
{
    return secondNumber;
}

- (NSString *)description
{
    NSString *result;
    result = [[NSString alloc] initWithFormat:@"%@ = %d and %d", 
              [entryDate descriptionWithCalendarFormat:@"%b %d %Y"],
              firstNumber, secondNumber];
}
@end

lottery.m

#import  <Foundation/Foundation.h>
#import "LotteryEntry.h";

int main (int argc, const char * argv[])
{        
    @autoreleasepool
    {      
        //create the date object
        NSCalendarDate *now = [[NSCalendarDate alloc] init];

        //see the random number generator
        srandom(time(NULL));
        NSMutableArray *array;
        array = [[NSMutableArray alloc] init];

        int i;
        for( i = 0; i < 10; i++) 
        {
            //create date time object that is i weeks from now
            NSCalendarDate *iWeeksFromNow;
            iWeeksFromNow = [now dateByAddingYears:0
                                            months:0
                                              days:(i * 7)
                                             hours:0
                                           minutes:0
                                           seconds:0];

            //create a new isntand of lottery entry
            LotteryEntry *newEntry = [[LotteryEntry alloc] init];
            [newEntry prepareRandomNumbers];
            [newEntry setEntryDate:iWeeksFromNow];

            //add the lottery entry object to the array
            [array addObject:newEntry];

            /*
            NSNumber *newNumber;
            newNumber = [[NSNumber alloc] initWithInt:(i * 3)];
            //[array addObject:newNumber];

            @try 
            {

                [array addObject:newNumber];
            } 
            @catch (NSException *exception) 
            {
                NSLog(@"catched error: %@", exception.description);
            }
             */
        }


        for (LotteryEntry *entryToPrint in array)
        {
            //display entry
            NSLog(@"%@", entryToPrint);
        }


    }
    return 0;
}

Output:

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug  8 20:32:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
[Switching to process 898 thread 0x0]
2011-11-08 11:16:21.193 lottery[898:707] *nil description*
2011-11-08 11:16:21.196 lottery[898:707] *nil description*
2011-11-08 11:16:21.198 lottery[898:707] *nil description*
2011-11-08 11:16:21.199 lottery[898:707] *nil description*
2011-11-08 11:16:21.200 lottery[898:707] *nil description*
2011-11-08 11:16:21.201 lottery[898:707] *nil description*
2011-11-08 11:16:21.202 lottery[898:707] *nil description*
2011-11-08 11:16:21.204 lottery[898:707] *nil description*
2011-11-08 11:16:21.205 lottery[898:707] *nil description*
2011-11-08 11:16:21.206 lottery[898:707] *nil description*
Program ended with exit code: 0           

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独难免 2024-12-21 06:41:45

您的 -description 方法实际上没有返回任何内容。您应该收到编译器警告来告诉您这一点。

它还会泄漏您在该方法中分配/初始化的字符串(除非您在 ARC 下运行)。

请尝试以下方法:

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ = %d and %d", 
              [entryDate descriptionWithCalendarFormat:@"%b %d %Y"],
              firstNumber, secondNumber];
}

Your -description method isn't actually returning anything. You should be getting a compiler warning telling you this.

It's also leaking the string that you're alloc/initing in that method (unless you're running under ARC).

Try the following instead:

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ = %d and %d", 
              [entryDate descriptionWithCalendarFormat:@"%b %d %Y"],
              firstNumber, secondNumber];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文