将对象添加到数组不起作用,当我遍历数组时,我看不到任何对象

时间:2012-07-24 12:26:46

标签: iphone objective-c ios nsmutablearray

我想要的是什么:

在iPhone上,用户输入少量数值并点击保存按钮。 应用应将值存储在VehicleClass的实例中,并将其添加到NSMutableArray

希望你能提供帮助。

ViewController.h

#import <UIKit/UIKit.h>
#import "VehicleClass.h"

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *producerTextField;
@property (weak, nonatomic) IBOutlet UITextField *modelTextField;
@property (weak, nonatomic) IBOutlet UITextField *pYearTextField;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;

@property (weak, nonatomic) NSMutableArray *carArray;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;


- (IBAction)addCar:(id)sender;
- (IBAction)showCars:(id)sender;

@end

ViewController.m

@synthesize producerTextField, modelTextField, pYearTextField, priceTextField, carArray;

- (void)viewDidLoad
{
    [super viewDidLoad];

    carArray = [NSMutableArray array];
}

- (IBAction)addCar:(id)sender
{
    VehicleClass *newVehic = [[VehicleClass alloc]initWithProducer:producerTextField.text 
                                                          andModel:modelTextField.text
                                                          andPYear:producerTextField.text
                                                          andPrice:priceTextField.text];

    [carArray addObject:newVehic];
    if (carArray != nil) {
        statusLabel.text = @"Car saved to array.";
    }
    else {
        statusLabel.text = @"Error, check your code.";
    }
}

VehicleClass.h

#import <Foundation/Foundation.h>

@interface VehicleClass : NSObject

@property (weak, nonatomic) NSString *producer;
@property (weak, nonatomic) NSString *model;
@property (weak, nonatomic) NSString *pYear;
@property (weak, nonatomic) NSString *price;


-(id)initWithProducer:(NSString *)varProducer 
             andModel:(NSString *)varModel 
             andPYear:(NSString *)varPYear 
             andPrice:(NSString *)varPrice; 

@end

VehicleClass.m

#import "VehicleClass.h"

@implementation VehicleClass

@synthesize model, price,pYear,producer;


-(id)initWithProducer:(NSString *)varProducer 
             andModel:(NSString *)varModel 
             andPYear:(NSString *)varPYear 
             andPrice:(NSString *)varPrice
{
    self = [super init];
    if (self != nil) 
    {
        producer = varProducer;
        model = varModel;
        pYear = varPYear;
        price = varPYear;
    }
    return self;
}

@end

问题可能与我的init方法有关吗?

3 个答案:

答案 0 :(得分:3)

carArray是一个弱小的属性,它应该很强大。

另外:您应该使用self.carArray而不是carArray。使用carArray时,您使用的是基础iVar而不是实际属性。这会在以后引起混淆。

答案 1 :(得分:0)

您的属性应该是property (weak, nonatomic),而不是property (nonatomic, retain)

在init方法中,输入:

-(id)initWithProducer:(NSString *)varProducer 
             andModel:(NSString *)varModel 
             andPYear:(NSString *)varPYear 
             andPrice:(NSString *)varPrice
{
    self = [super init];
    if (self != nil) 
    {
        self.producer = varProducer;
        self.model = varModel;
        self.pYear = varPYear;
        self.price = varPYear;
    }
    return self;
}

然后,要检查您的carArray是否包含对象,而不是使用if (carArray != nil),请使用if ([carArray count] > 0),因为您已经初始化了它。

答案 2 :(得分:0)

你的carArray属于弱势财产。 carArray应该很强大。