Ios 程序在@synthesis name处挤压[order setPrice:price]断点

Ios 程序在@synthesis name处挤压[order setPrice:price]断点,ios,objective-c,Ios,Objective C,请帮我解决一个简单的问题。 我是objective-c的初学者,刚从java转到objective-c。我对java很了解,但不是很深入 我正在构建一个iphone应用程序。我的应用程序很简单。 我的iphone应用程序的目的是在餐厅用我的iphone应用程序点菜 我的应用程序的进度: 我的应用现在只有两个视图面板和按钮:) 是我的应用程序源代码,firstview和secondview 问题: 当我点击咖啡按钮时,我的文本字段不会显示咖啡名称和咖啡价格,它应该显示“咖啡1”。 xcode会把我

请帮我解决一个简单的问题。 我是objective-c的初学者,刚从java转到objective-c。我对java很了解,但不是很深入

我正在构建一个iphone应用程序。我的应用程序很简单。 我的iphone应用程序的目的是在餐厅用我的iphone应用程序点菜

我的应用程序的进度: 我的应用现在只有两个视图面板和按钮:) 是我的应用程序源代码,firstview和secondview

问题: 当我点击咖啡按钮时,我的文本字段不会显示咖啡名称和咖啡价格,它应该显示“咖啡1”。 xcode会把我从iphone模拟器中带到调试器。(我想它在一行中被压碎了,所以dubugger把我带到IBaction方法,并在@synthesis name行中断;它编译时没有错误。请帮助解决为什么xcode在我按下咖啡按钮时把我带到调试器的问题

这是咖啡按钮的动作代码

- (IBAction)Coffee:(id)sender {
    int price = 1;
    NSString *name = @"coffee";

    Storage *order = [[Storage alloc] init];
    [order setName:name];               // i assume the program crush at here when it look into setName method.
    [order setPrice:price];
    [orders addOrders:order];

    // Sanity check:     // the program not even hit this line yet before crush;
    NSLog(@"There are now %d orders in the array.", [orders.getOrders count]);

    for (Storage *obj in orders.getOrders){
        [check setText:[NSString stringWithFormat:@"%@",[obj description]]];  // check is the TextField instant varible. and the description method is from storage.m to print out the name and price.
    }
}
下面的代码是我的存储类,用于存储客户订购的所有项目。 它是一个二维数组,我的存储类是存储类的包装类。 数组格式如下所示:

arrayindex1->名称、价格

arrayindex2->名称、价格

ArrayIndex 3->名称、价格

arrayindex4->名称、价格

储存

#import <Foundation/Foundation.h>

@interface Storage : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger price;

@end
#import <Foundation/Foundation.h>

@interface Storage : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger price;

@end
仓库

#import <Foundation/Foundation.h>
#import "Storage.h"
@interface Storages : NSObject

@property (nonatomic,strong, readwrite) NSMutableArray *orders;

-(void) addOrders:(Storage *)anOrder;
-(NSMutableArray *) getOrders;

@end
#import <Foundation/Foundation.h>
#import "Storage.h"
@interface Storages : NSObject

@property (nonatomic,strong, readwrite) NSMutableArray *orders;

-(void) addOrders:(Storage *)anOrder;
-(NSMutableArray *) getOrders;

@end

描述在以下内容中做了什么?(我在存储类中看不到任何描述对象):

我想如果你想打印这个名字,你可以这样做:

[check setText:[NSString stringWithFormat:@"%@: %@",obj.name, obj.price]];

描述在以下内容中做了什么?(我在存储类中看不到任何描述对象):

我想如果你想打印这个名字,你可以这样做:

[check setText:[NSString stringWithFormat:@"%@: %@",obj.name, obj.price]];

您在
Storages
类中使用了
NSInteger
指针,该指针不正确。
NSInteger
是基本数据类型,不是指针。请删除该指针并使用
NSInteger
变量。 我希望这能解决你的问题

您可以使用以下代码:

@interface Storage : NSObject

@property (nonatomic, retain)NSString *name;

@property (nonatomic, assign)NSInteger price;

您在
Storages
类中使用了
NSInteger
指针,该指针不正确。
NSInteger
是基本数据类型,不是指针。请删除该指针并使用
NSInteger
变量。 我希望这能解决你的问题

您可以使用以下代码:

@interface Storage : NSObject

@property (nonatomic, retain)NSString *name;

@property (nonatomic, assign)NSInteger price;

这里有几个问题

1) 不要对price属性使用指针。通常,除非您正在做一些不寻常的事情,否则作为对象的属性将是指针,而作为基本体的属性(NSInteger、BOOL、float等)将不是指针

