无法访问objective-c中的对象属性

时间:2010-09-09 05:45:25

标签: objective-c uitableview nsobject

我刚刚跳进了Objective-C,很早就被卡住了。我将使用我的问题发布代码,但为了保持可读性,我将摆脱一些垃圾,如果你想要更多代码发布,请告诉我!

我创建了一个名为'Phrase'的新对象(从NSObject继承),并且正在将JSON中的项目读入这些'Phrase'对象并将它们添加到数组中。这是第一批代码:

示例JSON:

    {
    "phrases": [
        {
            "title": "Title of my Phrase",
         "definition" : "A way to look at some words",
   "location" : "Irish Proverb"
        }   
    ]
    }

脚本我正在阅读:

    - (void)viewDidLoad {
    [super viewDidLoad];

 self.phraseDictionary = [[NSMutableArray alloc] initWithObjects:nil];

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"phrase" ofType:@"json"];  
 NSString *myRawJSON = [[NSString alloc] initWithContentsOfFile:filePath];

 NSData *jsonData = [myRawJSON dataUsingEncoding:NSUTF32BigEndianStringEncoding];
 NSDictionary *entries = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil];


 for (id key in entries) {

  NSObject *phrases = [entries objectForKey:key];

  for (id phrase in phrases) {

   Phrase *pushArrayToPhrase = [[Phrase alloc] initWithText:[phrase objectForKey:@"title"] definition:[phrase objectForKey:@"definition"] location:[phrase objectForKey:@"location"]];
   [self.phraseDictionary addObject:pushArrayToPhrase];

  }

    }
    }

短语m文件:

#import "Phrase.h"


@implementation Phrase

@synthesize title;
@synthesize definition;
@synthesize location;

- (id)init {
 self = [super init];
 if (self != nil) {
  title = @"";
  definition = @"";
  location = @"";
 }
 return self;
}

- (id)initWithTitle:(NSString *)tit definition:(NSString *)def location:(NSString *)loc {
 self = [super init];
 if (self != nil) {
  title = tit;
  definition = def;
  location = loc;
 } 
 return self;
}


@end

从这里开始遍历对象并将它们添加到我的splitview中的列表中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *CellIdentifier = @"CellIdentifier";

 // Dequeue or create a cell of the appropriate type.
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    Phrase *cellPhrase = [self.phraseDictionary objectAtIndex:indexPath.row];
 cell.textLabel.text = cellPhrase.title;
 return cell;
}

但是当我点击一个项目并根据单击的indexPath.row请求一个短语时,我只能访问cell.textLabel.text中使用的属性。从此处开始从Phrase对象访问属性的任何其他尝试都将退出模拟器。

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 Phrase *cellPhrase = [self.phraseDictionary objectAtIndex:indexPath.row];
 detailViewController.detailItem = cellPhrase;
 //If i attempt 'cellPhrase.definition' here, the app will close without an error

}

希望这很容易理解,如果不是,请告诉我,我会再试一次!

1 个答案:

答案 0 :(得分:1)

在initWithTitle方法中,您要分配变量但不保留变量。如果它们没有保留在任何地方,它们将被释放,当您尝试访问它们时,您的应用程序将崩溃。如果您没有收到任何错误消息,请确保打开调试。