gpt4 book ai didi

iphone - NSArray 在 objective-c 中丢失数据,有人可以解释为什么吗?

转载 作者:行者123 更新时间:2023-11-28 19:23:28 25 4
gpt4 key购买 nike

我有一个 nsarray,当我从我的一种方法 (在 WorkOutList.m 中) 中对它进行 NSLog 时,我可以看到它的内容,但是当我尝试从 WorkOutList 中的另一种方法查看它时.m 它似乎是空的。我知道我的内存管理需要一些工作,任何人都可以帮助解释这里发生了什么吗?

我正在使用 popViewControllerAnimated:YES 将 View 从 tableView 返回到 View Controller ,但就在我这样做之前,我在 WorkOutList 中的一个方法中设置了我的数组。当我从相同的方法 NSLog 该数组时,我返回了结果,但是当我从其他地方 NSLog 它返回空时。

有人告诉我,它可能是数组初始化的 viewDidLoad 方法,但该方法中的另一个数组 customWorkouts 仍然保留其数据。所以我不知道,任何解释都会很有帮助。我希望它能正常工作,但我也真的很想理解它,以便我可以继续正确编码。

非常感谢!

WorkOutList.h

#import <UIKit/UIKit.h>


@interface WorkOutList : UIViewController {

NSManagedObjectContext *managedObjectContext;
NSMutableArray *customWorkouts;
NSArray *passedWorkout;
}

@property(nonatomic, retain)NSManagedObjectContext *managedObjectContext;
@property(nonatomic, retain)NSMutableArray *customWorkouts;
@property(nonatomic, retain)NSArray *passedWorkout;

-(IBAction)customWorkouts:(id)sender;

-(void)passWorkoutBack:(NSArray *)workout;


@end

WorkOutList.m

@implementation WorkOutList

@synthesize managedObjectContext, customWorkouts, passedWorkout;

- (void)viewDidLoad {
[self setupContext];
NSLog(@"View Did Load");
customWorkouts = [[NSMutableArray alloc] init];
passedWorkout = [[NSArray alloc] init];
[super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self fetchWorkoutList];
NSLog(@"View will Appear");
NSLog(@"Array from View Will Appear : %@", passedWorkout);

}

-(IBAction)customWorkouts:(id)sender{
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
SelectedWorkout *selectedWorkout = [[SelectedWorkout alloc] initWithStyle:UITableViewStyleGrouped];

[selectedWorkout recieveNeededData:customWorkouts];

[appDelegate.practiceNavController pushViewController:selectedWorkout animated:YES];
[selectedWorkout release];
}

-(void)passWorkoutBack:(NSArray *)workout{
passedWorkout = workout;
[passedWorkout retain];
}

- (void)dealloc {
[super dealloc];
}

SelectedWorkout.h

#import <UIKit/UIKit.h>


@interface SelectedWorkout : UITableViewController {


NSMutableArray *workoutListForTable;
}

@property(nonatomic,retain)NSMutableArray *workoutListForTable;

-(void)recieveNeededData:(NSMutableArray *)workoutList;

@end

SelectedWorkout.m(除了设置 tableView 的所有内容)

    @implementation SelectedWorkout

@synthesize workoutListForTable;

-(void)recieveNeededData:(NSMutableArray *)workoutList{

if (workoutListForTable != workoutList) {
workoutListForTable = workoutList;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];

CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
WorkOutList *workoutListView = [[WorkOutList alloc]init];

[workoutListView passWorkoutBack:[workoutListForTable objectAtIndex:indexPath.row]];
[appDelegate.practiceNavController popViewControllerAnimated:YES];
}

- (void)dealloc {

[workoutListForTable release];
[super dealloc];
}


NSLog(@"other table : %@", workoutListForTable);
[workoutListForTable retain];
}

最佳答案

在这一行中:

passedWorkout = [[NSArray alloc] init];

您正在创建一个不可变的数组,其中没有任何内容。您希望它包含什么?

关于iphone - NSArray 在 objective-c 中丢失数据,有人可以解释为什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6613148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com