【问题标题】:Count number of objects of class计算类的对象数
【发布时间】:2015-02-14 08:57:12
【问题描述】:

我已经声明了一个 Recipe 类并手动创建了 16 个对象。在我的应用程序中,一个对象就是我的一行。

所以我需要在方法 numberOfRowsInSection 中返回行数

如何返回Recipe类拥有的对象数

这是我的视图 controller.m 文件

- (void)viewDidLoad
{
    [super viewDidLoad];

    Recipe *recipe1 = [Recipe new];
    recipe1.name = @"Egg Benedict";
    recipe1.prepTime = @"30 min";
    recipe1.imageFile = @"egg_benedict.jpg";
    recipe1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs", @"4 rashers of back bacon", @"2 egg yolks", @"1 tbsp of lemon juice", @"125 g of butter", @"salt and pepper", nil];

    Recipe *recipe2 = [Recipe new];
    recipe2.name = @"Mushroom Risotto";
    recipe2.prepTime = @"30 min";
    recipe2.imageFile = @"mushroom_risotto.jpg";
    recipe2.ingredients = [NSArray arrayWithObjects:@"1 tbsp dried porcini mushrooms", @"2 tbsp olive oil", @"1 onion, chopped", @"2 garlic cloves", @"350g/12oz arborio rice", @"1.2 litres/2 pints hot vegetable stock", @"salt and pepper", @"25g/1oz butter", nil];

    Recipe *recipe3 = [Recipe new];
    recipe3.name = @"Full Breakfast";
    recipe3.prepTime = @"20 min";
    recipe3.imageFile = @"full_breakfast.jpg";
    recipe3.ingredients = [NSArray arrayWithObjects:@"2 sausages", @"100 grams of mushrooms", @"2 rashers of bacon", @"2 eggs", @"150 grams of baked beans", @"Vegetable oil", nil];
    .
    .
    .
   // up to 16 objects
    .
    .
    .
    Recipe *recipe15 = [Recipe new];
    recipe15.name = @"Angry Birds Cake";
    recipe15.prepTime = @"4 hours";
    recipe15.imageFile = @"angry_birds_cake.jpg";
    recipe15.ingredients = [NSArray arrayWithObjects:@"12 tablespoons (1 1/2 sticks) unsalted butter", @"2 1/2 cups all-purpose flour", @"1 tablespoon baking powder", @"1 teaspoon salt", @"1 3/4 cups sugar", @"2 large eggs, plus 3 large egg yolks", @"1 cup of milk", nil];

    Recipe *recipe16 = [Recipe new];
    recipe16.name = @"Ham and Cheese Panini";
    recipe16.prepTime = @"10 min";
    recipe16.imageFile = @"ham_and_cheese_panini.jpg";
    recipe16.ingredients = [NSArray arrayWithObjects:@"2 tablespoons unsalted butter", @"4 cups thinly sliced shallots", @"2 teaspoons fresh thyme", @"1/4 cup grainy Dijon mustard", @"8 slices rustic white bread", @"8 slices Gruyere cheese", @"8 ounces sliced cooked ham", nil];

}

这是我的 numberOfRowInSection 方法

如何返回行数,即 16

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //how can I return no of rows (no of objects)

}

【问题讨论】:

  • 如果你手动创建了 16 个实例并且需要 16 行...让它返回 16
  • 或....将 16 个人添加到您保留的数组中,这将使 IMO 更容易

标签: ios objective-c iphone ios7


【解决方案1】:

显而易见的解决方案是使用一个数组,将所有食谱放入一个数组中,然后在 tableView 数据源上返回该数组的计数。类似的东西

// Declare it on your interface
@property (nonatomic, strong) NSArray *recipes;
...

// On your viewDidLoad
self.recipes = @[recipe1, recipe2]; // and so on
...

// UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.recipes.count;
}

【讨论】:

    【解决方案2】:

    一种方法是在类的实现中添加一个静态变量,并提供一种方法来获取它的值。

    当你调用Recipe类的构造函数时这个变量会加一,当你调用析构函数时这个变量会减一。

    【讨论】:

    • 不知道Objective-C有构造函数和析构函数。真的,这是一个糟糕的设计。他需要知道他的视图控制器正式存在的对象数量。
    • 关键不在于它是如何实现的,而在于如果你愿意的话,有一个构造函数的行为。传统上,对象计数是由静态类成员实现的。 Objective-C 有静态变量(实际上,它们与 C 类似,但服务几乎相同)。因此,该类应该自己知道它创建了多少对象。您不应该通过计算指针或其他任何东西来跟踪。在每个对象的alloc或dealloc方法中,都要处理类的实现文件的counter/static变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 2014-09-06
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 2013-06-03
    相关资源
    最近更新 更多