ios - 程序暗恋 [order setPrice :price] break point at @synthesize name

标签 ios objective-c

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

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

我的应用程序的进度: 我的应用程序现在只有几个 viewPanels 和按钮 :) Here是我的应用程序源代码,firstview screenshot & secondview screenshot

问题: 当我点击 Coffee 按钮时,我的 textField 不会显示咖啡名称和咖啡价格,它们应该显示为“coffee 1”。 xcode 会将我从 iphone 模拟器带到调试器。(我认为它在一行中被压碎,所以调试器将我带到 IBaction 方法并在@synthesize name 行中断;它编译没有错误。请帮助解决原因当我按下咖啡按钮时,xcode 将我带到调试器。

SCREEN SHOWS UP RIGHT AFTER PRESS THE COFFEE BUTTON

这是咖啡按钮的 Action 代码

- (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.
    }
}

下面的代码是我的存储类,用于存储我的客户订购的所有商品。 它是一个二维数组,我的 Storages 类是 Storage 类的包装类。 数组格式如下所示:

arrayindex1->名称,价格

arrayindex2->名称,价格

arrayindex3->名称、价格

arrayindex4->名称,价格

存储.h

#import <Foundation/Foundation.h>

@interface Storage : NSObject

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

@end

存储.m

#import "Storage.h"

@implementation Storage

@synthesize name;         // program crush and goes to here.
@synthesize price;

- (NSString *)description {
    // example: "coffee 1"
    return [NSString stringWithFormat:@"%@ %d", self.name, self.price];
}

@end

存储.h

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

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

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

@end

存储.m

#import "Storages.h"

@implementation Storages
@synthesize orders;

- (id)init {
    self = [super init];
    if (self) {
        orders = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void) addOrders:(Storage *)anOrder{
    [orders addObject: anOrder];
}
-(NSMutableArray *) getOrders{
    return orders;
}
@end

最佳答案

这里有几个问题。

1) 不要对价格属性使用指针。一般来说,除非你做一些不寻常的事情,否则你的对象属性将是指针,而你的基元属性(NSInteger、BOOL、float 等)将不是指针。

2) 你需要确保订单 NSMutableArray 是用 Storages 对象初始化的,否则订单将保持为 nil 并且每当你尝试向它添加对象时,什么都不会发生。要初始化 NSMutableArray,请在 init 方法中执行此操作,如下所示。您还可以通过在 for (Storage *obj in orders.getOrders) { ... } 循环中放置一个简单的 NSLog 语句并确保您在循环中至少获得一次迭代。如果 orders.getOrders 为 nil,for 循环的工作 block 将永远不会运行。

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

4) 根据您所拥有的,我认为您根本不需要 Storages 类中的 order 属性。

5) 确保您没有忽略忘记将 Interface Builder 中的 IBOutlet 连接到 check 文本字段的可能性。对此的一个很好的测试,除了确认它在 Interface Builder 中连接之外,将是一个现实检查测试,如 [check setText:@"This is a test."];

6) 请记住,一旦此操作生效,您的 for 循环将执行得非常快,您将立即只看到订单数组中最后一个对象的描述。但这似乎不是你的问题。

我建议您进行以下更改:

存储.h

#import <Foundation/Foundation.h>

@interface Storage : NSObject

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

@end

存储.m

#import "Storage.h"

@implementation Storage

@synthesize name;
@synthesize price;

- (NSString *)description {
    // example: "coffee 1"
    return [NSString stringWithFormat:@"%@ %d", self.name, self.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.
    }
}

存储.h

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

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

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

@end

存储.m

#import "Storages.h"

@implementation Storages
@synthesize orders;

- (id)init {
    self = [super init];
    if (self) {
        orders = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void) addOrders:(Storage *)anOrder{
    [orders addObject: anOrder];
}
-(NSMutableArray *) getOrders{
    return orders;
}
@end

关于ios - 程序暗恋 [order setPrice :price] break point at @synthesize name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14597121/

相关文章:

iphone - 将标题添加到以编程方式创建的选项卡栏

objective-c - iOS mapview - 添加注释时出现问题

ios - iOS,fopen挂起/卡住

ios - 如何防止当前正在审核的应用程序版本自动发布?

iphone - UIScrollView - 显示滚动条

objective-c - 如何在NSBrowser中向前和向后遍历?

ios - tableViewCell MVVM 中的 CollectionView

ios - 苹果 map 上的方向

ios - CAAnimationGroup在动画开始时播放声音

iphone - 在不引发警告的情况下以动画方式进出 iAd