0

私は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

ここからオブジェクトをループして、分割ビューのリストに追加します。

- (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

}

簡単にフォローできることを願っています。そうでない場合はお知らせください。もう一度試します。

4

1 に答える 1

1

initWithTitle メソッドでは、変数を割り当てていますが、保持していません。それらがどこにも保持されていない場合、それらは解放され、それらにアクセスしようとするとアプリがクラッシュします。エラー メッセージが表示されない場合は、デバッグを有効にしてください。

于 2010-09-09T06:38:05.813 に答える