Can';t访问对象';objective-c中的s性质
我刚跳进Objective-C,很早就被卡住了。我会用我的问题发布代码,但为了保持可读性,我会去掉一些垃圾,如果你想发布更多代码,请告诉我 我已经创建了一个名为“短语”的新对象(从NSObject子类化),并且正在将JSON中的项读取到这些“短语”对象中,并将它们添加到一个数组中。以下是第一批代码: 示例JSON:Can';t访问对象';objective-c中的s性质,objective-c,uitableview,nsobject,Objective C,Uitableview,Nsobject,我刚跳进Objective-C,很早就被卡住了。我会用我的问题发布代码,但为了保持可读性,我会去掉一些垃圾,如果你想发布更多代码,请告诉我 我已经创建了一个名为“短语”的新对象(从NSObject子类化),并且正在将JSON中的项读取到这些“短语”对象中,并将它们添加到一个数组中。以下是第一批代码: 示例JSON: { "phrases": [ { "title": "Title of my Phrase", "defin
{
"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];
}
}
}
#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
我正在阅读的脚本:
{
"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];
}
}
}
#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
短语m文件:
{
"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];
}
}
}
#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.textlab.text中使用的属性。从此处开始,从短语对象访问属性的任何其他尝试都将退出模拟器
- (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
}
希望这是容易遵循,让我知道如果不是,我会再试一次 在initWithTitle方法中,您分配变量,但不保留它们。如果它们没有保留在任何地方,它们将被释放,当你试图访问它们时,你的应用程序将崩溃。如果没有收到任何错误消息,请确保打开调试。有任何信息说明它存在的原因吗?你检查过日志了吗?正如我刚才在下面发布的,当我打开调试时,我收到了这个“程序接收信号:“EXC_BAD_ACCESS”。我看到了这个链接,但无法将其与我的情况联系起来……你所说的分配但不保留是什么意思?(我对这一点还不熟悉,所以仍在考虑分配/保留等问题)-我的头衔=乳头;definition=def等在initWithTitle方法中,我是否需要以不同的方式执行此操作?另外-我打开了调试,并收到“程序接收信号:“EXC\u BAD\u ACCESS”。“当您执行title=tit;没有任何东西可以阻止字符串保留的内存稍后被释放。因此,下次尝试访问字符串时,程序将崩溃,因为内存不再有效。要防止出现这种情况,请尝试title=[tit retain];或者如果你的财产是保留的,你可以做self.title=tit;然后在您的dealloc方法中执行[标题发布];以确保稍后清除内存。谢谢堆,这就成功了。你对objective-c中关于内存管理/保留等方面的阅读有什么建议,甚至对一般原则有什么建议?