Objective-c 并使用 NSString 作为函数参数

发布于 2024-11-18 03:08:23 字数 6564 浏览 2 评论 0原文

我有一个奇怪的问题(至少对我来说)。

我正在 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 视为@“测试字符串”。它在函数中被设置为其他值,但该值永远不会返回到主程序。

我对这些符号和对象之间的区别感到困惑。

想法?

好的....代码片段...来自主例程...

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;

}

I have an odd question (to me, anyway).

I am calling a function within my Objective-C program and am passing in three arguments... a home cooked object, an NSMutableArray, and an NSString.

The function prototype looks like this....

 int LoadNoteTableArray (Entryfile* EntryfileName, 
      NSMutableArray* NSMutableArrayInputEntry, NSString* title_string);

I call it from within main like this...

 Entryfile* Entryfile1 = [[Entryfile alloc] init];
 NSMutableArray *NoteTableArray = [[NSMutableArray alloc] initWithCapacity:1];
 NSString*          title_string;

title_string = @"test string";

checkerror = LoadNoteTableArray (Entryfile1, NoteTableArray, title_string);

And it is set up in the function like this....

 int LoadNoteTableArray (Entryfile* Entryfile1, NSMutableArray* NoteTableArray,
   NSString* title_string) {

I am able to use the object Entryfile1 and change it in the function using the objects various methods and those changes are seen in the main program. I am able to add entries into NoteTableArray and see then back in the main program. However, changes to the NSString do not get reflected back in the main program. I am able to see title_string as @"test string" at the start of the function. It is set to something else in the function but that value never makes it back to the main program.

I am confused at the difference between these notations and objects.

Thoughts?

Ok.... code snippers... from the main routine...

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);

Please be aware that check error will be used to catch error conditions reported from the various functions after I get this problem worked out...

You asked for it... you got it... Toyota... here is the function... I'm not cleaning up the spacing for this posting site. Some of the code was play code so if you want to go after it that's ok but be aware that you're not helping me much. It reads a text file (Mac or Windows), parses certain lines into objects, and loads them into an object array.

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叶落知秋 2024-11-25 03:08:23

因为字符串是不可变的!

您无法更改字符串在内存中当前位置的值。在您的主程序中,您有一个指向内存中存储值“测试字符串”的位置的指针。在您的方法中,当您为 title_string 分配新值时,您将局部变量指向内存中的新位置。但是,主程序中的title_string值仍然指向原始位置。

如果在您的方法中,您说:Entryfile1 = [[Entryfile1 alloc] init];,我的猜测是您不会期望EntryfileName<的值/code> 在您的主程序中进行更改。您会直观地了解到,在您的主应用程序中,您仍然引用“旧”Entryfile - 而不是您刚刚创建的新文件。这本质上就是您将 title_string 设置为新值时所做的事情。您只是更改局部变量的指针...而不是主程序中的指针。

编辑:
为了解决 OP 关于在数组中传递 NSString 的评论

,请考虑以下应用程序:

#import <Foundation/Foundation.h>

void foo (NSString ** stringRef);

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString * myString = @"Testing";

    NSLog(@"%@", myString);

    foo(&myString);

    NSLog(@"%@", myString);

    [pool drain];
    return 0;
}

void foo (NSString ** stringRef)
{
    *stringRef = @"Bar";
}

我相信,此处的结果就是您所期望的 >。我希望您会看到这与您正在做的事情有何不同,以及为什么它有效(鉴于我上面的解释)。

Because Strings are immutable!

You can't change the value of a string at it's current location in memory. In your main program, you have a pointer to a location in memory which is storing the value "test string". In your method, when you assign a new value to title_string, you're pointing your local variable to a new location in memory. However, the value of title_string in your main program still points to the original location.

If, in your method, you said: Entryfile1 = [[Entryfile1 alloc] init];, my guess is you wouldn't expect the value of EntryfileName in your main program to change. You'd intuitively understand that in your main application you're still referencing the 'old' Entryfile - not the new one you just created. This is essentially what you are doing when you set title_string to a new value. You're only changing the local variable's pointer... not the pointer you have in your main program.

EDIT:
To address the OP's comment about passing an NSString in an array

Consider the following app:

#import <Foundation/Foundation.h>

void foo (NSString ** stringRef);

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString * myString = @"Testing";

    NSLog(@"%@", myString);

    foo(&myString);

    NSLog(@"%@", myString);

    [pool drain];
    return 0;
}

void foo (NSString ** stringRef)
{
    *stringRef = @"Bar";
}

The result here, I believe, is what you're expecting. And I'm hoping that you'll see how this is different than what you are doing, and why this works (given my explanation above).

浅笑轻吟梦一曲 2024-11-25 03:08:23

您无法更改 NSString。即使在您调用的方法中也不行。它们是一成不变的。如果您在字符串上调用类似 stringByAppendingString: 的方法,则不会修改字符串,而是返回一个新的 NSString 对象。

您很可能需要 NSMutableString

You can't change NSStrings. Not even within the method you are calling. They are immutable. If you are calling something like stringByAppendingString: on the string, that doesn't modify the string, it returns a new NSString object.

Chances are, you want NSMutableString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文