正如您可以通过我的问题告诉我,我是Objective C的新手。我可以访问静态属性,例如NSString,但似乎我无法访问数组甚至其他属性,必须做错了。基本上,我有一个模型文件,包含我的主要属性。
这是我的名为ListingManager.h的模型文件
#import <Foundation/Foundation.h>
@interface ListingManager : NSObject
@property (nonatomic, strong) NSString *listingTitle;
@property (nonatomic, strong) NSString *listingDescription;
@property (nonatomic, strong) NSString *listingPrice;
@property (nonatomic, readonly) NSString *datePosted;
@property (nonatomic, retain) NSMutableArray *lists;
@property (nonatomic) BOOL *viewed;
@end
ListingManager.m
#import "ListingManager.h"
@implementation ListingManager
@synthesize lists;
@synthesize listingPrice;
@synthesize listingDescription;
@synthesize listingTitle;
@synthesize datePosted;
@end
这是我正在使用它的另一个视图控制器ListingTableViewController.m
#import "ListingTableViewController.h"
@interface ListingTableViewController ()
@property (nonatomic, strong) ListingManager *manageListings;
@end
@implementation ListingTableViewController
@synthesize manageListings;
- (void) loadInitialData {
ListingManager *listing1 = [[ListingManager alloc] init];
listing1.listingTitle = @"Title here";
listing1.listingDescription = @"another description..Whatever it is";
listing1.listingPrice = @"100";
[self.manageListings.lists addObject:listing1];
NSLog(@"%@", listing1); //for debugging purposes
ListingManager *listing2 = [[ListingManager alloc] init];
listing2.listingTitle = @"Title here";
listing2.listingDescription = @"a long description here";
listing2.listingPrice = @"50";
[self.manageListings.lists addObject:listing2];
ListingManager *listing3 = [[ListingManager alloc] init];
listing3.listingTitle = @"Cool stuff";
listing3.listingDescription = @"Another descitpion righ there";
listing3.listingPrice = @"90";
[self.manageListings.lists addObject:listing3];
}
- (instancetype)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//self.listings= [[NSMutableArray alloc] init];
self.manageListings = [[ListingManager alloc] init];
[self.backdropTableView reloadData]; //reload table view
[self loadInitialData]; //load inital static data
}
/*
- (void)viewWillAppear:(BOOL)animated
{
[self.backdropTableView reloadData];
}
*/
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.manageListings.lists count]; //this here is not returning anything
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *custom = @"WXCustomCell";
WXTableCell *cell = (WXTableCell *)[tableView dequeueReusableCellWithIdentifier:custom];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WXCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
ListingManager *listingManager = [self.manageListings.lists objectAtIndex:indexPath.row];
cell.titleLabel.text = listingManager.listingTitle;
cell.descriptionlabel.text = listingManager.listingDescription;
cell.thumbnail.image = [UIImage imageNamed:@"nothumb.gif"];
cell.price.text = @"$50";
cell.dateLabel.text = @"N/A";
NSLog(@"%@", cell.titleLabel.text);
//use for later
/*
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"MM/dd/yyy HH:mm:ss"];
NSLog(@"%@",[DateFormatter stringFromDate:[NSDate date]]);
*/
return cell;
}
//Call segue
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"showDetail" sender:nil];
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"showDetail"]) {
// Get the new view controller using [segue destinationViewController].
NSIndexPath *indexPath = [self.backdropTableView indexPathForSelectedRow];
DetailsViewController *vc = segue.destinationViewController;
// Pass the selected object to the new view controller.
ListingManager *listingManager = [self.manageListings.lists objectAtIndex:indexPath.row];
vc.titleName = listingManager.listingTitle;
vc.descriptionDetail = listingManager.listingDescription;
}
}
@end
例如,当我返回&#39; [self.manageListings.lists count]&#39;时,它不会返回任何内容,如果我将其硬编码为3,我会得到3个初始项目,标题,描述和标题为空。连接我的模型和VC时,一定有什么错误。
答案 0 :(得分:1)
在Objective-C中,您已手动为对象分配内存。声明像@property (nonatomic, retain) NSMutableArray *lists;
这样的iVar(属性)时,所有运行时和编译器都会为该属性创建指针内存并将内存值设置为0x00
。因此,尚未分配阵列存储器。你应该像这样实例化内存
self.manageListings = [[ListingManager alloc] init];
self.manageListings.lists = [[NSMutableArray alloc] init];
[self.manageListings.lists addObject:listing1];
向nil
对象发送消息只会返回,因此您不会发现任何崩溃。
答案 1 :(得分:0)
可能是因为您在viewDidLoad
[self.backdropTableView reloadData]; //reload table view
[self loadInitialData]; //load inital static data
对此进行了尝试
[self loadInitialData]; //load inital static data
[self.backdropTableView reloadData]; //reload table view
答案 2 :(得分:0)
正如iRavi指出的,你sholud分配内存。但是在内部对象init方法中分配是更好的做法。以这种方式覆盖它:
- (id) init {
if (self = [super init]) // not ==
{
_lists = [NSMutableArray new];
}
return self;
}
_lists - 自动生成的iVar,如果省略@synthesize