【问题标题】:How do I properly acces properties in Model class - Objective C [duplicate]如何正确访问模型类中的属性 - 目标 C [重复]
【发布时间】:2014-08-22 17:37:41
【问题描述】:

从我的问题可以看出,我是 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

例如,当我返回“[self.manageListings.lists count]”时,它不会返回任何内容,但如果我将其硬编码为 3,我会得到 3 个初始项目,其中标题、描述和标题分别为空值。连接我的模型和 VC 一定是我做错了什么。

【问题讨论】:

    标签: objective-c model-view-controller ios7 nsmutablearray


    【解决方案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 对象发送消息只会返回,因此您不会观察到任何崩溃。

    【讨论】:

      【解决方案2】:

      你应该分配内存,正如 iRavi 所指出的那样。但更好的做法是在对象 init 方法中分配。以这种方式覆盖它:

      - (id) init {
      
         if (self = [super init]) // not ==
         {
            _lists = [NSMutableArray new];
         }
         return self;
      }
      

      _lists - 一个自动生成的 iVar,如果你省略 @synthesize

      【讨论】:

        【解决方案3】:

        可能是因为你在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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多