【发布时间】:2011-09-27 11:25:49
【问题描述】:
我有一个奇怪的问题(对我来说,无论如何)。
我在我的 Objective-C 程序中调用一个函数并传入三个参数...一个自制对象、一个 NSMutableArray 和一个 NSString。
函数原型长这样......
int LoadNoteTableArray (Entryfile* EntryfileName,
NSMutableArray* NSMutableArrayInputEntry, NSString* title_string);
我像这样从 main 内部调用它...
Entryfile* Entryfile1 = [[Entryfile alloc] init];
NSMutableArray *NoteTableArray = [[NSMutableArray alloc] initWithCapacity:1];
NSString* title_string;
title_string = @"test string";
checkerror = LoadNoteTableArray (Entryfile1, NoteTableArray, title_string);
而且在函数中是这样设置的......
int LoadNoteTableArray (Entryfile* Entryfile1, NSMutableArray* NoteTableArray,
NSString* title_string) {
我可以使用对象 Entryfile1 并使用对象的各种方法在函数中更改它,这些更改在主程序中可见。我可以将条目添加到 NoteTableArray 中,然后在主程序中查看。但是,对 NSString 的更改不会反映在主程序中。我可以在函数的开头看到 title_string 为 @"test string" 。它在函数中设置为其他值,但该值永远不会返回到主程序。
我对这些符号和对象之间的区别感到困惑。
想法?
好的....代码剪辑器...来自主程序...
checkerror = LoadConfigTableArray (Configfile1, ConfigEntryArray);
title_string = @"test string";
checkerror = LoadNoteTableArray (Entryfile1, NoteTableArray, &title_string);
[NoteTableArray sortUsingSelector:@selector(compareNoteEntryTime:)];
checkerror = RemediateNoteTableArray (NoteTableArray);
checkerror = WriteOutTimeFromNoteTableArray (NoteTableArray, ConfigEntryArray, title_string);
[NoteTableArray sortUsingSelector:@selector(compareNoteIDandMarkTime:)];
checkerror = WriteOutNotesFromNoteTableArray (NoteTableArray, ConfigEntryArray);
请注意,在我解决此问题后,将使用检查错误来捕获各种功能报告的错误情况...
您要求...您得到它...丰田...这是功能...我不会清理此发布站点的间距。一些代码是播放代码,所以如果你想继续它,那没关系,但要知道你对我帮助不大。它读取文本文件(Mac 或 Windows),将某些行解析为对象,并将它们加载到对象数组中。
int LoadNoteTableArray (Entryfile* Entryfile1, NSMutableArray* NoteTableArray, NSString** title_string) {
NSString* tokenclass;
NSString* previous_tokenclass;
NSString* token;
NSString* savedheaderid;
NSString* response;
NSString* tempstring;
NSString* tempstring2;
NSString* token_xx;
NSString* entryfile1path;
NSString* entryfile1name;
NSString* responseok;
NSNumber* temp_MarkTime;
NoteTableEntry* NoteTableEntrytemp;
char firstline;
char first_id_found;
int duplicate_count;
int arrayCount;
int loop_count;
entryfile1path = [NSString stringWithString: @"PATH"];
entryfile1name = [NSString stringWithString: @"notes"];
responseok = [Entryfile1 OpenEntryFile: entryfile1path withdatafilename: entryfile1name];
if ([responseok isEqualToString:@"ERROR"]) {return 1;};
if ([responseok isEqualToString:@"LAST"]) {return 2;};
firstline = 'N';
first_id_found = 'N';
tokenclass = nil;
*title_string = nil;
do {
if (tokenclass == nil) {previous_tokenclass = nil;}
if (tokenclass != nil) {previous_tokenclass = [NSMutableString stringWithString: tokenclass];}
tokenclass = nil;
token = [Entryfile1 GetNextToken];
if (token == nil) {break;};
tokenclass = [Entryfile1 ClassifyToken:token];
if ([tokenclass isEqualToString: @"i"]) {
[NoteTableArray addObject:[NoteTableEntry initNoteTableEntryForID:token]];
firstline = 'Y';
first_id_found = 'Y';
savedheaderid = response;
duplicate_count = 0;
previous_tokenclass = nil;
continue;};
if (([tokenclass isEqualToString: @"s"]) & (first_id_found == 'N')) {
*title_string = token;
continue;};
if (first_id_found == 'N') {continue;};
if (([tokenclass isEqualToString: @"t"]) & ([previous_tokenclass isEqualToString: @"t"] == FALSE)) {
arrayCount = [ NoteTableArray count ];
arrayCount--;
NoteTableEntrytemp = [NoteTableArray objectAtIndex:arrayCount];
[NoteTableEntrytemp AddTimeEntry:savedheaderid withtimestring:token];
continue;};
if (([tokenclass isEqualToString: @"t"]) & ([previous_tokenclass isEqualToString: @"t"] == TRUE)) {
duplicate_count++;
arrayCount = [ NoteTableArray count ];
arrayCount--;
NoteTableEntrytemp = [NoteTableArray objectAtIndex:arrayCount];
[NoteTableArray addObject:[NoteTableEntry initNoteTableEntryForID:[NoteTableEntrytemp NoteID]]];
arrayCount = [ NoteTableArray count ];
arrayCount--;
NoteTableEntrytemp = [NoteTableArray objectAtIndex:arrayCount];
[NoteTableEntrytemp AddTimeEntry:savedheaderid withtimestring:token];
[NoteTableEntrytemp AddNoteType:savedheaderid withnotetype:@"t"];
continue;};
if ([tokenclass isEqualToString: @"d"]) {
arrayCount = [ NoteTableArray count ];
arrayCount--;
NoteTableEntrytemp = [NoteTableArray objectAtIndex:(arrayCount - duplicate_count)];
[NoteTableEntrytemp AddNoteType:savedheaderid withnotetype:token];
if ([token isEqualToString: @"n"] & (arrayCount > 0)) {
NoteTableEntrytemp = [NoteTableArray objectAtIndex:(arrayCount - duplicate_count - 1)];
temp_MarkTime = NoteTableEntrytemp.TimeMark;
NoteTableEntrytemp = [NoteTableArray objectAtIndex:(arrayCount - duplicate_count)];
[NoteTableEntrytemp setTimeMark: temp_MarkTime];
}
continue;};
if (([tokenclass isEqualToString: @"s"]) & (firstline == 'Y')) {
arrayCount = [ NoteTableArray count ];
arrayCount--;
loop_count = 0;
do {
NoteTableEntrytemp = [NoteTableArray objectAtIndex:(arrayCount - loop_count)];
[NoteTableEntrytemp AddNoteHeader:savedheaderid withnoteheader:token];
if (loop_count == duplicate_count) {
tempstring = [NSString stringWithString: @" - XX:XX"];
tempstring2 = [token stringByAppendingString:tempstring];
token_xx = tempstring2;
[NoteTableEntrytemp AddNoteBody:savedheaderid withnotebody:token_xx];}
else {
[NoteTableEntrytemp AddNoteBody:savedheaderid withnotebody:token];};
loop_count++;
} while (loop_count <= duplicate_count);
firstline = 'N';
continue;};
if (([tokenclass isEqualToString: @"s"]) & (firstline == 'N')) {
arrayCount = [ NoteTableArray count ];
arrayCount--;
NoteTableEntrytemp = [NoteTableArray objectAtIndex:(arrayCount - duplicate_count)];
[NoteTableEntrytemp AddNoteBody:savedheaderid withnotebody:token];
firstline = 'N';
continue;};
} while (token);
return 0;
}
【问题讨论】:
-
因为我的问题真的没有得到回答。我能够通过使用指针传递 NSString 来使代码工作。但是,Entryfile 不需要指针,并且传递的样式不同。如果您对此有答案,我将放弃金色的“答案”框顶。
标签: objective-c function pass-by-reference