How to convert NSString to NSArray with characters one by one in Objective-C -
this question has answer here:
i'd convert nsstring(ex. @"hello")
nsarray(ex. [@"h", @"e", @"l", @"l", @"o", nil])
.
first, tried use componentsseparatedbystring
. needs indicate separator, not. how can that?
here code:
@interface nsstring (converttoarray) -(nsarray *)converttoarray; @end @implementation nsstring (converttoarray) -(nsarray *)converttoarray { nsmutablearray *arr = [[nsmutablearray alloc]init]; (int i=0; < self.length; i++) { nsstring *tmp_str = [self substringwithrange:nsmakerange(i, 1)]; [arr addobject:[tmp_str stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]]; } return arr; } @end
:
- (void)foo { nsstring *exstring = @"hello"; nsarray *arr = [exstring converttoarray]; (nsstring *str in arr) { nslog(@"%@\n",str); } }
Comments
Post a Comment