为啥这个 NSMutableArray 没有被填充?
Posted
技术标签:
【中文标题】为啥这个 NSMutableArray 没有被填充?【英文标题】:Why isn't this NSMutableArray getting populated?为什么这个 NSMutableArray 没有被填充? 【发布时间】:2010-08-27 01:38:55 【问题描述】:我有一个NSMutableArray
我正在尝试使用for-loop
填充并为它制作NSStrings
。这些将成为我的UIPickerView
的数据源。您会看到一些注释掉的行,其中我手动创建了 NSArrays
并且它们显示得很好,但是我的 for-loop
NSMutableArray
似乎不接受我正在制作的字符串。 NSLogs
表明我正在制作字符串(以及等效的 float
),但是从 NSMutableArray
中提取值的 NSLogs
显示为 null
和 0.0
界面...
// PoolSizePickerViewController.h
#import <UIKit/UIKit.h>
#define kLengthComponent 0
#define kWidthComponent 1
#define kDepthComponent 2
@protocol PoolSizePickerViewControllerDelegate;
@interface PoolSizePickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
UIPickerView *poolSizePicker;
float length, width, depth;
NSMutableArray *lengthStrings, *widthStrings, *depthStrings;
NSMutableArray *lengthFloats, *widthFloats, *depthFloats;
NSString *pickerType;
NSString *pickerDescription;
UIButton *selectButton;
UIButton *cancelButton;
UILabel *pickerTitleLabel;
UITextView *pickerDescriptionLabel;
id <PoolSizePickerViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIPickerView *poolSizePicker;
@property float length, width, depth;
@property (nonatomic, retain) NSMutableArray *lengthStrings, *widthStrings, *depthStrings;
@property (nonatomic, retain) NSMutableArray *lengthFloats, *widthFloats, *depthFloats;
@property (nonatomic, retain) NSString *pickerType;
@property (nonatomic, retain) NSString *pickerDescription;
@property (nonatomic, retain) IBOutlet UIButton *selectButton;
@property (nonatomic, retain) IBOutlet UIButton *cancelButton;
@property (nonatomic, retain) IBOutlet UILabel *pickerTitleLabel;
@property (nonatomic, retain) IBOutlet UITextView *pickerDescriptionLabel;
@property (assign) id <PoolSizePickerViewControllerDelegate> delegate;
- (IBAction)selectedSelectButton;
- (IBAction)selectedCancelButton;
@end
@protocol PoolSizePickerViewControllerDelegate <NSObject>
@optional
- (void)poolSizePickerViewController:(PoolSizePickerViewController *)controller
didSelectLength:(float)length
andWidth:(float)width
andDepth:(float)depth;
- (void)poolSizePickerViewController:(PoolSizePickerViewController *)controller
didSelectCancel:(BOOL)didCancel;
@end
以及实现...
// PoolSizePickerViewController.m
#import "PoolSizePickerViewController.h"
@implementation PoolSizePickerViewController
@synthesize poolSizePicker;
@synthesize length, width, depth;
@synthesize lengthStrings, widthStrings, depthStrings;
@synthesize lengthFloats, widthFloats, depthFloats;
@synthesize delegate;
@synthesize pickerType, pickerDescription;
@synthesize selectButton, cancelButton;
@synthesize pickerTitleLabel;
@synthesize pickerDescriptionLabel;
- (IBAction)selectedSelectButton
NSInteger lengthRow = [poolSizePicker selectedRowInComponent:kLengthComponent];
NSInteger widthRow = [poolSizePicker selectedRowInComponent:kWidthComponent];
NSInteger depthRow = [poolSizePicker selectedRowInComponent:kDepthComponent];
length = [[self.lengthFloats objectAtIndex:lengthRow] floatValue];
width = [[self.widthFloats objectAtIndex:widthRow] floatValue];
depth = [[self.depthFloats objectAtIndex:depthRow] floatValue];
if ([self.delegate respondsToSelector:@selector (poolSizePickerViewController:didSelectLength:andWidth:andDepth:)])
[self.delegate poolSizePickerViewController:self didSelectLength:length andWidth:width andDepth:depth];
- (IBAction)selectedCancelButton
if ([self.delegate respondsToSelector:@selector (poolSizePickerViewController:didSelectCancel:)])
[self.delegate poolSizePickerViewController:self didSelectCancel:YES];
- (void)viewDidLoad
for (int footIndex = 6; footIndex < 40; footIndex ++)
for (int inchIndex = 0; inchIndex < 2; inchIndex ++)
[self.lengthStrings addObject:[NSString stringWithFormat:@" %d' %d\"", footIndex, inchIndex * 6]];
NSLog(@" %d' %d\"", footIndex, inchIndex * 6);
NSLog(@" -%@", [self.lengthStrings objectAtIndex:footIndex - 6]);
[self.lengthFloats addObject:[NSNumber numberWithFloat:(float)footIndex + (float)inchIndex * 0.5f]];
NSLog(@" %1.1f", (float)footIndex + (float)inchIndex * 0.5f);
NSLog(@" -%1.1f", [[self.lengthFloats objectAtIndex:footIndex - 6] floatValue]);
for (int footIndex = 6; footIndex < 40; footIndex ++)
for (int inchIndex = 0; inchIndex < 2; inchIndex ++)
[self.widthStrings addObject:[NSString stringWithFormat:@" %d' %d\"", footIndex, inchIndex * 6]];
[self.widthFloats addObject:[NSNumber numberWithFloat:(float)footIndex + (float)inchIndex * 0.5f]];
for (int footIndex = 1; footIndex < 16; footIndex ++)
for (int inchIndex = 0; inchIndex < 2; inchIndex ++)
[self.depthStrings addObject:[NSString stringWithFormat:@" %d' %d\"", footIndex, inchIndex * 6]];
[self.depthFloats addObject:[NSNumber numberWithFloat:(float)footIndex + (float)inchIndex * 0.5f]];
// lengthStrings = [NSArray arrayWithObjects:@" 6' 0\"", nil];
// lengthFloats = [NSArray arrayWithObjects:[NSNumber numberWithFloat:1.0], nil];
// widthStrings = [NSArray arrayWithObjects:@" 6' 0\"", nil];
// widthFloats = [NSArray arrayWithObjects:[NSNumber numberWithFloat:2.0], nil];
// depthStrings = [NSArray arrayWithObjects:@" 1' 0\"", nil];
// depthFloats = [NSArray arrayWithObjects:[NSNumber numberWithFloat:3.0], nil];
UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[selectButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[cancelButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[selectButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
[cancelButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
pickerTitleLabel.text = [NSString stringWithFormat:@"Select a Pool %@", pickerType];
pickerDescriptionLabel.text = self.pickerDescription;
self.poolSizePicker.showsSelectionIndicator = YES;
[super viewDidLoad];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
- (void)viewDidUnload
[super viewDidUnload];
- (void)dealloc
[super dealloc];
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
return 3;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
switch (component)
case kLengthComponent:
return [self.lengthStrings count];
break;
case kWidthComponent:
return [self.widthStrings count];
break;
default:
return [self.lengthStrings count];
break;
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
switch (component)
case kLengthComponent:
return [self.lengthStrings objectAtIndex:row];
break;
case kWidthComponent:
return [self.widthStrings objectAtIndex:row];
break;
default:
return [self.depthStrings objectAtIndex:row];
break;
@end
【问题讨论】:
【参考方案1】:您是否正在某个地方初始化您的数组?我在任何地方都看不到[[NSMutableArray alloc] init]
。向 null 对象发送消息是无操作的,因此这些 addObjects 很可能被发送到 null 并且没有做任何事情。
【讨论】:
这是有道理的。我已经尽可能少地让自己适应alloc
!但是它是如何与手动数组一起工作的呢? [NSMutableArray addObject:]
与 [NSArray arrayWithObjects:]
有什么不同吗? @property (nonatomic, retain)
不会初始化它们吗?我会在viewDidLoad
中尝试alloc
和init
,然后在dealloc
中释放它们...
@Steve,[NSArray arrayWithObjects:] 等于 [[[NSArray alloc] initWithObjects:] autorelease]。
男孩,我希望有关于自动发布内容的参考!我在 Apple 文档中花了很多时间,但我没有提到那里的自动发布状态。谢谢!
大多数东西都是自动释放的,除非方法名称包括 init、new 或 copy。
这里有自动发布的参考,检查:developer.apple.com/mac/library/documentation/cocoa/conceptual/…以上是关于为啥这个 NSMutableArray 没有被填充?的主要内容,如果未能解决你的问题,请参考以下文章