2) 您需要确保orders
NSMutableArray
已使用
Storages
对象初始化,否则orders将保持为零,并且无论何时尝试向其添加对象,都不会发生任何事情。要初始化
NSMutableArray
,请在
init
方法中执行此操作,如下所示。您还可以通过在(Storage*obj in orders.getOrders){…}循环中为(Storage*obj in orders){…}放入一个简单的NSLog语句,并确保在循环中至少得到一次迭代,来检查对象是否实际进入了一个有效的可变数组。如果orders.getOrders为nil,则for循环的工作块将永远不会运行

3) 听起来您需要覆盖(可能已经覆盖)存储对象的
-[NSObject description]
方法。我猜这个方法与
-[NSString stringWithFormat:…]格式字符串不匹配。例如,您可能在
NSInteger*
的格式字符串中使用了%d或%@。类似的事情肯定会导致崩溃(我想你所说的“Xcode带你去调试器”就是这个意思)。对于NSInteger,您需要使用%d或%i。正如我和其他人所提到的,您需要的是
NSInteger
,而不是
NSInteger*
,您应该更改您的属性声明

4) 根据这里的内容,我认为您根本不需要
Storages
类中的
order
属性

5) 确保您没有忘记将Interface Builder中的IBOutlet连接到
检查
文本字段。除了确认它在Interface Builder中已连接外,一个很好的测试是一个现实检查测试,如
[check setText:@“这是一个测试]

6) 请记住,一旦这样做成功,for循环将非常快地执行,并且您将立即只看到orders数组中最后一个对象的描述。但这似乎不是你的问题所在

我建议您进行以下更改:

储存

#import <Foundation/Foundation.h>

@interface Storage : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger price;

@end
#import <Foundation/Foundation.h>

@interface Storage : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger price;

@end
你的IBAction方法

- (IBAction)Coffee:(id)sender {
    int price = 1;
    NSString *name = @"coffee";

    Storage *order = [[Storage alloc] init];
    [order setName:name];
    [order setPrice:price];
    [orders addOrders:order];

    // Sanity check:
    NSLog(@"There are now %d orders in the array.", [orders.getOrders count]);

    for (Storage *obj in orders.getOrders){
        [check setText:[NSString stringWithFormat:@"%@",[obj description]]];  // check is the TextField instant varible. and the description method is from storage.m to print out the name and price.
    }
}
仓库

#import <Foundation/Foundation.h>
#import "Storage.h"
@interface Storages : NSObject

@property (nonatomic,strong, readwrite) NSMutableArray *orders;

-(void) addOrders:(Storage *)anOrder;
-(NSMutableArray *) getOrders;

@end
#import <Foundation/Foundation.h>
#import "Storage.h"
@interface Storages : NSObject

@property (nonatomic,strong, readwrite) NSMutableArray *orders;

-(void) addOrders:(Storage *)anOrder;
-(NSMutableArray *) getOrders;

@end

这里有几个问题

1) 不要对price属性使用指针。通常,除非您正在做一些不寻常的事情,否则作为对象的属性将是指针,而作为基本体的属性(NSInteger、BOOL、float等)将不是指针

2) 您需要确保orders
NSMutableArray
已使用
Storages
对象初始化,否则orders将保持为零,并且无论何时尝试向其添加对象,都不会发生任何事情。要初始化
NSMutableArray
,请在
init
方法中执行此操作,如下所示。您还可以通过在(Storage*obj in orders.getOrders){…}
循环中为(Storage*obj in orders){…}放入一个简单的NSLog语句,并确保在循环中至少得到一次迭代,来检查对象是否实际进入了一个有效的可变数组。如果orders.getOrders为nil,则for循环的工作块将永远不会运行

3) 听起来您需要覆盖(可能已经覆盖)存储的
-[NSObject description]
方法
- (NSString *)description {
    return [NSString stringWithFormat: @"%@        %d", name, price];
}
-(id)init
{
    if (self = [super init])
    {
        orders = [[NSMutableArray alloc] init];
    }
    return self;
}