Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > making an array of characters in an NSString

making an array of characters in an NSString
Thread Tools
MaxPower2k3
Mac Elite
Join Date: Jan 2003
Location: NYC
Status: Offline
Reply With Quote
Jul 23, 2003, 09:17 PM
 
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
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 23, 2003, 09:32 PM
 
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'."
     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Jul 23, 2003, 09:41 PM
 
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.
     
MaxPower2k3  (op)
Mac Elite
Join Date: Jan 2003
Location: NYC
Status: Offline
Reply With Quote
Jul 23, 2003, 09:42 PM
 
thanks, that worked great


edit: disco_stu, thanks for the suggestion, that might work better than how i have it now. and nice signature
     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Jul 23, 2003, 09:46 PM
 
No problem - I try to avoid pointers and all that garbage as much as possible.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 24, 2003, 02:31 AM
 
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'."
     
smeger
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 24, 2003, 04:58 AM
 
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.
     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Jul 24, 2003, 09:26 AM
 
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...
     
MaxPower2k3  (op)
Mac Elite
Join Date: Jan 2003
Location: NYC
Status: Offline
Reply With Quote
Jul 25, 2003, 05:17 PM
 
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?
     
MaxPower2k3  (op)
Mac Elite
Join Date: Jan 2003
Location: NYC
Status: Offline
Reply With Quote
Jul 25, 2003, 05:32 PM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:27 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,