【问题标题】:NSLog not returning consistent resultsNSLog 没有返回一致的结果
【发布时间】:2011-06-22 15:03:11
【问题描述】:

我有一个 NSLog 问题,它不会在加载之间保持结果相同。这很令人沮丧,但如果有人能提供一些启示,那就太好了。我正在尝试从我的应用委托中调用一个数组

        NSLog(@"Selected Team 1 : %@", [[[appDelegate teamRoster]objectAtIndex:1] class]);
        NSLog(@"Selected Team 0 : %@", [[[appDelegate teamRoster]objectAtIndex:0] class]);

        NSLog(@"Selected Team : %@", [[[appDelegate teamRoster]objectAtIndex:indexPath.row] class]);
        NSLog(@"Selected Team : %@", [[appDelegate teamRoster]objectAtIndex:indexPath.row]);

连续三次我都返回了这些

2011-06-22 09:56:54.734 CoCoach[33182:207] Selected Team 1 : _NSIndexPathUniqueTreeNode
2011-06-22 09:56:54.735 CoCoach[33182:207] Selected Team 0 : __NSCFSet
2011-06-22 09:56:54.735 CoCoach[33182:207] Selected Team : __NSCFSet
2011-06-22 09:56:54.736 CoCoach[33182:207] Selected Team : {(
)}
2011-06-22 09:56:54.737 CoCoach[33182:207] Selected Team 1 : _NSIndexPathUniqueTreeNode
2011-06-22 09:56:54.737 CoCoach[33182:207] Selected Team 0 : __NSCFSet
2011-06-22 09:56:54.738 CoCoach[33182:207] Selected Team : _NSIndexPathUniqueTreeNode
2011-06-22 09:56:54.738 CoCoach[33182:207] Selected Team : <_NSIndexPathUniqueTreeNode: 0x703e6c0>

2

2011-06-22 09:58:30.082 CoCoach[33189:207] Selected Team 1 : __NSArrayM
2011-06-22 09:58:30.083 CoCoach[33189:207] Selected Team 0 : NSCFString
2011-06-22 09:58:30.083 CoCoach[33189:207] Selected Team : NSCFString
2011-06-22 09:58:30.084 CoCoach[33189:207] Selected Team : Koch
2011-06-22 09:58:30.084 CoCoach[33189:207] Selected Team 1 : __NSArrayM
2011-06-22 09:58:30.085 CoCoach[33189:207] Selected Team 0 : NSCFString
2011-06-22 09:58:30.085 CoCoach[33189:207] Selected Team : __NSArrayM
2011-06-22 09:58:30.086 CoCoach[33189:207] Selected Team : (
)

3

2011-06-22 09:59:17.825 CoCoach[33192:207] Selected Team 1 : _UITableViewReorderingSupport
2011-06-22 09:59:17.826 CoCoach[33192:207] Selected Team 0 : NSCFString
2011-06-22 09:59:17.826 CoCoach[33192:207] Selected Team : NSCFString
2011-06-22 09:59:17.826 CoCoach[33192:207] Selected Team : Smith
2011-06-22 09:59:17.827 CoCoach[33192:207] Selected Team 1 : _UITableViewReorderingSupport
2011-06-22 09:59:17.827 CoCoach[33192:207] Selected Team 0 : NSCFString
2011-06-22 09:59:17.828 CoCoach[33192:207] Selected Team : _UITableViewReorderingSupport
2011-06-22 09:59:17.828 CoCoach[33192:207] Selected Team : <_UITableViewReorderingSupport: 0x5b3cf30>

此外,当我检查数组中的内容时,我会得到以下信息。

2011-06-22 09:58:30.078 CoCoach[33189:207] Team Roster Array: (
    "Koch",
    "Smith"
)

这完全符合预期。

什么给了?有任何想法吗。谢谢。

更新:添加数组的函数

- (void)fetchRecords:(NSString *)teamToFind{

    teamRoster = [[NSMutableArray alloc] init];

    NSManagedObjectContext *context = [self managedObjectContext];
    // Define our table/entity to use
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Teams" inManagedObjectContext:context];

    // Setup the fetch request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    // Define how we will sort the records
    NSSortDescriptor *teams = [[NSSortDescriptor alloc] initWithKey:@"Team" ascending:NO];

    NSArray *sortDescriptors = [NSArray arrayWithObject:teams];


    [request setSortDescriptors:sortDescriptors];
    [teams release];

    // Fetch the records and handle an error
    NSError *error;
    NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];


    if (!mutableFetchResults) {
        // Handle the error.
        // This is a serious error and should advise the user to restart the application
    }

    // Save our fetched data to an array
    [self setStoredTeams:mutableFetchResults];
    NSLog(@"Number of teams, %i", [storedTeams count]);

//  NSMutableArray *temp = [[NSMutableArray alloc] init];

    for (Teams *diffTeams in storedTeams) {
        NSLog(@"Name: %@", diffTeams.Team);
        NSSet *rowers = [[NSSet alloc] initWithSet:diffTeams.Rowers];
        for (Rowers *roster in rowers){
            if ([diffTeams.Team isEqualToString:teamToFind]) {
                NSString *fullName = [NSString stringWithFormat:@"%@ %@", roster.FName, roster.LName];
                NSLog(@"%@", fullName);
                [teamRoster addObject:fullName];
                [fullName release];
                NSLog(@"Team Roster Array: %@", teamRoster);                
            }
        }
    }
    if (![context save:&error]) {
        //This is a serious error saying the record could not be saved.
        //Advise the user to restart the application
    }

    [mutableFetchResults release];
    [request release];
}

【问题讨论】:

  • 您是否正确保留了添加到[appDelegate teamRoster] 的对象? ,看起来指针曾经被设置为一个被释放的对象,并且内存地址被重用于另一个对象。请在您设置数组的位置添加代码。
  • 好吧,我会用一个问题来回答你的问题,在我的 appDelegate 中,设置数组后,我需要说 [teamRoster retain] 吗?出发前?
  • 首先取决于您创建数组的方式。如果您使用了 [NSMutableArray array] 之类的类方法,那么可以。如果您使用了 alloc/init 样式创建,那么没有(也许)
  • NSMutableArray *storedTeams; @property (nonatomic, 保留) NSMutableArray *storedTeams;在 .h 文件中
  • 第一个评论是正确的,我正在释放我放入对象的 NSString,但是在 for 循环中,对象被创建并第二次释放。为什么这对数组有影响?

标签: iphone objective-c ios xcode


【解决方案1】:

我想你想在你的NSLog 参数中使用NSStringFromClass([[[appDelegate teamRoster]objectAtIndex:1] class])

【讨论】:

    【解决方案2】:

    我正在释放我放入对象的 NSString,但在 for 循环中,该对象被创建并第二次释放

    【讨论】:

      猜你喜欢
      • 2021-07-23
      • 1970-01-01
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多