Objective-c and using NSString as a function parameter -
i have odd question (to me, anyway).
i calling function within objective-c program , passing in 3 arguments... home cooked object, nsmutablearray, , nsstring.
the function prototype looks this....
int loadnotetablearray (entryfile* entryfilename, nsmutablearray* nsmutablearrayinputentry, nsstring* title_string); i call within main 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 set in function this....
int loadnotetablearray (entryfile* entryfile1, nsmutablearray* notetablearray, nsstring* title_string) { i able use object entryfile1 , change in function using objects various methods , changes seen in main program. able add entries notetablearray , see in main program. however, changes nsstring not reflected in main program. able see title_string @"test string" @ start of function. set else in function value never makes main program.
i confused @ difference between these notations , objects.
thoughts?
ok.... code snippers... 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 aware check error used catch error conditions reported various functions after problem worked out...
you asked it... got it... toyota... here function... i'm not cleaning spacing posting site. of code play code if want go after that's ok aware you're not helping me much. reads text file (mac or windows), parses lines objects, , loads them 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; { 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; { 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; }
because strings immutable!
you can't change value of string @ it's current location in memory. in main program, have pointer location in memory storing value "test string". in method, when assign new value title_string, you're pointing local variable new location in memory. however, value of title_string in main program still points original location.
if, in method, said: entryfile1 = [[entryfile1 alloc] init];, guess wouldn't expect value of entryfilename in main program change. you'd intuitively understand in main application you're still referencing 'old' entryfile - not new 1 created. doing when set title_string new value. you're changing local variable's pointer... not pointer have in main program.
edit: to address op's comment passing nsstring in array
consider 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, believe, you're expecting. , i'm hoping you'll see how different doing, , why works (given explanation above).
Comments
Post a Comment