分段显示,为什么不能获取父类对象的信息呢

main.m如下

iOS 编程(第四版)第八章初级练习习题。

 1 #import <Foundation/Foundation.h> 2 #import "BNRContainer.h" 3 int main(int argc, const char * argv[]) { 4     @autoreleasepool { 5          6         //创建一个NSMutableArray对象,并用items变量保存该对象的地址 7         BNRContainer *items = [[BNRContainer alloc] init]; 8         for (int i = 0; i < 10; i++) { 9             BNRItem *item = [BNRItem randomItem];10             [items.subitems addObject:item];11         }12         // item数组一直为空,是为什么????????13         for (BNRItem *item in [items subitems]) {14            NSLog(@"%@",item);15        }16         NSLog(@"%@",items);17         //释放items所指向的NSMutableArray对象18         items = nil;19        20     }21     return 0;22 }

//

BNRContainer.h

//BNRItemsViewController.m

1 #import "BNRItem.h"2 3 @interface BNRContainer : BNRItem4 @property (nonatomic)NSMutableArray *subitems;5 - (NSString *)description;6 @end

//Homepwner

BNRContainer.m

//

 1 #import "BNRContainer.h" 2  3 @implementation BNRContainer 4 - (NSString *)description 5 { 6     NSString *string = [[NSString alloc] init]; 7     int sumtalto = 0; 8     NSLog(@"%@",self.subitems); 9         NSMutableArray *array = [self subitems];// 这个方法不对吗?? 不能获取subitems 中的BNRItem对象??10         for (int i = 0;i < [array count];i++) {11             12             sumtalto += [array[i] valueInDollars] + [self valueInDollars];13            14         }15         string = [NSString stringWithFormat:@"BNRItem and BNRContainer is total %d ",sumtalto];16         return string ;17     18 19 }20 @end

//Created by David on 5/25/16.

BNRItem.h

//Copyright © 2016 David. All rights reserved.

 1 #import <Foundation/Foundation.h> 2  3 @interface BNRItem : NSObject 4 { 5     NSString *_itemName; 6     NSString *_serialNumber; 7     int _valueInDollars; 8     NSDate *_dateCreated; 9 10 }11 12 + (instancetype)randomItem;13 //BNRItem类的指定初始化方法14 - (instancetype)initWithItemName:(NSString *)name15                   valueInDollars:(int)value16                     serialNumber:(NSString *)sNumber;17 18     19 20 21 - (instancetype)initWithItemName:(NSString *)name;22 23 - (instancetype)initWithItemName:(NSString *)name24                     serialNumber:(NSString *)sNumber;25 - (void)setItemName:(NSString *)str;26 - (NSString *)itemName;27 28 - (void)setSerialNumber:(NSString *)str;29 - (NSString *)serialNumber;30 31 - (void)setValueInDollars:(int)v;32 - (int)valueInDollars;33 34 - (NSDate *)dateCreated;35 @end

//

BNRItem.m

