我认为我缺少一些关于Xcode Objective C编程标准的基础知识。不幸的是我无法找到解决问题的合适方法。
问题在于,当我尝试将数据保存在一个对象数组中时,就无法将它们分开保存。添加新对象会覆盖数组中的先前对象。以下是一些代码:
CustomObject.m文件:
#import "CustomObject.h"
NSString * title;
NSString * detail;
@implementation CustomObject
- (void) initCustomObjectWithValues : (NSString *) iTitle : (NSString *) iDetail {
title = [NSString stringWithString:iTitle];
detail = [NSString stringWithString:iDetail];
}
- (NSString *) getTitle {
return title;
}
- (NSString *) getDetail {
return detail;
}
@end
ViewController.m文件中的viewDidLoad函数:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myMutableArray = [[NSMutableArray alloc] init];
for (int i=0; i<10; i++) {
NSString * tempTitle = [@"title " stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
CustomObject * myCustomObject = [[CustomObject alloc] init];
[myCustomObject initCustomObjectWithValues :[NSString stringWithFormat:@"%@",tempTitle]
:[@"detail " stringByAppendingString:[NSString stringWithFormat:@"%d",i]]];
[myMutableArray addObject:myCustomObject];
}
for (int i=0; i<10; i++) {
NSLog(@"%@",[[myMutableArray objectAtIndex:i] getTitle]);
NSLog(@"%@",[[myMutableArray objectAtIndex:i] getDetail]);
NSLog(@"----------------------------");
}
}
此处, myMutableArray 定义在 ViewController.m 文件的顶部。 (使其成为全球性的,将来可用于其他功能)
这是我在日志中的内容:
title 9
detail 9
----------------------------
title 9
detail 9
----------------------------
title 9
detail 9
----------------------------
title 9
detail 9
----------------------------
title 9
detail 9
----------------------------
title 9
detail 9
----------------------------
title 9
detail 9
----------------------------
title 9
detail 9
----------------------------
title 9
detail 9
----------------------------
title 9
detail 9
----------------------------
据我所知,每个新添加的对象都会覆盖旧的。首先我认为它们指的是相同的分配内存但是在调试工具中myMutableArray看起来像这样:
Printing description of myMutableArray:
<__NSArrayM 0x8d8cb60>(
<CustomObject: 0x8d8e990>,
<CustomObject: 0x8d8dd40>,
<CustomObject: 0x8d8d2e0>,
<CustomObject: 0x8d8d470>,
<CustomObject: 0x8d8d350>,
<CustomObject: 0x8d8ddf0>,
<CustomObject: 0x8d8df00>,
<CustomObject: 0x8d8df40>,
<CustomObject: 0x8d8dff0>,
<CustomObject: 0x8d8e0c0>
)
有没有人对解决方案有所了解。它应该是非常基本的东西,但我无法解决问题。
提前谢谢大家
答案 0 :(得分:3)
NSString * title;
NSString * detail;
@interface
部分之外的创建全局变量。将变量分配给title
或detail
时,不设置对象的实例变量,而是更改这些全局变量。因为它们是全局的,所以引用它们的所有对象都是相同的。
将这些全局变量转换为实例变量,甚至更好地使用@property
。
你的代码总体上是不好的目标。
您不应在返回变量的getter中使用get
。您不应该使用以init
开头并且不返回self的方法。您只应在init
情况下致电[[Foo alloc] init...]
。您应该避免在方法中使用未命名的参数。
并且不需要从字符串中创建字符串。
以下是我的写作方式:
// CustomObject.h
@interface CustomObject : NSObject
@property (copy, nonatomic) NSString * title;
@property (copy, nonatomic) NSString * detail;
- (id)initWithTitle:(NSString *)title detail:(NSString *)detail
@end
// CustomObject.m
#import "CustomObject.h"
@implementation CustomObject
- (id)initWithTitle:(NSString *)title detail:(NSString *)detail {
self = [super init];
if (self) {
// use stringWithString: to create @"" strings when title is nil
// if nil is a valid value for those variables you should use
// _title = [title copy];
_title = [NSString stringWithString:title];
_detail = [NSString stringWithString:detail];
}
return self;
}
@end
for (int i=0; i<10; i++) {
NSString *tempTitle = [NSString stringWithFormat:@"title %d",i];
NSString *tempDetail = [NSString stringWithFormat:@"detail %d",i];
CustomObject * myCustomObject = [[CustomObject alloc] initWithTitle:tempTitle detail:tempDetail];
[myMutableArray addObject:myCustomObject];
}
for (int i=0; i<10; i++) {
CustomObject *object = myMutableArray[i];
NSLog(@"%@", object.title);
// or NSLog(@"%@", [object title]); if you don't like dot-notation.
NSLog(@"%@", object.detail);
NSLog(@"----------------------------");
}