Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // name: first.mm
- // description: STL -> ObjectiveC conversion
- // compile: c++ first.mm -ObjC++ -framework Foundation
- #import <Foundation/Foundation.h>
- #include <string>
- #include <vector>
- using namespace std;
- NSArray *ToArray(const vector<string>& source) {
- NSMutableArray *result = [NSMutableArray arrayWithCapacity:source.size()];
- vector<string>::const_iterator it = source.begin();
- for ( ; it != source.end(); ++it) {
- const string& ss = *it;
- NSString *rs = [[[NSString alloc] initWithBytes:ss.c_str() length:ss.size() encoding:NSUTF8StringEncoding] autorelease];
- [result addObject:rs];
- }
- return result;
- }
- int main(int argc, char **argv) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- vector<string> v;
- for (int i = 1; i < argc; i++) { v.push_back(argv[i]); }
- NSArray *w = ToArray(v);
- for (NSInteger i = 0; i < [w count]; i++) {
- NSLog(@"%d: %@", i, [w objectAtIndex:i]);
- }
- [pool release];
- return 0;
- }
Add Comment
Please, Sign In to add comment