#import”BNRItemsViewController.h”

  1 #import "BNRItem.h"  2   3 @implementation BNRItem  4   5 + (instancetype)randomItem  6 {  7    //创建不可变数组对象,包含三个形容词  8     NSArray *randomAdjectiveList = @[@"Fluffy",@"Rusty",@"Shiny"];  9      10     //创建不可变数组对象,包含三个名词 11      12     NSArray *randomNounList = @[@"Bear",@"Spork",@"Mac"]; 13      14      15     //根据数组对象所含对象的个数,得到随机索引 16     //注意:运算符%是模运算符,运算后得到的是余数 17     //因此adjectiveIndex 是一个0到2的随机数 18      19     NSInteger adjectiveIndex = arc4random() % [randomAdjectiveList count]; 20     NSInteger nounIndex = arc4random() % [randomNounList count]; 21      22     //注意,类型为NSInteger的变量不是对象 23     //NSInteger是一种针对unsigned long的类型定义 24      25     NSString *randomName = [NSString stringWithFormat:@"%@  %@", 26                                   randomAdjectiveList[adjectiveIndex], 27                                        randomNounList[nounIndex]]; 28  29     int randomValue = arc4random() % 100; 30      31     NSString *randomserialNumber = [NSString stringWithFormat:@"%c%c%c%c%c", 32                                     '0' + arc4random() % 10, 33                                     'A' + arc4random() % 26, 34                                     '0' + arc4random() % 10, 35                                     'A' + arc4random() % 26, 36                                     '0' + arc4random() % 10]; 37      38     BNRItem *newItem = [[self alloc] initWithItemName:randomName 39                                        valueInDollars:randomValue 40                                          serialNumber:randomserialNumber]; 41      42     return newItem; 43  44  45  46 } 47 - (instancetype)initWithItemName:(NSString *)name 48                   valueInDollars:(int)value 49                     serialNumber:(NSString *)sNumber 50 { 51     //调用父类的指定初始化方法 52     self = [super init]; 53      54     //父类的初始化方法是否成功创建了父类对象 55     if  { 56         //为实例变量设定初始值 57          58         _itemName = name; 59         _valueInDollars = value; 60         _serialNumber = sNumber; 61          62         //设置_dateCreated 的值为系统当前时间 63         _dateCreated = [[NSDate alloc] init]; 64     } 65      66     //返回初始化后对象的新地址 67      68     return self; 69  70 } 71 - (instancetype)initWithItemName:(NSString *)name 72 { 73     return [self initWithItemName:name 74                    valueInDollars:0 75                      serialNumber:@""]; 76  77 } 78 - (instancetype)init 79 { 80     return [self initWithItemName:@"Item"]; 81  82 } 83 - (instancetype)initWithItemName:(NSString *)name serialNumber:(NSString *)sNumber 84 { 85     return [self initWithItemName:name 86                    valueInDollars:0 87                      serialNumber:sNumber]; 88  89 } 90 - (void)setItemName:(NSString *)str 91 { 92     _itemName = str ;//与self.itemName = str ;有什么区别? 93      94 } 95 - (NSString *)itemName 96 { 97     return _itemName; 98  99 }100 - (void)setSerialNumber:(NSString *)str101 {102 103     _serialNumber = str;104     105 }106 - (NSString *)serialNumber107 {108     return _serialNumber;109 }110 - (void)setValueInDollars:(int)v111 {112     _valueInDollars = v;113 }114 - (int)valueInDollars115 {116     return _valueInDollars;117 }118 - (NSDate *)dateCreated119 {120     return _dateCreated;121 }122 - (NSString *)description123 {124   NSString *descriptionString =125     [[NSString alloc] initWithFormat:@"%@ : Worth $%d,recorded on %@",126                       self.itemName,127                       self.serialNumber,128                       self.valueInDollars,129                       self.dateCreated ];130     131     return descriptionString;132 }133 @end

#import”BNRItemStore.h”

#import”BNRItem.h”

@interfaceBNRItemsViewController()

@propertyNSMutableArray*arr1;

@propertyNSMutableArray*arr2;

@end

@implementationBNRItemsViewController

-(instancetype)init

{

self= [superinitWithStyle:UITableViewStylePlain];

if(self) {

for(inti =0; i <5; i++) {

[[BNRItemStoresharedStore]createItem];

澳门网上正规赌场网址,}

}

returnself;

}

-(instancetype)initWithStyle:(UITableViewStyle)style

{

return[selfinit];

}

– (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

return2;

}

– (NSString*)tableView:(UITableView*)tableView
titleForHeaderInSection:(NSInteger)section

{

if(section ==0) {

return@”More than 50″;

}elseif(section ==1) {

return@”Less and equal to 50″;

}else{

returnnil;

}

}

– (NSInteger)tableView:(UITableView*)tableView

numberOfRowsInSection:(NSInteger)section

{

NSArray*all = [[BNRItemStoresharedStore]allItems];

self.arr1= [[NSMutableArrayalloc]init];

self.arr2= [[NSMutableArrayalloc]init];

for(BNRItem*iteminall) {

if(item.valueInDollars>50) {

[self.arr1addObject:item];

}else{

[self.arr2addObject:item];

}

}

if(section ==0) {

return[self.arr1count];

}else{

return[self.arr2count];

}

}

– (UITableViewCell*)tableView:(UITableView*)tableView

cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

UITableViewCell*cell =

[tableViewdequeueReusableCellWithIdentifier:@”UITableViewCell”forIndexPath:indexPath];

NSUIntegerrow = indexPath.row;

NSUIntegersection = indexPath.section;

if(section ==0) {

BNRItem*item =self.arr1[row];

cell.textLabel.text= [itemdescription];

returncell;

}

if(section ==1) {

BNRItem*item = [self.arr2objectAtIndex:row];

cell.textLabel.text= [itemdescription];

returncell;

}

returnnil;

}

– (void)viewDidLoad

{

[superviewDidLoad];

[self.tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@”UITableViewCell”];

}

@end

Post Author: admin

发表评论

电子邮件地址不会被公开。 必填项已用*标注