• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Strings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strings


  • Subject: Re: Strings
  • From: Sherm Pendley <email@hidden>
  • Date: Tue, 12 Feb 2002 20:04:09 -0500

char input[258], output[258], *increment;
int index;

*input = [inputField stringValue]; //warning #1: assignment makes integer
//from pointer without a cast

stringValue: returns an NSString object, not a C string. Also note that, in C, an array *is* a pointer - a string is just a null-terminated array of chars. Thus, when you use "*input" above, you're dereferencing the pointer "input" and trying to store the results of the stringValue call in the first element of the array "input[]". (That's what the "makes integer from pointer" message is referring to.)

To get a C string, you want to do something like this:

input = [[inputField stringValue] cString];

if ( strlen( input ) <= 256 ) {

increment = &input[0];
index = 0;

while (*increment++) {
if ( input[index] != ' ' ) {
output[index] = input[index];
}
else {
output[index] = '\n';
}

index += 1;
}

output[index] = '\0';

[parsedField insertText:output]; //warning #2: passing arg 1 of 'insertText:'
//from incompatible pointer type

Same here, but the other way 'round. You're passing insertText: a C string, when it wants an NSString. Unlike above, you're correctly referring to "output" as a pointer, hence the error message about "incompatible pointer type." While it *is* a pointer, "output" is a pointer to char, not to NSString.

What you want is something more like this:

[parsedField insertText: [NSString stringWithCString: output]];

}
else {
[parsedField setStringValue:@"Error: input exceeded 256 characters."];
}


OR, you could do it all in Objective-C, like this, and avoid all the fixed-sized character buffers above - and their potential for buffer overruns if someone enters too much text.

NSString *inValue = [inputField stringValue];
NSArray *ta = [inValue componentsSeparatedByString: @" "]];
NSString *outValue = [ta componentsJoinedByString: @"\n"]
[parsedField insertText: outValue];

Hope this helps!

sherm--
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Strings (From: Daniel Byer <email@hidden>)

  • Prev by Date: Re: NSWindow: initWithWindowRef
  • Next by Date: Re: Why won't my file load?
  • Previous by thread: Strings
  • Next by thread: Re: Strings
  • Index(es):
    • Date
    • Thread