|
|
making an array of characters in an NSString
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: NYC
Status:
Offline
|
|
I'm using Obj-C and trying to take an NSString and get an NSArray of each of the characters in that string. currently i'm using the following code:
Code:
NSString *searchWord = [searchBox stringValue];
int wordLength = [searchWord length];
NSMutableArray *searchChars = [NSMutableArray new];
unichar *searchChar;
NSString *searchLetter;
int i;
for (i = wordLength; i > 0; i--) {
searchChar = [searchWord characterAtIndex:i]; // warning on this line
searchLetter = [NSString stringWithCharacters:searchChar length:1];
[searchChars addObject:searchLetter];
}
but i get a warning on the commented line telling me that i'm making a pointer from an integer without a cast. Now, if i don't make searchChar a pointer, then i get a warning on the next line saying i'm trying to make an integer out of a pointer without a cast... i can't win. one line needs a pointer, the other doesn't. is there some way i can get around this, or convert it to/from a pointer on the fly? i'm still somewhat new to this...
Thanks,
Max
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
That's because they take two different types, obviously. What you want is this:
Code:
NSString *searchWord = [searchBox stringValue];
int wordLength = [searchWord length];
NSMutableArray *searchChars = [NSMutableArray new];
unichar searchChar;
NSString *searchLetter;
int i;
for (i = wordLength; i > 0; i--) {
searchChar = [searchWord characterAtIndex:i]; // warning on this line
searchLetter = [NSString stringWithCharacters:&searchChar length:1];
[searchChars addObject:searchLetter];
}
The & is the address-of operator in C; it returns a pointer to a variable.
(It would also be faster to just go ahead and get all the characters at first and create your NSStrings from a unichar array, but that's just an efficiency issue.)
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
This is probably your best bet:
Code:
int i;
NSString *string = @"This is a test of the emergency broadcast system.";
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[string length]];
for ( i = 0; i < [string length]; i++ )
[array addObject:[NSNumber numberWithChar:[string characterAtIndex:i]]];
for ( i = 0; i < [array count]; i++ )
NSLog( @"%c", [[array objectAtIndex:i] charValue] );
The last for loop is just to iterate through to loop to make sure it worked...
Hope that helps.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: NYC
Status:
Offline
|
|
thanks, that worked great
edit: disco_stu, thanks for the suggestion, that might work better than how i have it now. and nice signature
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
No problem - I try to avoid pointers and all that garbage as much as possible.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by disco_stu:
This is probably your best bet:
Code:
int i;
NSString *string = @"This is a test of the emergency broadcast system.";
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[string length]];
for ( i = 0; i < [string length]; i++ )
[array addObject:[NSNumber numberWithChar:[string characterAtIndex:i]]];
for ( i = 0; i < [array count]; i++ )
NSLog( @"%c", [[array objectAtIndex:i] charValue] );
Except that turns the two-byte unichar into a one-byte char, so it won't work with non-char characters, thus defeating the inherent international capabilities of Cocoa.
Just thought it was worth mentioning.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
If you can handle having a standard C array instead of an objective-C NSArray, you can do this quite a bit more quickly. Warning: untested code:
Code:
unichar *theCharacters = nil;
size_t characterCount = 0;
// get an array of unichar characters and place into theCharacters
NSString *searchWord = [searchBox stringValue];
if (searchWord)
{
characterCount = [searchWord length];
theCharacters = malloc( (characterCount + 1) * sizeof(unichar) );
if (theCharacters)
[searchWord getCharacters: theCharacters];
else characterCount = 0;
}
// do something with each entry in our array
if (theCharacters != nil)
{
size_t arrayIndex;
for (arrayIndex = 0; arrayIndex < characterCount; ++arrayIndex)
{
unichar thisCharacter = theCharacters[arrayIndex];
// do whatever you want with thisCharacter
}
}
// free the memory allocated for the array so that it doesn't leak
if (theCharacters)
free( theCharacters );
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
Originally posted by Chuckit:
Except that turns the two-byte unichar into a one-byte char, so it won't work with non-char characters, thus defeating the inherent international capabilities of Cocoa.
Just thought it was worth mentioning.
Doh, Chuckit's 100% right - my bad!
I'm not sure what you're trying to get your code to do MaxPower2k3, but another possible option would be just to keep the string as an NSString. The string itself is essentially an array of characters, and you even have the 'characterAtIndex:' method if you need to access a specific character...
Just thinking aloud...
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: NYC
Status:
Offline
|
|
i have another question, so i thought i'd reuse this topic.
I'm trying to get the source of a page displayed in WebKit. Currently i'm using the following code:
Code:
NSString *source = [[[[webView mainFrame] dataSource] representation] documentSource];
but this returns null. I tried using a while loop to keep getting the source until the representation's method canProvideDataSource is TRUE, but it still comes back null. any ideas?
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Jan 2003
Location: NYC
Status:
Offline
|
|
never mind, i got it... used the WebFrameLoad delegate (or something like that) and the code:
Code:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
NSString *source;
source = [[[frame dataSource] representation] documentSource];
}
and it works
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|